valid palindrome

2024. 11. 14. 20:34boj

Approach

two way solution first
string to lowerCase and start pointing end pointing
if start and end is not same then return false;

Complexity

  • Time complexity:

O(N)

  • Space complexity:

O(1)

function isAlphaNumering(s: string): boolean {
    return (s >= '0' && s <= '9') || (s >= 'a' && s <= 'z');
}

function isPalindrome(s: string): boolean {
    let i = 0;
    let j = s.length - 1;
    s = s.toLowerCase();

    while (i < j) {
        const iNumeric: boolean = isAlphaNumering(s[i]);
        const jNumeric: boolean = isAlphaNumering(s[j]);
        if (iNumeric && jNumeric) {
            if (s[i] !== s[j]) {
                return false;
            }

            i++;
            j--;
        } else if (iNumeric){
            j--;
        } else {
            i++;
        }
    }
    return true;
};

'boj' 카테고리의 다른 글

leet code binary search  (0) 2024.11.20
leet code invert binary tree  (0) 2024.11.16
leet code valid-parentheses  (4) 2024.11.13
경쟁적 전염  (0) 2023.01.30
15649 문제 해결.  (0) 2023.01.04