Skip to content

feat: add solutions to lc problem: No.3138 #3174

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
Jun 29, 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 @@ -67,32 +67,170 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:枚举

根据题目描述,字符串 $\text{t}$ 的长度一定是 $\text{s}$ 的长度的因子,我们可以从小到大枚举字符串 $\text{t}$ 的长度 $k$,然后检查是否满足题目要求,如果满足则返回。因此,问题转化为如何检查字符串 $\text{t}$ 的长度 $k$ 是否满足题目要求。

我们首先统计字符串 $\text{s}$ 中每个字符出现的次数,记录在数组或哈希表 $\text{cnt}$ 中。

然后,我们定义一个函数 $\text{check}(k)$,用来检查字符串 $\text{t}$ 的长度 $k$ 是否满足题目要求。我们可以遍历字符串 $\text{s}$,每次取长度为 $k$ 的子串,然后统计每个字符出现的次数,如果每个字符出现的次数乘以 $\frac{n}{k}$ 不等于 $\text{cnt}$ 中的值,则返回 $\text{false}$。遍历结束,如果都满足,则返回 $\text{true}$。

时间复杂度 $O(n \times A)$,其中 $n$ 是字符串 $\text{s}$ 的长度,而 $A$ 是 $n$ 的因子个数。空间复杂度 $O(|\Sigma|)$,其中 $\Sigma$ 是字符集,本题中为小写字母集合。

<!-- tabs:start -->

#### Python3

```python

class Solution:
def minAnagramLength(self, s: str) -> int:
def check(k: int) -> bool:
for i in range(0, n, k):
cnt1 = Counter(s[i : i + k])
for c, v in cnt.items():
if cnt1[c] * (n // k) != v:
return False
return True

cnt = Counter(s)
n = len(s)
for i in range(1, n + 1):
if n % i == 0 and check(i):
return i
```

#### Java

```java

class Solution {
private int n;
private char[] s;
private int[] cnt = new int[26];

public int minAnagramLength(String s) {
n = s.length();
this.s = s.toCharArray();
for (int i = 0; i < n; ++i) {
++cnt[this.s[i] - 'a'];
}
for (int i = 1;; ++i) {
if (n % i == 0 && check(i)) {
return i;
}
}
}

private boolean check(int k) {
for (int i = 0; i < n; i += k) {
int[] cnt1 = new int[26];
for (int j = i; j < i + k; ++j) {
++cnt1[s[j] - 'a'];
}
for (int j = 0; j < 26; ++j) {
if (cnt1[j] * (n / k) != cnt[j]) {
return false;
}
}
}
return true;
}
}
```

#### C++

```cpp

class Solution {
public:
int minAnagramLength(string s) {
int n = s.size();
int cnt[26]{};
for (char c : s) {
cnt[c - 'a']++;
}
auto check = [&](int k) {
for (int i = 0; i < n; i += k) {
int cnt1[26]{};
for (int j = i; j < i + k; ++j) {
cnt1[s[j] - 'a']++;
}
for (int j = 0; j < 26; ++j) {
if (cnt1[j] * (n / k) != cnt[j]) {
return false;
}
}
}
return true;
};
for (int i = 1;; ++i) {
if (n % i == 0 && check(i)) {
return i;
}
}
}
};
```

#### Go

```go
func minAnagramLength(s string) int {
n := len(s)
cnt := [26]int{}
for _, c := range s {
cnt[c-'a']++
}
check := func(k int) bool {
for i := 0; i < n; i += k {
cnt1 := [26]int{}
for j := i; j < i+k; j++ {
cnt1[s[j]-'a']++
}
for j, v := range cnt {
if cnt1[j]*(n/k) != v {
return false
}
}
}
return true
}
for i := 1; ; i++ {
if n%i == 0 && check(i) {
return i
}
}
}
```

#### TypeScript

