(LeetCode) 125. Valid Palindrome

Valid Palindrome

  • Explore : Interview > Top Interveiw Questions > Easy Collection
  • 분류 : String
  • 난이도 : Easy

Problem

Given a string s, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

Example 1

1
2
3
Input: s = "A man, a plan, a canal: Panama"
Output: true
Explanation: "amanaplanacanalpanama" is a palindrome.

Example 2

1
2
3
Input: s = "race a car"
Output: false
Explanation: "raceacar" is not a palindrome.

Constraints

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

Solution

Exapnder
1
2
3
4
5
6
7
class Solution {
fun isPalindrome(s: String): Boolean {
var input = s.filter { it.isLetterOrDigit() }.toLowerCase()
var reverse = input.reversed()
return input == reverse
}
}

Point of Thinking

  • alphanumeric으로 필터링하고, 리버싱해서 비교하면 끝