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.program main
implicit none
character(len=*), parameter :: s = "ilovecodingonleetcode "
character(len=*), parameter :: target = "code "
integer :: n
n = maxCopies(s, target)
write (*,*) n
contains
function maxCopies(s, target) result(n)
implicit none
character(len=*), intent(in) :: s
character(len=*), intent(in) :: target
integer :: n
integer :: i, j, k, l
n = 0
do i = 1, len(s)-len(target)+1
do j = i+len(target)-1, len(s)
if (s(i:j) == target) then
n = max(n, j-i+1)
end if
end do
end do
end function maxCopies
end program main
5
program maxCopies
implicit none
integer :: i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z
integer :: max_copies
character(len=100) :: s
character(len=10) :: target
read(*,*) s
read(*,*) target
max_copies = 0
do i = 1, len(s) - len(target) + 1
do j = i + 1, len(s) - len(target) + 1
if (s(i:j) == target) then
max_copies = max_copies + 1
end if
end do
end do
write(*,*) max_copies
end program maxCopies
temp.f95:6:29: 6 | character(len=100) :: s | 1 Error: Symbol βsβ at (1) already has basic type of INTEGER temp.f95:15:20: 15 | if (s(i:j) == target) then | 1 Error: Syntax error in IF-expression at (1) temp.f95:17:17: 17 | end if | 1 Error: Expecting END DO statement at (1) temp.f95:14:28: 14 | do j = i + 1, len(s) - len(target) + 1 | 1 Error: βstringβ argument of βlenβ intrinsic at (1) must be CHARACTER temp.f95:13:20: 13 | do i = 1, len(s) - len(target) + 1 | 1 Error: βstringβ argument of βlenβ intrinsic at (1) must be CHARACTER
def maxNumberOfCopies(s, target):
s_counts = [0] * 26
target_counts = [0] * 26
for c in s:
s_counts[ord(c) - ord('a')] += 1
for c in target:
target_counts[ord(c) - ord('a')] += 1
max_copies = float('inf')
for i in range(26):
if target_counts[i] > 0:
max_copies = min(max_copies, s_counts[i] // target_counts[i])
return max_copies
1. Create two arrays `s_counts` and `target_counts` of length 26 to count the occurrences of each alphabet in both strings s and target.
max_copies
to store the maximum number of copies that can be formed.target_counts
from 0 to 25 (inclusive) and check if the count of each character is greater than 0. If yes, update max_copies
with the minimum value between the current max_copies
value and the integer division of s_counts
and target_counts
at that index.max_copies
as the result.#include <string>
#include <vector>
int maxNumberOfCopies(std::string s, std::string target) {
std::vector<int> s_counts(26, 0);
std::vector<int> target_counts(26, 0);
for (char c : s)
s_counts[c - 'a']++;
for (char c : target)
target_counts[c - 'a']++;
int max_copies = INT32_MAX;
for (int i = 0; i < 26; i++) {
if (target_counts[i] > 0)
max_copies = std::min(max_copies, s_counts[i] / target_counts[i]);
}
return max_copies;
}
1. Create two arrays `s_counts` and `target_counts` of length 26 to count the occurrences of each alphabet in both strings s and target.
max_copies
to store the maximum number of copies that can be formed.target_counts
from 0 to 25 (inclusive) and check if the count of each character is greater than 0. If yes, update max_copies
with the minimum value between the current max_copies
value and the integer division of s_counts
and target_counts
at that index.max_copies
as the result.