Skip to content

Commit 47e6f66

Browse files
committed
A new solution for question No.125.
1 parent 0c64864 commit 47e6f66

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Easy/125.Valid Palindrome.playground/Contents.swift

+22
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,28 @@
2727
*/
2828
class Solution {
2929
func isPalindrome(_ s: String) -> Bool {
30+
var newStr = ""
31+
for c in s {
32+
if ("a"..."z").contains(c.lowercased()) {
33+
newStr.append(c.lowercased())
34+
} else if ("0"..."9").contains(c) {
35+
newStr.append(c)
36+
}
37+
}
38+
if newStr.isEmpty { return true }
39+
var left = newStr.startIndex
40+
var right = newStr.index(before: newStr.endIndex)
41+
while left < right {
42+
if newStr[left] != newStr[right] {
43+
return false
44+
}
45+
left = newStr.index(after: left)
46+
right = newStr.index(before: right)
47+
}
48+
return true
49+
}
50+
51+
func isPalindrome2(_ s: String) -> Bool {
3052
var newStr = ""
3153
for c in s {
3254
if ("a"..."z").contains(c.lowercased()) {

0 commit comments

Comments
 (0)