Skip to content

Commit a1dc981

Browse files
committed
feat: add solutions to lc problem: No.3138
No.3138. Minimum Length of Anagram Concatenation
1 parent dba96c4 commit a1dc981

File tree

7 files changed

+413
-8
lines changed

7 files changed

+413
-8
lines changed

solution/3100-3199/3138.Minimum Length of Anagram Concatenation/README.md

Lines changed: 142 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,32 +67,170 @@ tags:
6767

6868
<!-- solution:start -->
6969

70-
### 方法一
70+
### 方法一:枚举
71+
72+
根据题目描述,字符串 $\text{t}$ 的长度一定是 $\text{s}$ 的长度的因子,我们可以从小到大枚举字符串 $\text{t}$ 的长度 $k$,然后检查是否满足题目要求,如果满足则返回。因此,问题转化为如何检查字符串 $\text{t}$ 的长度 $k$ 是否满足题目要求。
73+
74+
我们首先统计字符串 $\text{s}$ 中每个字符出现的次数,记录在数组或哈希表 $\text{cnt}$ 中。
75+
76+
然后,我们定义一个函数 $\text{check}(k)$,用来检查字符串 $\text{t}$ 的长度 $k$ 是否满足题目要求。我们可以遍历字符串 $\text{s}$,每次取长度为 $k$ 的子串,然后统计每个字符出现的次数,如果每个字符出现的次数乘以 $\frac{n}{k}$ 不等于 $\text{cnt}$ 中的值,则返回 $\text{false}$。遍历结束,如果都满足,则返回 $\text{true}$。
77+
78+
时间复杂度 $O(n \times A)$,其中 $n$ 是字符串 $\text{s}$ 的长度,而 $A$ 是 $n$ 的因子个数。空间复杂度 $O(|\Sigma|)$,其中 $\Sigma$ 是字符集,本题中为小写字母集合。
7179

7280
<!-- tabs:start -->
7381

7482
#### Python3
7583

7684
```python
77-
85+
class Solution:
86+
def minAnagramLength(self, s: str) -> int:
87+
def check(k: int) -> bool:
88+
for i in range(0, n, k):
89+
cnt1 = Counter(s[i : i + k])
90+
for c, v in cnt.items():
91+
if cnt1[c] * (n // k) != v:
92+
return False
93+
return True
94+
95+
cnt = Counter(s)
96+
n = len(s)
97+
for i in range(1, n + 1):
98+
if n % i == 0 and check(i):
99+
return i
78100
```
79101

80102
#### Java
81103

82104
```java
83-
105+
class Solution {
106+
private int n;
107+
private char[] s;
108+
private int[] cnt = new int[26];
109+
110+
public int minAnagramLength(String s) {
111+
n = s.length();
112+
this.s = s.toCharArray();
113+
for (int i = 0; i < n; ++i) {
114+
++cnt[this.s[i] - 'a'];
115+
}
116+
for (int i = 1;; ++i) {
117+
if (n % i == 0 && check(i)) {
118+
return i;
119+
}
120+
}
121+
}
122+
123+
private boolean check(int k) {
124+
for (int i = 0; i < n; i += k) {
125+
int[] cnt1 = new int[26];
126+
for (int j = i; j < i + k; ++j) {
127+
++cnt1[s[j] - 'a'];
128+
}
129+
for (int j = 0; j < 26; ++j) {
130+
if (cnt1[j] * (n / k) != cnt[j]) {
131+
return false;
132+
}
133+
}
134+
}
135+
return true;
136+
}
137+
}
84138
```
85139

86140
#### C++
87141

