Skip to content

Latest commit

 

History

History

125. Valid Palindrome

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Valid Palindrome

Link to the problem here.

class Solution {
    public boolean isPalindrome(String s) {
        s = s.toLowerCase();
        int left = 0;
        int right = s.length() - 1;
        while (left < right){
            char leftLetter = s.charAt(left);
            char rightLetter = s.charAt(right);
            if (!Character.isLetterOrDigit(leftLetter)){
                left++;
            }
            else
            if (!Character.isLetterOrDigit(rightLetter)){
                right--;
                continue;
            }
            else
           { if (leftLetter != rightLetter)
            return false;
            else{
                left++;
                right--;
            }}
        }
        return true;
    }
}

Time Complexity: O(n), Space Complexity: O(1)

Explanation: To solve this problem i use two indexes, what start from begin and end of string. first think first check if any of characters with index is a space, if yes, we skip tis index, if not we check are a symbols the same. If yes , we just bring next indexes, if not, we return false.