Grokking LinkedIn Coding Interview
Ask Author
Back to course home

0% completed

Vote For New Content
Zachary Nelson
Why the nested while loops? Same complexity, much simpler solution

Zachary Nelson

Oct 17, 2024

There is a much simpler way to solve this problem and maintain O(n) Time and O(1) space complexity

class Solution { isPalindrome(s) { s = s.toLowerCase().replace(/[^a-z0-9]/g, ''); let l = 0; let r = s.length - 1; while (l < r) { if (s[l] !== s[r]) return false l++ r-- }
return true;

}

}

0

0

Comments
Comments
Brandon W
Brandon Wa year ago

Because this requires two passes. You are removing any non-numeric or alpha character from the string then parsing it.

On this page