DEV Community

Debesh P.
Debesh P.

Posted on

125. Valid Palindrome | LeetCode | Top Interview 150 | Coding Questions

Problem Link

https://leetcode.com/problems/valid-palindrome/


Detailed Step-by-Step Explanation

https://leetcode.com/problems/valid-palindrome/solutions/7446986/most-optimal-approach-beats-100-two-poin-uoy9


leetcode 125


Solution

class Solution {
    public boolean isPalindrome(String s) {

        s = s.toLowerCase();
        int left = 0;
        int right = s.length() - 1;

        while (left < right) {
            char chLeft = s.charAt(left);
            char chRight = s.charAt(right);

            if (!(chLeft >= 'a' && chLeft <= 'z' || chLeft >= '0' && chLeft <= '9')) {
                left++;
                continue;
            }

            if (!(chRight >= 'a' && chRight <= 'z' || chRight >= '0' && chRight <= '9')) {
                right--;
                continue;
            }

            if (chLeft != chRight) {
                return false;
            }

            left++;
            right--;
        }

        return true;
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)