File tree 1 file changed +32
-0
lines changed
1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments