-
Notifications
You must be signed in to change notification settings - Fork 20
/
LongestPalindromicSubstring.swift
68 lines (61 loc) · 1.83 KB
/
LongestPalindromicSubstring.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
//
// LongestPalindromicSubstring.swift
// LeetCode
//
// Created by 黎赵太郎 on 16/12/2017.
// Copyright © 2017 lizhaotailang. All rights reserved.
//
// Given a string s, find the longest palindromic substring in s.
// You may assume that the maximum length of s is 1000.
//
// Example:
//
// Input: "babad"
//
// Output: "bab"
//
// Note: "aba" is also a valid answer.
// Example:
//
// Input: "cbbd"
//
// Output: "bb"
//
// Accepted. See [LongestPalindromicSubstringTest](./LeetCodeTests/LongestPalindromicSubstringTests.swfit) for test cases.
//
import Foundation
class LongestPalindromicSubstring {
func longestPalindrome(_ s: String) -> String {
let array = Array(s)
if array.count <= 1 {
return s
}
var maxLength = 0
var startIndex = 0
for index in array.indices {
var leftIndex = index
var rightIndex = index
while leftIndex >= 0 && rightIndex < array.count && array[leftIndex] == array[rightIndex] {
let current = rightIndex - leftIndex + 1
if current > maxLength {
maxLength = current
startIndex = leftIndex
}
leftIndex -= 1
rightIndex += 1
}
leftIndex = index
rightIndex = index + 1
while leftIndex >= 0 && rightIndex < array.count && array[leftIndex] == array[rightIndex] {
let current = rightIndex - leftIndex + 1
if current > maxLength {
maxLength = current
startIndex = leftIndex
}
leftIndex -= 1
rightIndex += 1
}
}
return String(array[startIndex..<startIndex + maxLength])
}
}