-
Notifications
You must be signed in to change notification settings - Fork 41
/
5.swift
73 lines (59 loc) · 1.7 KB
/
5.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
//
// LongestPalindromicSubstring.swift
// LeetCodeTests
//
// Created by Lex on 2018/6/28.
// Copyright © 2018 Lex Tang. 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 1:
//
// Input: "babad"
// Output: "bab"
// Note: "aba" is also a valid answer.
// Example 2:
//
// Input: "cbbd"
// Output: "bb"
import Foundation
import XCTest
// @seealso https://leetcode.com/problems/longest-palindromic-substring/discuss/2928/Very-simple-clean-java-solution
func longestPalindrome(_ s: String) -> String {
var lo = 0
var maxLen = 0
let len = s.count
if len < 2 {
return s
}
func extendPalindrome(_ s: String, l: Int, r: Int) {
var chars = s.utf8CString
var l = l
var r = r
while l >= 0 && r < s.count && chars[l] == chars[r] {
l -= 1
r += 1
}
if maxLen < r - l - 1 {
lo = l + 1
maxLen = r - l - 1
}
}
s.enumerated().forEach { i, c in
extendPalindrome(s, l: i, r: i)
extendPalindrome(s, l: i, r: i + 1)
}
let lIndex = s.index(s.startIndex, offsetBy: lo)
let rIndex = s.index(s.startIndex, offsetBy: lo + maxLen)
return String(s[lIndex..<rIndex])
}
class LongestPalindromicSubstringTest: XCTestCase {
func testLongestPalindromicSubstring() {
XCTAssertEqual(longestPalindrome("babad"), "bab")
XCTAssertEqual(longestPalindrome("cbbd"), "bb")
XCTAssertEqual(longestPalindrome("aaaaa"), "aaaaa")
XCTAssertEqual(longestPalindrome("aa"), "aa")
XCTAssertEqual(longestPalindrome("aaaa"), "aaaa")
}
}