```ts
function minAnagramLength(s: string): number {
const n = s.length;
const cnt: Record<string, number> = {};
for (let i = 0; i < n; i++) {
cnt[s[i]] = (cnt[s[i]] || 0) + 1;
}
const check = (k: number): boolean => {
for (let i = 0; i < n; i += k) {
const cnt1: Record<string, number> = {};
for (let j = i; j < i + k; j++) {
cnt1[s[j]] = (cnt1[s[j]] || 0) + 1;
}
for (const [c, v] of Object.entries(cnt)) {
if (cnt1[c] * ((n / k) | 0) !== v) {
return false;
}
}
}
return true;
};
for (let i = 1; ; ++i) {
if (n % i === 0 && check(i)) {
return i;
}
}
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,32 +65,170 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Enumeration

Based on the problem description, the length of string $\text{t}$ must be a factor of the length of string $\text{s}$. We can enumerate the length $k$ of string $\text{t}$ from small to large, and then check if it meets the requirements of the problem. If it does, we return. Thus, the problem is transformed into how to check whether the length $k$ of string $\text{t}$ meets the requirements.

First, we count the occurrence of each character in string $\text{s}$ and record it in an array or hash table $\text{cnt}$.

Next, we define a function $\text{check}(k)$ to check whether the length $k$ of string $\text{t}$ meets the requirements. We can traverse string $\text{s}$, taking a substring of length $k$ each time, and then count the occurrence of each character. If the occurrence of each character multiplied by $\frac{n}{k}$ does not equal the value in $\text{cnt}$, then return $\text{false}$. If all checks pass by the end of the traversal, return $\text{true}$.

The time complexity is $O(n \times A)$, where $n$ is the length of string $\text{s}$, and $A$ is the number of factors of $n$. The space complexity is $O(|\Sigma|)$, where $\Sigma$ is the character set, which in this case is the set of lowercase letters.

<!-- tabs:start -->

#### Python3

```python

class Solution:
def minAnagramLength(self, s: str) -> int:
def check(k: int) -> bool:
for i in range(0, n, k):
cnt1 = Counter(s[i : i + k])
for c, v in cnt.items():
if cnt1[c] * (n // k) != v:
return False
return True

cnt = Counter(s)
n = len(s)
for i in range(1, n + 1):
if n % i == 0 and check(i):
return i
```

#### Java

```java

class Solution {
private int n;
private char[] s;
private int[] cnt = new int[26];

public int minAnagramLength(String s) {
n = s.length();
this.s = s.toCharArray();
for (int i = 0; i < n; ++i) {
++cnt[this.s[i] - 'a'];
}
for (int i = 1;; ++i) {
if (n % i == 0 && check(i)) {
return i;
}
}
}

private boolean check(int k) {
for (int i = 0; i < n; i += k) {
int[] cnt1 = new int[26];
for (int j = i; j < i + k; ++j) {
++cnt1[s[j] - 'a'];
}
for (int j = 0; j < 26; ++j) {
if (cnt1[j] * (n / k) != cnt[j]) {
return false;
}
}
}
return true;
}
}
```

#### C++

```cpp

class Solution {
public:
int minAnagramLength(string s) {
int n = s.size();
int cnt[26]{};
for (char c : s) {
cnt[c - 'a']++;
}
auto check = [&](int k) {
for (int i = 0; i < n; i += k) {
int cnt1[26]{};
for (int j = i; j < i + k; ++j) {
cnt1[s[j] - 'a']++;
}
for (int j = 0; j < 26; ++j) {
if (cnt1[j] * (n / k) != cnt[j]) {
return false;
}
}
}
return true;
};
for (int i = 1;; ++i) {
if (n % i == 0 && check(i)) {
return i;
}
}
}
};
```

#### Go

```go
func minAnagramLength(s string) int {
n := len(s)
cnt := [26]int{}
for _, c := range s {
cnt[c-'a']++
}
check := func(k int) bool {
for i := 0; i < n; i += k {
cnt1 := [26]int{}
for j := i; j < i+k; j++ {
cnt1[s[j]-'a']++
}
for j, v := range cnt {
if cnt1[j]*(n/k) != v {
return false
}
}
}
return true
}
for i := 1; ; i++ {
if n%i == 0 && check(i) {
return i
}
}
}
```

#### TypeScript

```ts
function minAnagramLength(s: string): number {
const n = s.length;
const cnt: Record<string, number> = {};
for (let i = 0; i < n; i++) {
cnt[s[i]] = (cnt[s[i]] || 0) + 1;
}
const check = (k: number): boolean => {
for (let i = 0; i < n; i += k) {
const cnt1: Record<string, number> = {};
for (let j = i; j < i + k; j++) {
cnt1[s[j]] = (cnt1[s[j]] || 0) + 1;
}
for (const [c, v] of Object.entries(cnt)) {
if (cnt1[c] * ((n / k) | 0) !== v) {
return false;
}
}
}
return true;
};
for (let i = 1; ; ++i) {
if (n % i === 0 && check(i)) {
return i;
}
}
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
public:
int minAnagramLength(string s) {
int n = s.size();
int cnt[26]{};
for (char c : s) {
cnt[c - 'a']++;
}
auto check = [&](int k) {
for (int i = 0; i < n; i += k) {
int cnt1[26]{};
for (int j = i; j < i + k; ++j) {
cnt1[s[j] - 'a']++;
}
for (int j = 0; j < 26; ++j) {
if (cnt1[j] * (n / k) != cnt[j]) {
return false;
}
}
}
return true;
};
for (int i = 1;; ++i) {
if (n % i == 0 && check(i)) {
return i;
}
}
}
};
Loading
Loading