diff --git a/solution/3300-3399/3365.Rearrange K Substrings to Form Target String/README.md b/solution/3300-3399/3365.Rearrange K Substrings to Form Target String/README.md index 328d3bf743c54..7eaeb18825e7d 100644 --- a/solution/3300-3399/3365.Rearrange K Substrings to Form Target String/README.md +++ b/solution/3300-3399/3365.Rearrange K Substrings to Form Target String/README.md @@ -89,32 +89,114 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3365.Re -### 方法一 +### 方法一:哈希表 + +我们记字符串 $s$ 的长度为 $n$,那么每个子字符串的长度为 $m = n / k$。 + +用一个哈希表 $\textit{cnt}$ 记录每个长度为 $m$ 的子字符串在字符串 $s$ 中出现的次数与在字符串 $t$ 中出现的次数之差。 + +遍历字符串 $s$,每次取出长度为 $m$ 的子字符串,更新哈希表 $\textit{cnt}$。 + +最后判断哈希表 $\textit{cnt}$ 中的所有值是否都为 $0$。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。 #### Python3 ```python - +class Solution: + def isPossibleToRearrange(self, s: str, t: str, k: int) -> bool: + cnt = Counter() + n = len(s) + m = n // k + for i in range(0, n, m): + cnt[s[i : i + m]] += 1 + cnt[t[i : i + m]] -= 1 + return all(v == 0 for v in cnt.values()) ``` #### Java ```java - +class Solution { + public boolean isPossibleToRearrange(String s, String t, int k) { + Map cnt = new HashMap<>(k); + int n = s.length(); + int m = n / k; + for (int i = 0; i < n; i += m) { + cnt.merge(s.substring(i, i + m), 1, Integer::sum); + cnt.merge(t.substring(i, i + m), -1, Integer::sum); + } + for (int v : cnt.values()) { + if (v != 0) { + return false; + } + } + return true; + } +} ``` #### C++ ```cpp - +class Solution { +public: + bool isPossibleToRearrange(string s, string t, int k) { + unordered_map cnt; + int n = s.size(); + int m = n / k; + for (int i = 0; i < n; i += m) { + cnt[s.substr(i, m)]++; + cnt[t.substr(i, m)]--; + } + for (auto& [_, v] : cnt) { + if (v) { + return false; + } + } + return true; + } +}; ``` #### Go ```go +func isPossibleToRearrange(s string, t string, k int) bool { + n := len(s) + m := n / k + cnt := map[string]int{} + for i := 0; i < n; i += m { + cnt[s[i:i+m]]++ + cnt[t[i:i+m]]-- + } + for _, v := range cnt { + if v != 0 { + return false + } + } + return true +} +``` +#### TypeScript + +```ts +function isPossibleToRearrange(s: string, t: string, k: number): boolean { + const cnt: Record = {}; + const n = s.length; + const m = Math.floor(n / k); + for (let i = 0; i < n; i += m) { + const a = s.slice(i, i + m); + cnt[a] = (cnt[a] || 0) + 1; + const b = t.slice(i, i + m); + cnt[b] = (cnt[b] || 0) - 1; + } + return Object.values(cnt).every(x => x === 0); +} ``` diff --git a/solution/3300-3399/3365.Rearrange K Substrings to Form Target String/README_EN.md b/solution/3300-3399/3365.Rearrange K Substrings to Form Target String/README_EN.md index e69f34da176ab..b8b9fbd1ff794 100644 --- a/solution/3300-3399/3365.Rearrange K Substrings to Form Target String/README_EN.md +++ b/solution/3300-3399/3365.Rearrange K Substrings to Form Target String/README_EN.md @@ -87,32 +87,114 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3365.Re -### Solution 1 +### 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$. #### Python3 ```python - +class Solution: + def isPossibleToRearrange(self, s: str, t: str, k: int) -> bool: + cnt = Counter() + n = len(s) + m = n // k + for i in range(0, n, m): + cnt[s[i : i + m]] += 1 + cnt[t[i : i + m]] -= 1 + return all(v == 0 for v in cnt.values()) ``` #### Java ```java - +class Solution { + public boolean isPossibleToRearrange(String s, String t, int k) { + Map cnt = new HashMap<>(k); + int n = s.length(); + int m = n / k; + for (int i = 0; i < n; i += m) { + cnt.merge(s.substring(i, i + m), 1, Integer::sum); + cnt.merge(t.substring(i, i + m), -1, Integer::sum); + } + for (int v : cnt.values()) { + if (v != 0) { + return false; + } + } + return true; + } +} ``` #### C++ ```cpp - +class Solution { +public: + bool isPossibleToRearrange(string s, string t, int k) { + unordered_map cnt; + int n = s.size(); + int m = n / k; + for (int i = 0; i < n; i += m) { + cnt[s.substr(i, m)]++; + cnt[t.substr(i, m)]--; + } + for (auto& [_, v] : cnt) { + if (v) { + return false; + } + } + return true; + } +}; ``` #### Go ```go +func isPossibleToRearrange(s string, t string, k int) bool { + n := len(s) + m := n / k + cnt := map[string]int{} + for i := 0; i < n; i += m { + cnt[s[i:i+m]]++ + cnt[t[i:i+m]]-- + } + for _, v := range cnt { + if v != 0 { + return false + } + } + return true +} +``` +#### TypeScript + +```ts +function isPossibleToRearrange(s: string, t: string, k: number): boolean { + const cnt: Record = {}; + const n = s.length; + const m = Math.floor(n / k); + for (let i = 0; i < n; i += m) { + const a = s.slice(i, i + m); + cnt[a] = (cnt[a] || 0) + 1; + const b = t.slice(i, i + m); + cnt[b] = (cnt[b] || 0) - 1; + } + return Object.values(cnt).every(x => x === 0); +} ``` diff --git a/solution/3300-3399/3365.Rearrange K Substrings to Form Target String/Solution.cpp b/solution/3300-3399/3365.Rearrange K Substrings to Form Target String/Solution.cpp new file mode 100644 index 0000000000000..ad4556f3c04a3 --- /dev/null +++ b/solution/3300-3399/3365.Rearrange K Substrings to Form Target String/Solution.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + bool isPossibleToRearrange(string s, string t, int k) { + unordered_map cnt; + int n = s.size(); + int m = n / k; + for (int i = 0; i < n; i += m) { + cnt[s.substr(i, m)]++; + cnt[t.substr(i, m)]--; + } + for (auto& [_, v] : cnt) { + if (v) { + return false; + } + } + return true; + } +}; diff --git a/solution/3300-3399/3365.Rearrange K Substrings to Form Target String/Solution.go b/solution/3300-3399/3365.Rearrange K Substrings to Form Target String/Solution.go new file mode 100644 index 0000000000000..11af16e188385 --- /dev/null +++ b/solution/3300-3399/3365.Rearrange K Substrings to Form Target String/Solution.go @@ -0,0 +1,15 @@ +func isPossibleToRearrange(s string, t string, k int) bool { + n := len(s) + m := n / k + cnt := map[string]int{} + for i := 0; i < n; i += m { + cnt[s[i:i+m]]++ + cnt[t[i:i+m]]-- + } + for _, v := range cnt { + if v != 0 { + return false + } + } + return true +} diff --git a/solution/3300-3399/3365.Rearrange K Substrings to Form Target String/Solution.java b/solution/3300-3399/3365.Rearrange K Substrings to Form Target String/Solution.java new file mode 100644 index 0000000000000..b76cde27882ae --- /dev/null +++ b/solution/3300-3399/3365.Rearrange K Substrings to Form Target String/Solution.java @@ -0,0 +1,17 @@ +class Solution { + public boolean isPossibleToRearrange(String s, String t, int k) { + Map cnt = new HashMap<>(k); + int n = s.length(); + int m = n / k; + for (int i = 0; i < n; i += m) { + cnt.merge(s.substring(i, i + m), 1, Integer::sum); + cnt.merge(t.substring(i, i + m), -1, Integer::sum); + } + for (int v : cnt.values()) { + if (v != 0) { + return false; + } + } + return true; + } +} diff --git a/solution/3300-3399/3365.Rearrange K Substrings to Form Target String/Solution.py b/solution/3300-3399/3365.Rearrange K Substrings to Form Target String/Solution.py new file mode 100644 index 0000000000000..e1e43807006af --- /dev/null +++ b/solution/3300-3399/3365.Rearrange K Substrings to Form Target String/Solution.py @@ -0,0 +1,9 @@ +class Solution: + def isPossibleToRearrange(self, s: str, t: str, k: int) -> bool: + cnt = Counter() + n = len(s) + m = n // k + for i in range(0, n, m): + cnt[s[i : i + m]] += 1 + cnt[t[i : i + m]] -= 1 + return all(v == 0 for v in cnt.values()) diff --git a/solution/3300-3399/3365.Rearrange K Substrings to Form Target String/Solution.ts b/solution/3300-3399/3365.Rearrange K Substrings to Form Target String/Solution.ts new file mode 100644 index 0000000000000..714212b1edefa --- /dev/null +++ b/solution/3300-3399/3365.Rearrange K Substrings to Form Target String/Solution.ts @@ -0,0 +1,12 @@ +function isPossibleToRearrange(s: string, t: string, k: number): boolean { + const cnt: Record = {}; + const n = s.length; + const m = Math.floor(n / k); + for (let i = 0; i < n; i += m) { + const a = s.slice(i, i + m); + cnt[a] = (cnt[a] || 0) + 1; + const b = t.slice(i, i + m); + cnt[b] = (cnt[b] || 0) - 1; + } + return Object.values(cnt).every(x => x === 0); +}