88142
```cpp
89-
143+
class Solution {
144+
public:
145+
int minAnagramLength(string s) {
146+
int n = s.size();
147+
int cnt[26]{};
148+
for (char c : s) {
149+
cnt[c - 'a']++;
150+
}
151+
auto check = [&](int k) {
152+
for (int i = 0; i < n; i += k) {
153+
int cnt1[26]{};
154+
for (int j = i; j < i + k; ++j) {
155+
cnt1[s[j] - 'a']++;
156+
}
157+
for (int j = 0; j < 26; ++j) {
158+
if (cnt1[j] * (n / k) != cnt[j]) {
159+
return false;
160+
}
161+
}
162+
}
163+
return true;
164+
};
165+
for (int i = 1;; ++i) {
166+
if (n % i == 0 && check(i)) {
167+
return i;
168+
}
169+
}
170+
}
171+
};
90172
```
91173
92174
#### Go
93175
94176
```go
177+
func minAnagramLength(s string) int {
178+
n := len(s)
179+
cnt := [26]int{}
180+
for _, c := range s {
181+
cnt[c-'a']++
182+
}
183+
check := func(k int) bool {
184+
for i := 0; i < n; i += k {
185+
cnt1 := [26]int{}
186+
for j := i; j < i+k; j++ {
187+
cnt1[s[j]-'a']++
188+
}
189+
for j, v := range cnt {
190+
if cnt1[j]*(n/k) != v {
191+
return false
192+
}
193+
}
194+
}
195+
return true
196+
}
197+
for i := 1; ; i++ {
198+
if n%i == 0 && check(i) {
199+
return i
200+
}
201+
}
202+
}
203+
```
95204

205+
#### TypeScript
206+
207+
```ts
208+
function minAnagramLength(s: string): number {
209+
const n = s.length;
210+
const cnt: Record<string, number> = {};
211+
for (let i = 0; i < n; i++) {
212+
cnt[s[i]] = (cnt[s[i]] || 0) + 1;
213+
}
214+
const check = (k: number): boolean => {
215+
for (let i = 0; i < n; i += k) {
216+
const cnt1: Record<string, number> = {};
217+
for (let j = i; j < i + k; j++) {
218+
cnt1[s[j]] = (cnt1[s[j]] || 0) + 1;
219+
}
220+
for (const [c, v] of Object.entries(cnt)) {
221+
if (cnt1[c] * ((n / k) | 0) !== v) {
222+
return false;
223+
}
224+
}
225+
}
226+
return true;
227+
};
228+
for (let i = 1; ; ++i) {
229+
if (n % i === 0 && check(i)) {
230+
return i;
231+
}
232+
}
233+
}
96234
```
97235

98236
<!-- tabs:end -->

solution/3100-3199/3138.Minimum Length of Anagram Concatenation/README_EN.md

Lines changed: 142 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,32 +65,170 @@ tags:
6565

6666
<!-- solution:start -->
6767

68-
### Solution 1
68+
### Solution 1: Enumeration
69+
70+
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.
71+
72+
First, we count the occurrence of each character in string $\text{s}$ and record it in an array or hash table $\text{cnt}$.
73+
74+
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}$.
75+
76+
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.
6977

7078
<!-- tabs:start -->
7179

7280
#### Python3
7381

7482
```python
75-
83+
class Solution:
84+
def minAnagramLength(self, s: str) -> int:
85+
def check(k: int) -> bool:
86+
for i in range(0, n, k):
87+
cnt1 = Counter(s[i : i + k])
88+
for c, v in cnt.items():
89+
if cnt1[c] * (n // k) != v:
90+
return False
91+
return True
92+
93+
cnt = Counter(s)
94+
n = len(s)
95+
for i in range(1, n + 1):
96+
if n % i == 0 and check(i):
97+
return i
7698
```
7799

78100
#### Java
79101

80102
```java
81-
103+
class Solution {
104+
private int n;
105+
private char[] s;
106+
private int[] cnt = new int[26];
107+
108+
public int minAnagramLength(String s) {
109+
n = s.length();
110+
this.s = s.toCharArray();
111+
for (int i = 0; i < n; ++i) {
112+
++cnt[this.s[i] - 'a'];
113+
}
114+
for (int i = 1;; ++i) {
115+
if (n % i == 0 && check(i)) {
116+
return i;
117+
}
118+
}
119+
}
120+
121+
private boolean check(int k) {
122+
for (int i = 0; i < n; i += k) {
123+
int[] cnt1 = new int[26];
124+
for (int j = i; j < i + k; ++j) {
125+
++cnt1[s[j] - 'a'];
126+
}
127+
for (int j = 0; j < 26; ++j) {
128+
if (cnt1[j] * (n / k) != cnt[j]) {
129+
return false;
130+
}
131+
}
132+
}
133+
return true;
134+
}
135+
}
82136
```
83137

