Input: s ="ilovecodingonleetcode", target ="code"Output: 2Explanation:
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 return2.
Input: s ="abcba", target ="abc"Output: 1Explanation:
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 return1.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".
Input: s ="abbaccaddaeea", target ="aaaaa"Output: 1Explanation:
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 return1.
Count the frequency of each letter in s and in target. The answer is the minimum number of times we can use the letters in s to form target, i.e., for each letter in target, how many times it appears in s divided by how many times it is needed.
classSolution {
public:int rearrangeCharacters(string s, string target) {
vector<int> cntS(26), cntT(26);
for (char c : s) cntS[c -'a']++;
for (char c : target) cntT[c -'a']++;
int ans = INT_MAX;
for (int i =0; i <26; ++i) {
if (cntT[i]) ans = min(ans, cntS[i] / cntT[i]);
}
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
funcrearrangeCharacters(s, targetstring) int {
cntS, cntT:= [26]int{}, [26]int{}
for_, c:=ranges { cntS[c-'a']++ }
for_, c:=rangetarget { cntT[c-'a']++ }
ans:=1<<30fori:=0; i < 26; i++ {
ifcntT[i] > 0 {
ifcntS[i]/cntT[i] < ans {
ans = cntS[i]/cntT[i]
}
}
}
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution {
publicintrearrangeCharacters(String s, String target) {
int[] cntS =newint[26], cntT =newint[26];
for (char c : s.toCharArray()) cntS[c -'a']++;
for (char c : target.toCharArray()) cntT[c -'a']++;
int ans = Integer.MAX_VALUE;
for (int i = 0; i < 26; ++i) {
if (cntT[i]> 0) ans = Math.min(ans, cntS[i]/ cntT[i]);
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution {
funrearrangeCharacters(s: String, target: String): Int {
val cntS = IntArray(26)
val cntT = IntArray(26)
for (c in s) cntS[c - 'a']++for (c in target) cntT[c - 'a']++var ans = Int.MAX_VALUE
for (i in0 until 26) {
if (cntT[i] > 0) ans = minOf(ans, cntS[i] / cntT[i])
}
return ans
}
}
1
2
3
4
5
6
classSolution:
defrearrangeCharacters(self, s: str, target: str) -> int:
from collections import Counter
cntS = Counter(s)
cntT = Counter(target)
return min(cntS[c] // cntT[c] for c in cntT)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
use std::cmp::min;
impl Solution {
pubfnrearrange_characters(s: String, target: String) -> i32 {
letmut cnt_s = [0; 26];
letmut cnt_t = [0; 26];
for c in s.chars() { cnt_s[(c asu8-b'a') asusize] +=1; }
for c in target.chars() { cnt_t[(c asu8-b'a') asusize] +=1; }
letmut ans =i32::MAX;
for i in0..26 {
if cnt_t[i] >0 {
ans = min(ans, cnt_s[i] / cnt_t[i]);
}
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution {
rearrangeCharacters(s: string, target: string):number {
constcntS= Array(26).fill(0), cntT= Array(26).fill(0);
for (constcofs) cntS[c.charCodeAt(0) -97]++;
for (constcoftarget) cntT[c.charCodeAt(0) -97]++;
letans= Number.MAX_SAFE_INTEGER;
for (leti=0; i<26; ++i) {
if (cntT[i] >0) ans= Math.min(ans, Math.floor(cntS[i] /cntT[i]));
}
returnans;
}
}