string process

 

Assumption for Special Characters:

Let’s define what each special character does (you can modify this logic later):

  • # → Repeat the previous character.

  • % → Remove the last character.

  • * → Replace the last character with *.

     

     

    class Solution {
        public String processStr(String s) {
            StringBuilder res = new StringBuilder();

            for (char c : s.toCharArray()) {
                if (c >= 'a' && c <= 'z') {
                    res.append(c);
                } else if (c == '*') {
                    if (res.length() > 0) {
                        res.deleteCharAt(res.length() - 1);
                    }
                } else if (c == '#') {
                    res.append(res.toString());
                } else if (c == '%') {
                    res.reverse();
                }
            }

            return res.toString();
        }
    }
     

Comments

Popular Posts