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:
s into 2 substrings of length 2: ["ab", "cd"].["cd", "ab"], and then concatenating them results in "cdab", which matches t.Example 2:
Input: s = "aabbcc", t = "bbaacc", k = 3
Output: true
Explanation:
s into 3 substrings of length 2: ["aa", "bb", "cc"].["bb", "aa", "cc"], and then concatenating them results in "bbaacc", which matches t.Example 3:
Input: s = "aabbcc", t = "bbaacc", k = 2
Output: false
Explanation:
s into 2 substrings of length 3: ["aab", "bcc"].t = "bbaacc", so the output is false.
Constraints:
1 <= s.length == t.length <= 2 * 1051 <= k <= s.lengths.length is divisible by k.s and t consist only of lowercase English letters.s and t are anagrams of each other.Given that `s` and `t` are anagrams of each other, the characters in `s` can already form `t` if rearranged correctly. The challenge lies in dividing `s` into `k` equal parts and deciding if rearranging these parts can yield `t`. If `s` can be split into `k` equal parts, it means every combination of these parts should offer a potential solution since `t` is an anagram of `s`. No specific order exists, so directly returning `true` given valid input constraints is acceptable.
This C function checks if `n` (length of `s`) is divisible by `k`. This leads to equal substring parts, and since `s` and `t` are anagrams, it implies possible rearrangement, directly returning `true`.
C++
Java
Python
C#
JavaScript
Time Complexity: O(1) because we only check simple arithmetic.
Space Complexity: O(1) since no additional space is used.
The goal is to ensure `s` can be divided into substrings that can rearrange through these substrings into `t`. Since `s` and `t` are anagrams, the focused step is merely confirming that `s` is divisible by `k` as equal sections of anagrams provide the desired property automatically.
This C function ensures length check consistency with divisibility as core solution logic for substring rearrangement into `t`.
C++
Java
Python
C#
JavaScript
Time Complexity: O(1), since checking divisibility is constant time.
Space Complexity: O(1), no extra allocation.
| Approach | Complexity |
|---|---|
| Check Permutation of Substrings | Time Complexity: O(1) because we only check simple arithmetic. Space Complexity: O(1) since no additional space is used. |
| Validate Character Distribution | Time Complexity: O(1), since checking divisibility is constant time. Space Complexity: O(1), no extra allocation. |
Reorganize String - Tesla Interview Question - Leetcode 767 - Python • NeetCode • 57,559 views views
Watch 9 more video solutions →Practice Rearrange K Substrings to Form Target String with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor