Skip to content

feat: add solutions to lc problem: No.3365 #3810

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,32 +89,114 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3365.Re

<!-- solution:start -->

### 方法一
### 方法一:哈希表

我们记字符串 $s$ 的长度为 $n$,那么每个子字符串的长度为 $m = n / k$。

用一个哈希表 $\textit{cnt}$ 记录每个长度为 $m$ 的子字符串在字符串 $s$ 中出现的次数与在字符串 $t$ 中出现的次数之差。

遍历字符串 $s$,每次取出长度为 $m$ 的子字符串,更新哈希表 $\textit{cnt}$。

最后判断哈希表 $\textit{cnt}$ 中的所有值是否都为 $0$。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。

<!-- tabs:start -->

#### 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<String, Integer> 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<string, int> 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<string, number> = {};
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);
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,32 +87,114 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3365.Re

<!-- solution:start -->

### 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$.

<!-- tabs:start -->

#### 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<String, Integer> 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<string, int> 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<string, number> = {};
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);
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public:
bool isPossibleToRearrange(string s, string t, int k) {
unordered_map<string, int> 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;
}
};
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public boolean isPossibleToRearrange(String s, String t, int k) {
Map<String, Integer> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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())
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function isPossibleToRearrange(s: string, t: string, k: number): boolean {
const cnt: Record<string, number> = {};
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);
}
Loading