/
Contents.swift
93 lines (60 loc) · 2.31 KB
/
Contents.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import UIKit
/*
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
e.g.1
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
e.g.2
Input: "cbbd"
Output: "bb"
Clarifying questions:
. when checking, is it case sensitive
. is the input lower case
*/
func longestPalindrome(from s: String) -> Substring {
// window frame approach
// start from s.startIndex,s.endIndex
// check the characters at each end, if they're not equal, shift the startIndex up
// if it can't find, loop again and shift the endIndex down.
var start = s.startIndex
var end = s.index(before: s.endIndex)
var palindrome: Substring = ""
while start <= end {
// print("character at start: \(s[start]). character at end: \(s[end])")
if s[start] == s[end] {
print("EQUAL: character at start: \(s[start]). character at end: \(s[end])")
if start == end {
palindrome.insert(s[start], at: palindrome.index(palindrome.startIndex, offsetBy: palindrome.count / 2))
return palindrome
}
palindrome.insert(s[start], at: palindrome.index(palindrome.startIndex, offsetBy: palindrome.count / 2))
palindrome.insert(s[end], at: palindrome.index(palindrome.startIndex, offsetBy: palindrome.count / 2))
print(palindrome)
start = s.index(after: start)
end = s.index(before: end)
} else {
print("NOT EQUAL: character at start: \(s[start]). character at end: \(s[end])")
start = s.index(after: start)
palindrome.removeAll()
}
}
return ""
}
longestPalindrome(from: "babbab")
func isPalindrome(word: String) -> Bool {
var startNum = 0
var endNum = word.count - 1
while startNum <= (word.count / 2) && endNum >= (word.count / 2) {
let startInd = word.index(word.startIndex, offsetBy: startNum)
let endInd = word.index(word.startIndex, offsetBy: endNum)
if word[startInd] == word[endInd] {
startNum += 1
endNum -= 1
} else {
return false
}
}
return true
}
isPalindrome(word: "rinnir")