Algorithm


Problem Name: 917. Reverse Only Letters

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 '\\'.

Code Examples

#1 Code Example with Java Programming

Code - Java Programming


class Solution {
  public String reverseOnlyLetters(String s) {
    int start = 0;
    int end = s.length() - 1;
    char[] letters = s.toCharArray();
    while (start  < = end) {
      while (start <= end && !Character.isLetter(s.charAt(start))) {
        start++;
      }
      while (end >= start && !Character.isLetter(s.charAt(end))) {
        end--;
      }
      if (start  < = end) {
        char temp = letters[start];
        letters[start++] = letters[end];
        letters[end--] = temp;
      }
    }
    return String.valueOf(letters);
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
s = "ab-cd"

Output

x
+
cmd
"dc-ba"

#2 Code Example with Javascript Programming

Code - Javascript Programming


const reverseOnlyLetters = function(S) {
  let start = 0
  let end = S.length - 1
  const arr = S.split("")
  while (start < end) {
    while (start < end && !chk(S.charCodeAt(start))) {
      start++
    }
    while (start < end && !chk(S.charCodeAt(end))) {
      end--
    }

    let tmp = S[end]
    arr[end] = S[start]
    arr[start] = tmp
    start++
    end--
  }
  return arr.join("")
}

function chk(num) {
  const aCode = "a".charCodeAt(0)
  const zCode = "z".charCodeAt(0)
  const ACode = "A".charCodeAt(0)
  const ZCode = "Z".charCodeAt(0)

  if ((num >= aCode && num <= zCode) || (num >= ACode && num <= ZCode)) {
    return true
  } else {
    return false
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
s = "ab-cd"

Output

x
+
cmd
"dc-ba"

#3 Code Example with Python Programming

Code - Python Programming


class Solution:
    def reverseOnlyLetters(self, S):
        r = [s for s in S if s.isalpha()]
        return "".join(S[i] if not S[i].isalpha() else r.pop() for i in range(len(S)))
Copy The Code & Try With Live Editor

Input

x
+
cmd
s = "a-bC-dEf-ghIj"

Output

x
+
cmd
"j-Ih-gfE-dCba"

#4 Code Example with C# Programming

Code - C# Programming


using System.Text;

namespace LeetCode
{
    public class _0917_ReverseOnlyLetters
    {
        public string ReverseOnlyLetters(string S)
        {
            var sb = new StringBuilder();

            var j = S.Length - 1;
            for (int i = 0; i  <  S.Length; i++)
            {
                if (char.IsLetter(S[i]))
                {
                    while (!char.IsLetter(S[j]))
                        j--;

                    sb.Append(S[j--]);
                }
                else
                    sb.Append(S[i]);
            }

            return sb.ToString();
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
s = "a-bC-dEf-ghIj"

Output

x
+
cmd
"j-Ih-gfE-dCba"
Advertisements

Demonstration


Previous
#916 Leetcode Word Subsets Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#918 Leetcode Maximum Sum Circular Subarray Solution in C, C++, Java, JavaScript, Python, C# Leetcode