Community

Start a Thread


Notifications
Subscribe You’re not receiving notifications from this thread.

Validate Palindrome

Here is the interview question prompt, presented for reference.

Given a string str, can you write a method that will return True if is a palindrome and False if it is not? If you'll recall, a palindrome is defined as "a word, phrase, or sequence that reads the same backward as forward". For now, assume that we will not have input strings that contain special characters or spaces, so the following examples hold:

let str = 'thisisnotapalindrome';
isPalindrome(str);
// false

str = 'racecar';
isPalindrome(str);
// true

java ```java String str = "thisisnotapalindrome"; boolean isPalindrome = isPalindrome(str); // false

str = "racecar"; isPalindrome = isPalindrome(str); // true ```

python ```python str = 'thisisnotapalindrome' ispalindrome = ispalindrome(str)

False

str = 'racecar' ispalindrome = ispalindrome(str)

True


cpp
```cpp
std::string str = "thisisnotapalindrome";
bool isPalindrome = isPalindrome(str);
// false

str = "racecar";
isPalindrome = isPalindrome(str);
// true

csharp ```csharp string str = "thisisnotapalindrome"; bool isPalindrome = IsPalindrome(str); // false

str = "racecar"; isPalindrome = IsPalindrome(str); // true ```

go ```go str := "thisisnotapalindrome" isPalindrome := IsPalindrome(str) // false

str = "racecar" isPalindrome = IsPalindrome(str) // true ```

For an extra challenge, try to ignore non-alphanumerical characters. The final solution that we present will handle all edge cases.

Constraints

  • Length of the given string <= 100000
  • The string will consist of ASCII characters (some or all)
  • Expected time complexity : O(n)
  • Expected space complexity : O(1)

You can see the full challenge with visuals at this link.

Challenges • Asked over 5 years ago by Jake from AlgoDaily

Christian Austria Commented on Jul 04, 2019:

I think for the solution you provided, we must account for the case of the character or letter as well as the whitespaces.

So here's my solution:

`

/* jshint esversion: 6 */

function isPalindrome(str) {

if (! isAlphaNumeric(str)) {

// return false;

}

const reverseStr = Array.from(removeSpaces(str))

.reverse()

.join('')

.toLowerCase();

return reverseStr === removeSpaces(str).toLowerCase();

}

function isAlphaNumeric(c) {

// create a regex pattern for alphanum

const reAlphaNumeric = /[a-z0-9]+$/i;

return reAlphaNumeric.test(c);

}

function removeSpaces(str) {

return str.split(' ').join('');

}

console.log(isPalindrome("A Santa Lived As a Devil At NASA"));

`

Christian Austria Commented on Jul 05, 2019:

I think for the solution you provided, we must account for the case of the character or letter as well as the whitespaces.

So here's my solution:

js
/* jshint esversion: 6 */

function isPalindrome(str) {
    if (!isAlphaNumeric(str)) {
        // return false;
    }

    const reverseStr = Array.from(removeSpaces(str)).reverse().join('').toLowerCase();

    return reverseStr === removeSpaces(str).toLowerCase();
}

function isAlphaNumeric(c) {
    // create a regex pattern for alphanum
    const reAlphaNumeric = /[a-z0-9]+$/i;
    return reAlphaNumeric.test(c);
}

function removeSpaces(str) {
    return str.split(' ').join('');
}

console.log(isPalindrome('A Santa Lived As a Devil At NASA'));
Anonymous Commented on Aug 27, 2019:

You say not to worry about spaces or special characters in your initial problem, but one of your test cases depends on handling spaces for it to pass.

Bryce Soghigian Commented on Dec 19, 2019:

//============JS Solution=========================//
function isPalindrome(str) {
// console.log(str,"str");
// console.log(str.split("").reverse().join(""),"split");
if(str.split("").reverse().join("") === str){
return true;
}else{
return false;
}
}

Anonymous Commented on Feb 14, 2020:

[deleted]