-
Notifications
You must be signed in to change notification settings - Fork 890
/
ValidPalindromeII.swift
40 lines (37 loc) · 1.16 KB
/
ValidPalindromeII.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* Question Link: https://leetcode.com/problems/valid-palindrome-ii/
* Primary idea: Two pointers. Move left and right when they are equal or cannot separate by moving either side, otherwise move one direction and update the flag.
*
* Time Complexity: O(n), Space Complexity: O(1)
*
*/
class ValidPalindromeII {
func validPalindrome(_ s: String) -> Bool {
var i = 0, j = s.count - 1, isDeleted = false
let s = Array(s)
while i < j {
if s[i] != s[j] {
if isDeleted {
return false
} else {
if s[i + 1] == s [j] && s[j - 1] == s[i] {
i += 1
j -= 1
} else if s[i + 1] == s[j] {
i += 1
isDeleted = true
} else if s[j - 1] == s[i] {
j -= 1
isDeleted = true
} else {
return false
}
}
} else {
i += 1
j -= 1
}
}
return true
}
}