FastPrepValid Palindrome

Valid Palindrome

Meta logoMeta● EasyFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Given a string s, ignore every character other than ASCII letters and digits, and compare letters without regard to case.

Return true if the remaining characters read identically from left to right and right to left. Otherwise, return false. A string with no remaining characters is a palindrome.

Function

isPalindrome(s: String) → boolean

Examples

Example 1

s = "A man, a plan, a canal: Panama"return = true

Ignoring spaces, punctuation and case leaves amanaplanacanalpanama, which reads the same in both directions.

Example 2

s = "race a car"return = false

The retained string is raceacar; its fourth character from the left is e, while its fourth character from the right is a.

Example 3

s = " "return = true

Removing the space leaves no characters, so the result is true.

Example 4

s = "0P"return = false

Digits are retained. After lowercasing, 0p is not a palindrome.

Constraints

  • 1 <= s.length <= 2 * 10^5.
  • s contains only printable ASCII characters.

More Meta problems

See Meta hiring insights
public boolean isPalindrome(String s) {
    // Write your code here.
}
s"A man, a plan, a canal: Panama"
expectedtrue
Checking account…