FrontendChallenges is a collection of frontend interview questions and answers. It is designed to help you prepare for frontend interviews. It's free and open source.
Given a string s, return true if it is a palindrome, otherwise return false.
A palindrome is a string that reads the same forward and backward. It is also case-insensitive and ignores all non-alphanumeric characters.
Constraints:
1 ≤ s.length ≤ 1000
s is made up of only printable ASCII characters
Examples:
// Example 1:
const s1 = "Was it a car or a cat I saw?";
console.log(isPalindrome(s1));
// Output: true
// Explanation: After considering only alphanumerical characters we have "wasitacaroracatisaw", which is a palindrome.
// Example 2:
const s2 = "tab a cat";
console.log(isPalindrome(s2));
// Output: false
// Explanation: "tabacat" is not a palindrome.
Template
index.ts
export function isPalindrome(s: string): boolean {
}
index.test.ts
import { isPalindrome } from './index';
describe('isPalindrome', () => {
test('Example 1: Sentence palindrome', () => {
expect(isPalindrome("Was it a car or a cat I saw?")).toBe(true);
});
test('Example 2: Non-palindrome', () => {
expect(isPalindrome("tab a cat")).toBe(false);
});
test('Empty string', () => {
expect(isPalindrome("")).toBe(true);
});
test('Single character', () => {
expect(isPalindrome("a")).toBe(true);
});
test('Special characters and numbers', () => {
expect(isPalindrome("A man, a plan, a canal: Panama")).toBe(true);
});
test('Mixed case with numbers', () => {
expect(isPalindrome("Race a car")).toBe(false);
});
test('Only special characters', () => {
expect(isPalindrome(".,")).toBe(true);
});
});
Info
Question
Given a string
s
, returntrue
if it is a palindrome, otherwise returnfalse
.A palindrome is a string that reads the same forward and backward. It is also case-insensitive and ignores all non-alphanumeric characters.
Constraints:
Examples:
Template
index.ts
index.test.ts