All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0901_1000.s0917_reverse_only_letters.Solution Maven / Gradle / Ivy

There is a newer version: 1.37
Show newest version
package g0901_1000.s0917_reverse_only_letters;

// #Easy #String #Two_Pointers

public class Solution {
    public String reverseOnlyLetters(String s) {
        char[] array = s.toCharArray();
        int i = 0;
        int j = array.length - 1;
        while (i < j) {
            if (Character.isLetter(array[i]) && Character.isLetter(array[j])) {
                char temp = array[i];
                array[i++] = array[j];
                array[j--] = temp;
            } else if (Character.isLetter(array[i])) {
                j--;
            } else if (Character.isLetter(array[j])) {
                i++;
            } else {
                i++;
                j--;
            }
        }
        return new String(array);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy