返回介绍

solution / 0400-0499 / 0466.Count The Repetitions / README_EN

发布于 2024-06-17 01:04:00 字数 4817 浏览 0 评论 0 收藏 0

466. Count The Repetitions

中文文档

Description

We define str = [s, n] as the string str which consists of the string s concatenated n times.

  • For example, str == ["abc", 3] =="abcabcabc".

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, s1 = "abc" can be obtained from s2 = "abdbec" based on our definition by removing the bolded underlined characters.

You are given two strings s1 and s2 and two integers n1 and n2. You have the two strings str1 = [s1, n1] and str2 = [s2, n2].

Return _the maximum integer _m_ such that _str = [str2, m]_ can be obtained from _str1.

 

Example 1:

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

Example 2:

Input: s1 = "acb", n1 = 1, s2 = "acb", n2 = 1
Output: 1

 

Constraints:

  • 1 <= s1.length, s2.length <= 100
  • s1 and s2 consist of lowercase English letters.
  • 1 <= n1, n2 <= 106

Solutions

Solution 1

class Solution:
  def getMaxRepetitions(self, s1: str, n1: int, s2: str, n2: int) -> int:
    n = len(s2)
    d = {}
    for i in range(n):
      cnt = 0
      j = i
      for c in s1:
        if c == s2[j]:
          j += 1
        if j == n:
          cnt += 1
          j = 0
      d[i] = (cnt, j)

    ans = 0
    j = 0
    for _ in range(n1):
      cnt, j = d[j]
      ans += cnt
    return ans // n2
class Solution {
  public int getMaxRepetitions(String s1, int n1, String s2, int n2) {
    int m = s1.length(), n = s2.length();
    int[][] d = new int[n][0];
    for (int i = 0; i < n; ++i) {
      int j = i;
      int cnt = 0;
      for (int k = 0; k < m; ++k) {
        if (s1.charAt(k) == s2.charAt(j)) {
          if (++j == n) {
            j = 0;
            ++cnt;
          }
        }
      }
      d[i] = new int[] {cnt, j};
    }
    int ans = 0;
    for (int j = 0; n1 > 0; --n1) {
      ans += d[j][0];
      j = d[j][1];
    }
    return ans / n2;
  }
}
class Solution {
public:
  int getMaxRepetitions(string s1, int n1, string s2, int n2) {
    int m = s1.size(), n = s2.size();
    vector<pair<int, int>> d;
    for (int i = 0; i < n; ++i) {
      int j = i;
      int cnt = 0;
      for (int k = 0; k < m; ++k) {
        if (s1[k] == s2[j]) {
          if (++j == n) {
            ++cnt;
            j = 0;
          }
        }
      }
      d.emplace_back(cnt, j);
    }
    int ans = 0;
    for (int j = 0; n1; --n1) {
      ans += d[j].first;
      j = d[j].second;
    }
    return ans / n2;
  }
};
func getMaxRepetitions(s1 string, n1 int, s2 string, n2 int) (ans int) {
  n := len(s2)
  d := make([][2]int, n)
  for i := 0; i < n; i++ {
    j := i
    cnt := 0
    for k := range s1 {
      if s1[k] == s2[j] {
        j++
        if j == n {
          cnt++
          j = 0
        }
      }
    }
    d[i] = [2]int{cnt, j}
  }
  for j := 0; n1 > 0; n1-- {
    ans += d[j][0]
    j = d[j][1]
  }
  ans /= n2
  return
}
function getMaxRepetitions(s1: string, n1: number, s2: string, n2: number): number {
  const n = s2.length;
  const d: number[][] = new Array(n).fill(0).map(() => new Array(2).fill(0));
  for (let i = 0; i < n; ++i) {
    let j = i;
    let cnt = 0;
    for (const c of s1) {
      if (c === s2[j]) {
        if (++j === n) {
          j = 0;
          ++cnt;
        }
      }
    }
    d[i] = [cnt, j];
  }
  let ans = 0;
  for (let j = 0; n1 > 0; --n1) {
    ans += d[j][0];
    j = d[j][1];
  }
  return Math.floor(ans / n2);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文