84138
#### C++
85139

86140
```cpp
87-
141+
class Solution {
142+
public:
143+
int minAnagramLength(string s) {
144+
int n = s.size();
145+
int cnt[26]{};
146+
for (char c : s) {
147+
cnt[c - 'a']++;
148+
}
149+
auto check = [&](int k) {
150+
for (int i = 0; i < n; i += k) {
151+
int cnt1[26]{};
152+
for (int j = i; j < i + k; ++j) {
153+
cnt1[s[j] - 'a']++;
154+
}
155+
for (int j = 0; j < 26; ++j) {
156+
if (cnt1[j] * (n / k) != cnt[j]) {
157+
return false;
158+
}
159+
}
160+
}
161+
return true;
162+
};
163+
for (int i = 1;; ++i) {
164+
if (n % i == 0 && check(i)) {
165+
return i;
166+
}
167+
}
168+
}
169+
};
88170
```
89171
90172
#### Go
91173
92174
```go
175+
func minAnagramLength(s string) int {
176+
n := len(s)
177+
cnt := [26]int{}
178+
for _, c := range s {
179+
cnt[c-'a']++
180+
}
181+
check := func(k int) bool {
182+
for i := 0; i < n; i += k {
183+
cnt1 := [26]int{}
184+
for j := i; j < i+k; j++ {
185+
cnt1[s[j]-'a']++
186+
}
187+
for j, v := range cnt {
188+
if cnt1[j]*(n/k) != v {
189+
return false
190+
}
191+
}
192+
}
193+
return true
194+
}
195+
for i := 1; ; i++ {
196+
if n%i == 0 && check(i) {
197+
return i
198+
}
199+
}
200+
}
201+
```
93202

203+
#### TypeScript
204+
205+
```ts
206+
function minAnagramLength(s: string): number {
207+
const n = s.length;
208+
const cnt: Record<string, number> = {};
209+
for (let i = 0; i < n; i++) {
210+
cnt[s[i]] = (cnt[s[i]] || 0) + 1;
211+
}
212+
const check = (k: number): boolean => {
213+
for (let i = 0; i < n; i += k) {
214+
const cnt1: Record<string, number> = {};
215+
for (let j = i; j < i + k; j++) {
216+
cnt1[s[j]] = (cnt1[s[j]] || 0) + 1;
217+
}
218+
for (const [c, v] of Object.entries(cnt)) {
219+
if (cnt1[c] * ((n / k) | 0) !== v) {
220+
return false;
221+
}
222+
}
223+
}
224+
return true;
225+
};
226+
for (let i = 1; ; ++i) {
227+
if (n % i === 0 && check(i)) {
228+
return i;
229+
}
230+
}
231+
}
94232
```
95233

96234
<!-- tabs:end -->
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
int minAnagramLength(string s) {
4+
int n = s.size();
5+
int cnt[26]{};
6+
for (char c : s) {
7+
cnt[c - 'a']++;
8+
}
9+
auto check = [&](int k) {
10+
for (int i = 0; i < n; i += k) {
11+
int cnt1[26]{};
12+
for (int j = i; j < i + k; ++j) {
13+
cnt1[s[j] - 'a']++;
14+
}
15+
for (int j = 0; j < 26; ++j) {
16+
if (cnt1[j] * (n / k) != cnt[j]) {
17+
return false;
18+
}
19+
}
20+
}
21+
return true;
22+
};
23+
for (int i = 1;; ++i) {
24+
if (n % i == 0 && check(i)) {
25+
return i;
26+
}
27+
}
28+
}
29+
};

0 commit comments

Comments
 (0)