[Swift]LeetCode466. 統計重複個數 | Count The Repetitions

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

Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc", 3]="abcabcabc".git

On the other hand, we define that string s1 can be obtained from string s2 if we can remove some characters from s2 such that it becomes s1. For example, 「abc」 can be obtained from 「abdbec」 based on our definition, but it can not be obtained from 「acbbe」.github

You are given two non-empty strings s1 and s2 (each at most 100 characters long) and two integers 0 ≤ n1 ≤ 106 and 1 ≤ n2 ≤ 106. Now consider the strings S1 and S2, where S1=[s1,n1] and S2=[s2,n2]. Find the maximum integer M such that [S2,M] can be obtained from S1.數組

Example:微信

Input:
s1="acb", n1=4
s2="ab", n2=2

Return:
2

定義由 n 個鏈接的字符串 s 組成字符串 S,即 S = [s,n]。例如,["abc", 3]=「abcabcabc」。app

另外一方面,若是咱們能夠從 s2 中刪除某些字符使其變爲 s1,咱們稱字符串 s1 能夠從字符串 s2 得到。例如,「abc」 能夠根據咱們的定義從 「abdbec」 得到,但不能從 「acbbe」 得到。ide

如今給出兩個非空字符串 S1 和 S2(每一個最多 100 個字符長)和兩個整數 0 ≤ N1 ≤ 106 和 1 ≤ N2 ≤ 106。如今考慮字符串 S1 和 S2,其中S1=[s1,n1]S2=[s2,n2]。找出能夠使[S2,M]從 S1 得到的最大整數 M。函數

示例:spa

輸入:
s1 ="acb",n1 = 4
s2 ="ab",n2 = 2

返回:
2

132ms
 1 class Solution {
 2     func getMaxRepetitions(_ s1: String, _ n1: Int, _ s2: String, _ n2: Int) -> Int {
 3         var repeatCnt:[Int] = [Int](repeating:0,count:n1 + 1)
 4         var nextIdx:[Int] = [Int](repeating:0,count:n1 + 1)
 5         var j:Int = 0
 6         var cnt:Int = 0
 7         if n1 == 0 {return 0}
 8         for k in 1...n1
 9         {
10             for i in 0..<s1.count
11             {
12                 if s1[i] == s2[j]
13                 {
14                     j += 1
15                     if j == s2.count
16                     {
17                         j = 0
18                         cnt += 1
19                     }
20                 }
21             }
22             repeatCnt[k] = cnt
23             nextIdx[k] = j
24             for start in 0..<k
25             {
26                 if nextIdx[start] == j
27                 {
28                     var interval:Int = k - start
29                     var rep:Int = (n1 - start) / interval
30                     var patternCnt:Int = (repeatCnt[k] - repeatCnt[start]) * rep
31                     var remainCnt:Int = repeatCnt[start + (n1 - start) % interval]
32                     return (patternCnt + remainCnt) / n2
33                 }
34             }
35         }
36         return repeatCnt[n1] / n2
37     }
38     
39     func getArray(_ str:String) -> [Character]
40     {
41         var arr:[Character] = [Character]()
42         for char in str.characters
43         {
44             arr.append(char)
45         }
46         return arr
47     }
48 }
49 
50 extension String {        
51     //subscript函數能夠檢索數組中的值
52     //直接按照索引方式截取指定索引的字符
53     subscript (_ i: Int) -> Character {
54         //讀取字符
55         get {return self[index(startIndex, offsetBy: i)]}
56     }
57 }
相關文章
相關標籤/搜索