Skip to content

Instantly share code, notes, and snippets.

@akaila
Last active August 11, 2021 01:35
Show Gist options
  • Save akaila/ac3a5515e8bbe50f351a to your computer and use it in GitHub Desktop.
Save akaila/ac3a5515e8bbe50f351a to your computer and use it in GitHub Desktop.
//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