返回介绍

solution / 2200-2299 / 2287.Rearrange Characters to Make Target String / README_EN

发布于 2024-06-17 01:03:07 字数 5646 浏览 0 评论 0 收藏 0

2287. Rearrange Characters to Make Target String

中文文档

Description

You are given two 0-indexed strings s and target. You can take some letters from s and rearrange them to form new strings.

Return_ the maximum number of copies of _target_ that can be formed by taking letters from _s_ and rearranging them._

 

Example 1:

Input: s = "ilovecodingonleetcode", target = "code"
Output: 2
Explanation:
For the first copy of "code", take the letters at indices 4, 5, 6, and 7.
For the second copy of "code", take the letters at indices 17, 18, 19, and 20.
The strings that are formed are "ecod" and "code" which can both be rearranged into "code".
We can make at most two copies of "code", so we return 2.

Example 2:

Input: s = "abcba", target = "abc"
Output: 1
Explanation:
We can make one copy of "abc" by taking the letters at indices 0, 1, and 2.
We can make at most one copy of "abc", so we return 1.
Note that while there is an extra 'a' and 'b' at indices 3 and 4, we cannot reuse the letter 'c' at index 2, so we cannot make a second copy of "abc".

Example 3:

Input: s = "abbaccaddaeea", target = "aaaaa"
Output: 1
Explanation:
We can make one copy of "aaaaa" by taking the letters at indices 0, 3, 6, 9, and 12.
We can make at most one copy of "aaaaa", so we return 1.

 

Constraints:

  • 1 <= s.length <= 100
  • 1 <= target.length <= 10
  • s and target consist of lowercase English letters.

Solutions

Solution 1

class Solution:
  def rearrangeCharacters(self, s: str, target: str) -> int:
    cnt1 = Counter(s)
    cnt2 = Counter(target)
    return min(cnt1[c] // v for c, v in cnt2.items())
class Solution {
  public int rearrangeCharacters(String s, String target) {
    int[] cnt1 = new int[26];
    int[] cnt2 = new int[26];
    for (int i = 0; i < s.length(); ++i) {
      ++cnt1[s.charAt(i) - 'a'];
    }
    for (int i = 0; i < target.length(); ++i) {
      ++cnt2[target.charAt(i) - 'a'];
    }
    int ans = 100;
    for (int i = 0; i < 26; ++i) {
      if (cnt2[i] > 0) {
        ans = Math.min(ans, cnt1[i] / cnt2[i]);
      }
    }
    return ans;
  }
}
class Solution {
public:
  int rearrangeCharacters(string s, string target) {
    int cnt1[26]{};
    int cnt2[26]{};
    for (char& c : s) {
      ++cnt1[c - 'a'];
    }
    for (char& c : target) {
      ++cnt2[c - 'a'];
    }
    int ans = 100;
    for (int i = 0; i < 26; ++i) {
      if (cnt2[i]) {
        ans = min(ans, cnt1[i] / cnt2[i]);
      }
    }
    return ans;
  }
};
func rearrangeCharacters(s string, target string) int {
  var cnt1, cnt2 [26]int
  for _, c := range s {
    cnt1[c-'a']++
  }
  for _, c := range target {
    cnt2[c-'a']++
  }
  ans := 100
  for i, v := range cnt2 {
    if v > 0 {
      ans = min(ans, cnt1[i]/v)
    }
  }
  return ans
}
function rearrangeCharacters(s: string, target: string): number {
  const idx = (s: string) => s.charCodeAt(0) - 97;
  const cnt1 = new Array(26).fill(0);
  const cnt2 = new Array(26).fill(0);
  for (const c of s) {
    ++cnt1[idx(c)];
  }
  for (const c of target) {
    ++cnt2[idx(c)];
  }
  let ans = 100;
  for (let i = 0; i < 26; ++i) {
    if (cnt2[i]) {
      ans = Math.min(ans, Math.floor(cnt1[i] / cnt2[i]));
    }
  }
  return ans;
}
impl Solution {
  pub fn rearrange_characters(s: String, target: String) -> i32 {
    let mut count1 = [0; 26];
    let mut count2 = [0; 26];
    for c in s.as_bytes() {
      count1[(c - b'a') as usize] += 1;
    }
    for c in target.as_bytes() {
      count2[(c - b'a') as usize] += 1;
    }
    let mut ans = i32::MAX;
    for i in 0..26 {
      if count2[i] != 0 {
        ans = ans.min(count1[i] / count2[i]);
      }
    }
    ans
  }
}
#define min(a, b) (((a) < (b)) ? (a) : (b))

int rearrangeCharacters(char* s, char* target) {
  int count1[26] = {0};
  int count2[26] = {0};
  for (int i = 0; s[i]; i++) {
    count1[s[i] - 'a']++;
  }
  for (int i = 0; target[i]; i++) {
    count2[target[i] - 'a']++;
  }
  int ans = INT_MAX;
  for (int i = 0; i < 26; i++) {
    if (count2[i]) {
      ans = min(ans, count1[i] / count2[i]);
    }
  }
  return ans;
}

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

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

发布评论

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