forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValidPalindromeII.swift
40 lines (34 loc) · 1016 Bytes
/
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: Take advantage of validPalindrome, and jump left and right separately to get correct character should be deleted
*
* Time Complexity: O(n), Space Complexity: O(n)
*
*/
class ValidPalindromeII {
func validPalindrome(_ s: String) -> Bool {
var i = 0, j = s.count - 1
let sChars = Array(s)
while i < j {
if sChars[i] != sChars[j] {
return isPalindrome(sChars, i + 1, j) || isPalindrome(sChars, i, j - 1)
} else {
i += 1
j -= 1
}
}
return true
}
fileprivate func isPalindrome(_ s: [Character], _ i: Int, _ j: Int) -> Bool {
var i = i, j = j
while i < j {
if s[i] != s[j] {
return false
} else {
i += 1
j -= 1
}
}
return true
}
}