Class Solution
-
- All Implemented Interfaces:
public final class Solution3365 - Rearrange K Substrings to Form Target String.
Medium
You are given two strings
sandt, both of which are anagrams of each other, and an integerk.Your task is to determine whether it is possible to split the string
sintokequal-sized substrings, rearrange the substrings, and concatenate them in any order to create a new string that matches the given stringt.Return
trueif this is possible, otherwise, returnfalse.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
sinto 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
sinto 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
sinto 2 substrings of length 3:["aab", "bcc"].These substrings cannot be rearranged to form
t = "bbaacc", so the output isfalse.
Constraints:
<code>1 <= s.length == t.length <= 2 * 10<sup>5</sup></code>
1 <= k <= s.lengths.lengthis divisible byk.sandtconsist only of lowercase English letters.The input is generated such that
sandtare anagrams of each other.