分析
给你两个下标从 0 开始的字符串 s 和 target 。你可以从 s 取出一些字符并将其重排,得到若干新的字符串。
从 s 中取出字符并重新排列,返回可以形成 target 的 最大 副本数。
class Solution {
public int rearrangeCharacters(String s, String target) {
int[] from = new int[26];
int[] to = new int[26];
for (char ch : s.toCharArray()) {
int index = ch - 'a';
from[index] ++;
}
for (char ch : target.toCharArray()) {
int index = ch - 'a';
to[index]++;
}
int ans = Integer.MAX_VALUE;
for (int i = 0; i < 26; i++) {
if (to[i] == 0) {
continue;
}
ans = Math.min(ans,from[i] / to[i]);
}
return ans;
}
}