3365. Rearrange K Substrings to Form Target String LeetCode Solution
In this guide, you will get 3365. Rearrange K Substrings to Form Target String LeetCode Solution with the best time and space complexity. The solution to Rearrange K Substrings to Form Target String problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Rearrange K Substrings to Form Target String solution in C++
- Rearrange K Substrings to Form Target String solution in Java
- Rearrange K Substrings to Form Target String solution in Python
- Additional Resources
Problem Statement of Rearrange K Substrings to Form Target String
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 matches t.
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 matches t.
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 is false.
Constraints:
1 <= s.length == t.length <= 2 * 105
1 <= k <= s.length
s.length is divisible by k.
s and t consist only of lowercase English letters.
The input is generated such that s and t are anagrams of each other.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
3365. Rearrange K Substrings to Form Target String LeetCode Solution in C++
class Solution {
public:
bool isPossibleToRearrange(string s, string t, int k) {
const int n = s.length();
return getCount(s, n / k) == getCount(t, n / k);
}
private:
unordered_map<string, int> getCount(const string& s, int sz) {
unordered_map<string, int> count;
for (int i = 0; i < s.length(); i += sz)
++count[s.substr(i, sz)];
return count;
};
};
/* code provided by PROGIEZ */
3365. Rearrange K Substrings to Form Target String LeetCode Solution in Java
class Solution {
public boolean isPossibleToRearrange(String s, String t, int k) {
final int n = s.length();
return getCount(s, n / k).equals(getCount(t, n / k));
}
private Map<String, Integer> getCount(final String s, int sz) {
Map<String, Integer> count = new HashMap<>();
for (int i = 0; i < s.length(); i += sz)
count.merge(s.substring(i, i + sz), 1, Integer::sum);
return count;
}
}
// code provided by PROGIEZ
3365. Rearrange K Substrings to Form Target String LeetCode Solution in Python
class Solution:
def isPossibleToRearrange(self, s: str, t: str, k: int) -> bool:
n = len(s)
return (collections.Counter(s[i:i + n // k] for i in range(0, n, n // k)) ==
collections.Counter(t[i:i + n // k] for i in range(0, n, n // k)))
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.