[Swift]LeetCode467. 環繞字符串中惟一的子字符串 | Unique Substrings in Wraparound String

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-gvfjuzdk-kz.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", so s will look like this: "...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd....".git

Now we have another string p. Your job is to find out how many unique non-empty substrings of p are present in s. In particular, your input is the string p and you need to output the number of different non-empty substrings of p in the string s.github

Note: p consists of only lowercase English letters and the size of p might be over 10000.數組

Example 1:微信

Input: "a"
Output: 1

Explanation: Only the substring "a" of string "a" is in the string s. 

Example 2:ide

Input: "cac"
Output: 2
Explanation: There are two substrings "a", "c" of string "cac" in the string s. 

Example 3:函數

Input: "zab"
Output: 6
Explanation: There are six substrings "z", "a", "b", "za", "ab", "zab" of string "zab" in the string s.

把字符串 s 看做是「abcdefghijklmnopqrstuvwxyz」的無限環繞字符串,因此 s 看起來是這樣的:"...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd....". this

如今咱們有了另外一個字符串 p 。你須要的是找出 s 中有多少個惟一的 p的非空子串,尤爲是當你的輸入是字符串 p ,你須要輸出字符串 s 中 p的不一樣的非空子串的數目。 spa

注意: p 僅由小寫的英文字母組成,p 的大小可能超過 10000。 code

示例 1:

輸入: "a"
輸出: 1
解釋: 字符串 S 中只有一個"a"子字符。 

示例 2:

輸入: "cac"
輸出: 2
解釋: 字符串 S 中的字符串「cac」只有兩個子串「a」、「c」。 

示例 3:

輸入: "zab"
輸出: 6
解釋: 在字符串 S 中有六個子串「z」、「a」、「b」、「za」、「ab」、「zab」。

8236ms
 1 class Solution {
 2     func findSubstringInWraproundString(_ p: String) -> Int {
 3         var cnt:[Int] = [Int](repeating:0,count:26)
 4         var res:Int = 0
 5         var len:Int = 0
 6         for i in 0..<p.count
 7         {
 8             var cur:Int = p[i].ascii - 97
 9             if i > 0 && p[i - 1].ascii != (cur + 26 - 1) % 26 + 97
10             {
11                 len = 0
12             }
13             len += 1
14             if len > cnt[cur]
15             {
16                 res += len - cnt[cur]
17                 cnt[cur] = len
18             }
19             
20         }
21         return res
22     }
23 }
24 
25 extension String {        
26     //subscript函數能夠檢索數組中的值
27     //直接按照索引方式截取指定索引的字符
28     subscript (_ i: Int) -> Character {
29         //讀取字符
30         get {return self[index(startIndex, offsetBy: i)]}
31     }
32 }
33 
34 extension Character  
35 {  
36   //屬性:ASCII整數值(定義小寫爲整數值)
37    var ascii: Int {
38         get {
39             let s = String(self).unicodeScalars
40             return Int(s[s.startIndex].value)
41         }
42     }
43 }

Memory Usage: 4194kb

 1 class Solution {
 2     func findSubstringInWraproundString(_ p: String) -> Int {
 3         var count: [Character: Int] = [ : ]
 4         let s = Array("abcdefghijklmnopqrstuvwxyz")
 5         let p = Array(p)
 6         for c in s {
 7             count[c] = 0
 8         }
 9         
10         var maxLen = 0
11         
12         for i in 0 ..< p.count {
13             if i > 0 && (p[i].unicodeScalars.first!.value == p[i - 1].unicodeScalars.first!.value + 1 || p[i - 1].unicodeScalars.first!.value == p[i].unicodeScalars.first!.value + 25) {
14                 maxLen += 1
15             } else {
16                 maxLen = 1
17             }
18             count[p[i]] = max(count[p[i]]!, maxLen)
19         }
20         
21         return count.values.reduce(0, { $0 + $1 })
22     }
23 }
相關文章
相關標籤/搜索