Last active
August 11, 2021 01:35
-
-
Save akaila/ac3a5515e8bbe50f351a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//http://www.glassdoor.com/Interview/Given-a-large-input-string-write-a-function-to-check-if-it-is-a-palindrome-according-to-the-following-restrictions-low-QTN_927605.htm | |
//Given a large input string, write a function to check if it is a palindrome, | |
//according to the following restrictions: -lowercase and uppercase characters are considered equal -special characters are ignored | |
var aCode = "a".charCodeAt(0); | |
var ACode = "A".charCodeAt(0); | |
var setLen = 26; | |
function isChar(char) { | |
var code = char.charCodeAt(0); | |
return ((code >= aCode) && ((code - aCode) < setLen)) || | |
((code >= ACode) && ((code - ACode) < setLen)); | |
} | |
function normalizedCharCode(char) { | |
var code = char.charCodeAt(0); | |
if (!isChar(char)) { | |
return -1; | |
} | |
if ((code >= ACode) && ((code - ACode) < (setLen - 1))) { | |
return (code - ACode); | |
} | |
return code - aCode; | |
} | |
function isPalindrome(str) { | |
var start = 0; | |
var end = str.length - 1; | |
while (start < end) { | |
var startCode = normalizedCharCode(str[start]); | |
if (startCode === -1) { | |
start++; | |
continue; | |
} | |
var endCode = normalizedCharCode(str[end]); | |
if (endCode === -1) { | |
end--; | |
continue; | |
} | |
if (startCode !== endCode) { | |
return false; | |
} | |
start++; | |
end--; | |
} | |
return true; | |
} | |
console.log(isPalindrome("abc*(c/B;}{A")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment