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)
str = 'racecar' ispalindrome = ispalindrome(str)
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.
100000
O(n)
O(1)
You can see the full challenge with visuals at this link.
Challenges • Asked over 5 years ago by Jake from AlgoDaily
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"));
`
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'));
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.
//============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;
}
}
[deleted]