3365. Rearrange K Substrings to Form Target String
Description
You are given two strings s
and t
, both of which are anagrams of each other, and an integer k
.
Your task is to determine whether it is possible to split the string s
into k
equal-sized substrings, rearrange the substrings, and concatenate them in any order to create a new string that matches the given string t
.
Return true
if this is possible, otherwise, return false
.
An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, using all the original letters exactly once.
A substring is a contiguous non-empty sequence of characters within a string.
Example 1:
Input: s = "abcd", t = "cdab", k = 2
Output: true
Explanation:
- Split
s
into 2 substrings of length 2:["ab", "cd"]
. - Rearranging these substrings as
["cd", "ab"]
, and then concatenating them results in"cdab"
, which matchest
.
Example 2:
Input: s = "aabbcc", t = "bbaacc", k = 3
Output: true
Explanation:
- Split
s
into 3 substrings of length 2:["aa", "bb", "cc"]
. - Rearranging these substrings as
["bb", "aa", "cc"]
, and then concatenating them results in"bbaacc"
, which matchest
.
Example 3:
Input: s = "aabbcc", t = "bbaacc", k = 2
Output: false
Explanation:
- Split
s
into 2 substrings of length 3:["aab", "bcc"]
. - These substrings cannot be rearranged to form
t = "bbaacc"
, so the output isfalse
.
Constraints:
1 <= s.length == t.length <= 2 * 105
1 <= k <= s.length
s.length
is divisible byk
.s
andt
consist only of lowercase English letters.- The input is generated such that
s
andt
are anagrams of each other.
Solutions
Solution 1: Hash Table
Let the length of the string $s$ be $n$, then the length of each substring is $m = n / k$.
We use a hash table $\textit{cnt}$ to record the difference between the number of occurrences of each substring of length $m$ in string $s$ and in string $t$.
We traverse the string $s$, extracting substrings of length $m$ each time, and update the hash table $\textit{cnt}$.
Finally, we check whether all values in the hash table $\textit{cnt}$ are $0$.
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string $s$.
1 2 3 4 5 6 7 8 9 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
|