Description

Given a string s, reverse the string according to the following rules:

  • All the characters that are not English letters remain in the same position.
  • All the English letters (lowercase or uppercase) should be reversed.

Return s after reversing it.

Example 1:

Input: s = "ab-cd"

Output: "dc-ba"

Example 2:

Input: s = "a-bC-dEf-ghIj"

Output: "j-Ih-gfE-dCba"

Example 3:

Input: s = "Test1ng-Leet=code-Q!"

Output: "Qedo1ct-eeLg=ntse-T!"

Constraints:

  • 1 <= s.length <= 100
  • s consists of characters with ASCII values in the range [33, 122].
  • s does not contain '\"' or '\\'.

Solution

In this case, we need two pointers, just like reversing a string. However, in this case, we want to know when left index has non-alphabetic characters, at that point, we want to insert that character and increase only left pointer and if right index has non-alphabetic character, then we simply decrement the right pointer. In this case, we have to loop until we reach end of the string with left pointer.

 1class Solution {
 2    public String reverseOnlyLetters(String s) {
 3        StringBuilder result = new StringBuilder();
 4        if (s == null || s.length() == 0)
 5            return s;
 6        int left = 0, right = s.length() - 1;
 7        while (left < s.length()) {
 8            if (Character.isAlphabetic(s.charAt(left)) && Character.isAlphabetic(s.charAt(right))) {
 9                result.append(s.charAt(right--));
10                left++;
11            } else if (!Character.isAlphabetic(s.charAt(left))) {
12                result.append(s.charAt(left++));
13            } else if (!Character.isAlphabetic(s.charAt(right))) {
14                right--;
15            }
16        }
17        return result.toString();
18    }
19}