Maximising Repetitions through Iterative Subsequence Matching
Intuition\nInitially, the problem seems straightforward - repeat strings s1 and s2 for n1 and n2 times respectively, and then find how many times s2 can be obta
# Intuition\nInitially, the problem seems straightforward - repeat strings `s1` and `s2` for `n1` and `n2` times respectively, and then find how many times `s2` can be obtained from `s1`. However, the challenge is to efficiently handle cases where parts of `s2` may be formed within a single repetition of `s1`, as well as cases where `s2` can be formed across the boundary of `s1`\'s repetitions.\n\n# Approach\nTo solve this, we iterate through each character of `s1`, repeated `n1` times, and check if it matches the current character of `s2`. If it does, we move to the next character in `s2`. Once we reach the end of `s2`, it means one repetition of `s2` is completed within `s1`, and we reset our position in `s2` to the start. We repeat this process until we\'ve gone through `s1` `n1` times. The number of times we complete `s2` gives us the total count of `s2` repetitions. Finally, we divide this count by `n2` to get the maximum integer `m`.\n\n# Complexity\n- Time complexity: $$O(n1 \\times |s1|)$$, where $$|s1|$$ is the length of the string `s1`. This is because we iterate through each character of `s1` for `n1` times.\n- Space complexity: $$O(|s1| + |s2|)$$, as we convert `s1` and `s2` into arrays for easier access to their characters.\n\n# Code\n```swift\nclass Solution {\n    func getMaxRepetitions(_ s1: String, _ n1: Int, _ s2: String, _ n2: Int) -> Int {\n        let s1Array = Array(s1)\n        let s2Array = Array(s2)\n        var indexS2 = 0\n        var countS2 = 0\n\n        for _ in 0..<n1 {\n            for char in s1Array {\n                if char == s2Array[indexS2] {\n                    indexS2 += 1\n                    if indexS2 == s2Array.count {\n                        indexS2 = 0\n                        countS2 += 1\n                    }\n                }\n            }\n        }\n\n        return countS2 / n2\n    }\n}\n```
['Swift']