Skip to content

Commit 31faf13

Browse files
committed
feat: Add solution for LeetCode problem 125
1 parent 74353b8 commit 31faf13

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

valid-palindrome/WhiteHyun.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// 125. Valid Palindrome.swift
3+
// https://leetcode.com/problems/valid-palindrome/description/
4+
// Algorithm
5+
//
6+
// Created by 홍승현 on 2024/04/26.
7+
//
8+
9+
import Foundation
10+
11+
final class LeetCode125 {
12+
func isPalindrome(_ s: String) -> Bool {
13+
if let regex = try? NSRegularExpression(pattern: "[a-zA-Z0-9]+") {
14+
let string = regex
15+
.matches(in: s, range: .init(location: 0, length: s.count))
16+
.map { s[Range($0.range, in: s)!].lowercased() }
17+
.joined()
18+
19+
return string == String(string.reversed())
20+
}
21+
22+
return false
23+
}
24+
25+
func isPalindromeUsingRegexString(_ s: String) -> Bool {
26+
var alphabets = ""
27+
for match in s.matches(of: /(?<alphabet>[a-zA-Z0-9]+)/) {
28+
alphabets += match.alphabet.lowercased()
29+
}
30+
return String(alphabets.reversed()) == alphabets
31+
}
32+
}

0 commit comments

Comments
 (0)