[Swift]LeetCode466. 统计重复个数 | Count The Repetitions

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

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”.

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”。

另一方面,如果我们可以从 s2 中删除某些字符使其变为 s1,我们称字符串 s1 可以从字符串 s2 获得。例如,“abc” 可以根据我们的定义从 “abdbec” 获得,但不能从 “acbbe” 获得。

现在给出两个非空字符串 S1 和 S2(每个最多 100 个字符长)和两个整数 0 ≤ N1 ≤ 106 和 1 ≤ N2 ≤ 106。现在考虑字符串 S1 和 S2,其中S1=[s1,n1]S2=[s2,n2]。找出可以使[S2,M]从 S1 获得的最大整数 M。

示例:

输入:
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 }

 

上一篇:CH5702 Count The Repetitions[倍增dp]


下一篇:Joint CTC/attention decoding for end-to-end speech recognition