Skip to content

Commit b2b8c23

Browse files
committed
Longest palindromic substring.
1 parent 535e0f0 commit b2b8c23

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
Given a string s, return the longest palindromic substring in s.
3+
4+
 
5+
6+
Example 1:
7+
Input: s = "babad"
8+
Output: "bab"
9+
Explanation: "aba" is also a valid answer.
10+
11+
Example 2:
12+
Input: s = "cbbd"
13+
Output: "bb"
14+
 
15+
16+
Constraints:
17+
- 1 <= s.length <= 1000
18+
- s consist of only digits and English letters.
19+
*/
20+
class Solution {
21+
private var leftPosition = 0
22+
private var rightPosition = 0
23+
private var maxLength = 0
24+
func longestPalindrome(_ s: String) -> String {
25+
let characterArr = s.map { $0 }
26+
for i in 0..<s.count {
27+
var index1 = i
28+
var index2 = i
29+
findPalindrome(characterArr, leftPosition: &index1, rightPosition: &index2)
30+
var index3 = i
31+
var index4 = i + 1
32+
findPalindrome(characterArr, leftPosition: &index3, rightPosition: &index4)
33+
}
34+
return String(s[s.index(s.startIndex, offsetBy: leftPosition)...s.index(s.startIndex, offsetBy: rightPosition)])
35+
}
36+
37+
private func findPalindrome(_ s: [Character], leftPosition left: inout Int, rightPosition right: inout Int) {
38+
let n = s.count
39+
while left >= 0 && right < n && s[left] == s[right] {
40+
if right - left + 1 > maxLength {
41+
leftPosition = left
42+
rightPosition = right
43+
maxLength = right - left + 1
44+
}
45+
left -= 1
46+
right += 1
47+
}
48+
}
49+
}
50+
51+
let s = Solution()
52+
let r = s.longestPalindrome(
53+
"lphbehiapswjudnbcossedgioawodnwdruaaxhbkwrxyzaxygmnjgwysafuqbmtzwdkihbwkrjefrsgjbrycembzzlwhxneiijgzidhngbmxwkhphoctpilgooitqbpjxhwrekiqupmlcvawaiposqttsdgzcsjqrmlgyvkkipfigttahljdhtksrozehkzgshekeaxezrswvtinyouomqybqsrtegwwqhqivgnyehpzrhgzckpnnpvajqevbzeksvbezoqygjtdouecnhpjibmsgmcqcgxwzlzztdneahixxhwwuehfsiqghgeunpxgvavqbqrelnvhnnyqnjqfysfltclzeoaletjfzskzvcdwhlbmwbdkxnyqappjzwlowslwcbbmcxoiqkjaqqwvkybimebapkorhfdzntodhpbhgmsspgkbetmgkqlolsltpulgsmyapgjeswazvhxedqsypejwuzlvegtusjcsoenrcmypexkjxyduohlvkhwbrtzjnarusbouwamazzejhnetfqbidalfomecehfmzqkhndpkxinzkpxvhwargbwvaeqbzdhxzmmeeozxxtzpylohvdwoqocvutcelgdsnmubyeeeufdaoznxpvdiwnkjliqtgcmvhilndcdelpnilszzerdcuokyhcxjuedjielvngarsgxkemvhlzuprywlypxeezaxoqfges")
54+
print(r)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Medium/5.Longest Palindromic Substring.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,4 @@
107107

108108
1. [Min Stack](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/155.Min%20Stack.playground/Contents.swift)
109109
2. [Longest Substring Without Repeating Characters](https://github.com/recherst/leetcode-algtorithm/blob/main/Medium/3.Longest%20Substring%20Without%20Repeating%20Characters.playground/Contents.swift)
110+
3. [Longest Palindromic Substring](https://github.com/recherst/leetcode-algtorithm/blob/main/Medium/5.Longest%20Palindromic%20Substring.playground/Contents.swift)

0 commit comments

Comments
 (0)