Rearrange Characters to Make Target String

🏠 ⬅️ ➑️

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.

Note: This problem is from LeetCode.
βœ… Compiled
βœ… Executed
❌ Correct
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
❌ Compiled
❌ Executed
❌ Correct
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
🌐 Data from online sources
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.
  1. Iterate through the input strings s and target, and calculate the frequency of each character in both strings s and target using their ASCII values.
  2. Initialize a variable max_copies to store the maximum number of copies that can be formed.
  3. Iterate through the 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.
  4. Return the max_copies as the result.
🌐 Data from online sources
#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.
  1. Iterate through the input strings s and target, and calculate the frequency of each character in both strings s and target using their ASCII values.
  2. Initialize a variable max_copies to store the maximum number of copies that can be formed.
  3. Iterate through the 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.
  4. Return the max_copies as the result.