jsartisan / frontend-challenges

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.
https://frontend-challenges.com
43 stars 5 forks source link

Is Palindrome #203

Open jsartisan opened 4 hours ago

jsartisan commented 4 hours ago

Info

difficulty: easy
title: Is Palindrome
type: question
template: typescript
tags: javascript, blind75, string, two-pointers

Question

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:

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);
  });
});
github-actions[bot] commented 4 hours ago

204 - Pull Request updated.