★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:爲敢(WeiGanTechnologies)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-tlexolpr-kh.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Example 1:git
Input: "abab"
Output: "bab" Explanation: The substrings are ["a", "ab", "aba", "abab", "b", "ba", "bab"]. The lexicographically maximum substring is "bab".
Example 2:github
Input: "leetcode"
Output: "tcode"
Note:微信
1 <= s.length <= 10^5
給你一個字符串 s
,找出它的全部子串並按字典序排列,返回排在最後的那個子串。app
示例 1:spa
輸入:"abab" 輸出:"bab" 解釋:咱們能夠找出 7 個子串 ["a", "ab", "aba", "abab", "b", "ba", "bab"]。按字典序排在最後的子串是 "bab"。
示例 2:code
輸入:"leetcode" 輸出:"tcode"
提示:htm
1 <= s.length <= 10^5
Runtime: 168 msblog
1 class Solution { 2 func lastSubstring(_ s: String) -> String { 3 let arrS:[Character] = Array(s) 4 var i:Int = 0 5 let len:Int = s.count 6 for j in 1..<len 7 { 8 var sz:Int = 0 9 while(j + sz < len) 10 { 11 if arrS[i + sz] == arrS[j + sz] 12 { 13 sz += 1 14 continue 15 } 16 i = arrS[j + sz] > arrS[i + sz] ? j : i 17 break 18 } 19 if j + sz == len 20 { 21 break 22 } 23 } 24 return s.subString(i) 25 } 26 } 27 28 extension String { 29 // 截取字符串:從index到結束處 30 // - Parameter index: 開始索引 31 // - Returns: 子字符串 32 func subString(_ index: Int) -> String { 33 let theIndex = self.index(self.endIndex, offsetBy: index - self.count) 34 return String(self[theIndex..<endIndex]) 35 } 36 }
1 class Solution { 2 func lastSubstring(_ s: String) -> String { 3 4 let chars = Array(s) 5 var highest: Character = "A" 6 var idxs = [Int]() 7 var distinct = 0 8 for i in chars.indices { 9 if chars[i] > highest { 10 highest = chars[i] 11 idxs = [Int]() 12 distinct += 1 13 } 14 15 if chars[i] == highest { 16 idxs.append(i) 17 } else { 18 distinct += 1 19 } 20 } 21 22 if distinct == 1 { 23 return s 24 } 25 26 var shift = 1; 27 28 var nextLevel = [Int]() 29 30 while idxs.count > 1 { 31 var shiftHighest: Character = "A" 32 for i in idxs { 33 if i + shift < chars.count { 34 if chars[i+shift] > shiftHighest { 35 shiftHighest = chars[i+shift] 36 nextLevel = [Int]() 37 } 38 if chars[i+shift] == shiftHighest { 39 nextLevel.append(i) 40 } 41 } 42 } 43 idxs = nextLevel 44 nextLevel.removeAll() 45 shift += 1 46 } 47 return String(chars[idxs[0]..<chars.count]) 48 } 49 }
476ms索引
1 class Solution { 2 func lastSubstring(_ s: String) -> String { 3 4 var ret = s[s.startIndex...] 5 6 for i in s.indices.dropFirst() { 7 ret = max(ret, s[i...]) 8 } 9 10 return String(ret) 11 } 12 }
1772ms
1 class Solution { 2 func lastSubstring(_ s: String) -> String { 3 var maxCharacter = Character(Unicode.Scalar(0)) 4 var maxIndex = 0 5 6 for (index, character) in s.enumerated() { 7 if character > maxCharacter { 8 maxCharacter = character 9 maxIndex = index 10 } 11 else if character == maxCharacter, 12 String(s[String.Index(encodedOffset: index)...]) > String(s[String.Index(encodedOffset: maxIndex)...]) { 13 maxCharacter = character 14 maxIndex = index 15 } 16 } 17 18 return String(s[String.Index(encodedOffset: maxIndex)...]) 19 } 20 }