Skip to content

Commit 662e898

Browse files
committed
feat: add solutions to lc problem: No.2423~2430
* No.2423.Remove Letter To Equalize Frequency * No.2424.Longest Uploaded Prefix * No.2425.Bitwise XOR of All Pairings * No.2426.Number of Pairs Satisfying Inequality * No.2427.Number of Common Factors * No.2428.Maximum Sum of an Hourglass * No.2429.Minimize XOR * No.2430.Maximum Deletions on a String
1 parent c959c80 commit 662e898

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+3596
-1
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
# [2423. 删除字符使频率相同](https://leetcode.cn/problems/remove-letter-to-equalize-frequency)
2+
3+
[English Version](/solution/2400-2499/2423.Remove%20Letter%20To%20Equalize%20Frequency/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong>&nbsp;开始的字符串&nbsp;<code>word</code>&nbsp;,字符串只包含小写英文字母。你需要选择 <strong>一个</strong>&nbsp;下标并 <strong>删除</strong>&nbsp;下标处的字符,使得 <code>word</code>&nbsp;中剩余每个字母出现 <strong>频率</strong>&nbsp;相同。</p>
10+
11+
<p>如果删除一个字母后,<code>word</code>&nbsp;中剩余所有字母的出现频率都相同,那么返回 <code>true</code>&nbsp;,否则返回 <code>false</code>&nbsp;。</p>
12+
13+
<p><strong>注意:</strong></p>
14+
15+
<ul>
16+
<li>字母&nbsp;<code>x</code>&nbsp;的 <strong>频率</strong><strong>&nbsp;</strong>是这个字母在字符串中出现的次数。</li>
17+
<li>你 <strong>必须</strong>&nbsp;恰好删除一个字母,不能一个字母都不删除。</li>
18+
</ul>
19+
20+
<p>&nbsp;</p>
21+
22+
<p><strong>示例 1:</strong></p>
23+
24+
<pre><b>输入:</b>word = "abcc"
25+
<b>输出:</b>true
26+
<b>解释:</b>选择下标 3 并删除该字母,word 变成 "abc" 且每个字母出现频率都为 1 。
27+
</pre>
28+
29+
<p><strong>示例 2:</strong></p>
30+
31+
<pre><b>输入:</b>word = "aazz"
32+
<b>输出:</b>false
33+
<b>解释:</b>我们必须删除一个字母,所以要么 "a" 的频率变为 1 且 "z" 的频率为 2 ,要么两个字母频率反过来。所以不可能让剩余所有字母出现频率相同。
34+
</pre>
35+
36+
<p>&nbsp;</p>
37+
38+
<p><strong>提示:</strong></p>
39+
40+
<ul>
41+
<li><code>2 &lt;= word.length &lt;= 100</code></li>
42+
<li><code>word</code>&nbsp;只包含小写英文字母。</li>
43+
</ul>
44+
45+
## 解法
46+
47+
<!-- 这里可写通用的实现逻辑 -->
48+
49+
**方法一:直接模拟**
50+
51+
遍历字符串中的每个字符,删除该字符后,判断剩余字符串中每个字符出现的频率是否相同。如果相同,返回 `true`,否则遍历结束,返回 `false`
52+
53+
时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 为字符串的长度。
54+
55+
<!-- tabs:start -->
56+
57+
### **Python3**
58+
59+
<!-- 这里可写当前语言的特殊实现逻辑 -->
60+
61+
```python
62+
class Solution:
63+
def equalFrequency(self, word: str) -> bool:
64+
for i in range(len(word)):
65+
cnt = Counter(word[:i] + word[i + 1:])
66+
if len(set(cnt.values())) == 1:
67+
return True
68+
return False
69+
```
70+
71+
### **Java**
72+
73+
<!-- 这里可写当前语言的特殊实现逻辑 -->
74+
75+
```java
76+
class Solution {
77+
public boolean equalFrequency(String word) {
78+
for (int i = 0; i < word.length(); ++i) {
79+
int[] cnt = new int[26];
80+
for (int j = 0; j < word.length(); ++j) {
81+
if (j != i) {
82+
++cnt[word.charAt(j) - 'a'];
83+
}
84+
}
85+
Set<Integer> vis = new HashSet<>();
86+
for (int v : cnt) {
87+
if (v > 0) {
88+
vis.add(v);
89+
}
90+
}
91+
if (vis.size() == 1) {
92+
return true;
93+
}
94+
}
95+
return false;
96+
}
97+
}
98+
```
99+
100+
### **C++**
101+
102+
```cpp
103+
class Solution {
104+
public:
105+
bool equalFrequency(string word) {
106+
for (int i = 0; i < word.size(); ++i) {
107+
int cnt[26] = {0};
108+
for (int j = 0; j < word.size(); ++j) {
109+
if (j != i) {
110+
++cnt[word[j] - 'a'];
111+
}
112+
}
113+
unordered_set<int> vis;
114+
for (int v : cnt) {
115+
if (v) {
116+
vis.insert(v);
117+
}
118+
}
119+
if (vis.size() == 1) {
120+
return true;
121+
}
122+
}
123+
return false;
124+
}
125+
};
126+
```
127+
128+
### **Go**
129+
130+
```go
131+
func equalFrequency(word string) bool {
132+
for i := range word {
133+
cnt := make([]int, 26)
134+
for j, c := range word {
135+
if j != i {
136+
cnt[c-'a']++
137+
}
138+
}
139+
vis := map[int]bool{}
140+
for _, v := range cnt {
141+
if v > 0 {
142+
vis[v] = true
143+
}
144+
}
145+
if len(vis) == 1 {
146+
return true
147+
}
148+
}
149+
return false
150+
}
151+
```
152+
153+
### **TypeScript**
154+
155+
```ts
156+
157+
```
158+
159+
### **...**
160+
161+
```
162+
163+
```
164+
165+
<!-- tabs:end -->
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# [2423. Remove Letter To Equalize Frequency](https://leetcode.com/problems/remove-letter-to-equalize-frequency)
2+
3+
[中文文档](/solution/2400-2499/2423.Remove%20Letter%20To%20Equalize%20Frequency/README.md)
4+
5+
## Description
6+
7+
<p>You are given a <strong>0-indexed</strong> string <code>word</code>, consisting of lowercase English letters. You need to select <strong>one</strong> index and <strong>remove</strong> the letter at that index from <code>word</code> so that the <strong>frequency</strong> of every letter present in <code>word</code> is equal.</p>
8+
9+
<p>Return<em> </em><code>true</code><em> if it is possible to remove one letter so that the frequency of all letters in </em><code>word</code><em> are equal, and </em><code>false</code><em> otherwise</em>.</p>
10+
11+
<p><strong>Note:</strong></p>
12+
13+
<ul>
14+
<li>The <b>frequency</b> of a letter <code>x</code> is the number of times it occurs in the string.</li>
15+
<li>You <strong>must</strong> remove exactly one letter and cannot chose to do nothing.</li>
16+
</ul>
17+
18+
<p>&nbsp;</p>
19+
<p><strong>Example 1:</strong></p>
20+
21+
<pre>
22+
<strong>Input:</strong> word = &quot;abcc&quot;
23+
<strong>Output:</strong> true
24+
<strong>Explanation:</strong> Select index 3 and delete it: word becomes &quot;abc&quot; and each character has a frequency of 1.
25+
</pre>
26+
27+
<p><strong>Example 2:</strong></p>
28+
29+
<pre>
30+
<strong>Input:</strong> word = &quot;aazz&quot;
31+
<strong>Output:</strong> false
32+
<strong>Explanation:</strong> We must delete a character, so either the frequency of &quot;a&quot; is 1 and the frequency of &quot;z&quot; is 2, or vice versa. It is impossible to make all present letters have equal frequency.
33+
</pre>
34+
35+
<p>&nbsp;</p>
36+
<p><strong>Constraints:</strong></p>
37+
38+
<ul>
39+
<li><code>2 &lt;= word.length &lt;= 100</code></li>
40+
<li><code>word</code> consists of lowercase English letters only.</li>
41+
</ul>
42+
43+
## Solutions
44+
45+
<!-- tabs:start -->
46+
47+
### **Python3**
48+
49+
```python
50+
class Solution:
51+
def equalFrequency(self, word: str) -> bool:
52+
for i in range(len(word)):
53+
cnt = Counter(word[:i] + word[i + 1:])
54+
if len(set(cnt.values())) == 1:
55+
return True
56+
return False
57+
```
58+
59+
### **Java**
60+
61+
```java
62+
class Solution {
63+
public boolean equalFrequency(String word) {
64+
for (int i = 0; i < word.length(); ++i) {
65+
int[] cnt = new int[26];
66+
for (int j = 0; j < word.length(); ++j) {
67+
if (j != i) {
68+
++cnt[word.charAt(j) - 'a'];
69+
}
70+
}
71+
Set<Integer> vis = new HashSet<>();
72+
for (int v : cnt) {
73+
if (v > 0) {
74+
vis.add(v);
75+
}
76+
}
77+
if (vis.size() == 1) {
78+
return true;
79+
}
80+
}
81+
return false;
82+
}
83+
}
84+
```
85+
86+
### **C++**
87+
88+
```cpp
89+
class Solution {
90+
public:
91+
bool equalFrequency(string word) {
92+
for (int i = 0; i < word.size(); ++i) {
93+
int cnt[26] = {0};
94+
for (int j = 0; j < word.size(); ++j) {
95+
if (j != i) {
96+
++cnt[word[j] - 'a'];
97+
}
98+
}
99+
unordered_set<int> vis;
100+
for (int v : cnt) {
101+
if (v) {
102+
vis.insert(v);
103+
}
104+
}
105+
if (vis.size() == 1) {
106+
return true;
107+
}
108+
}
109+
return false;
110+
}
111+
};
112+
```
113+
114+
### **Go**
115+
116+
```go
117+
func equalFrequency(word string) bool {
118+
for i := range word {
119+
cnt := make([]int, 26)
120+
for j, c := range word {
121+
if j != i {
122+
cnt[c-'a']++
123+
}
124+
}
125+
vis := map[int]bool{}
126+
for _, v := range cnt {
127+
if v > 0 {
128+
vis[v] = true
129+
}
130+
}
131+
if len(vis) == 1 {
132+
return true
133+
}
134+
}
135+
return false
136+
}
137+
```
138+
139+
### **TypeScript**
140+
141+
```ts
142+
143+
```
144+
145+
### **...**
146+
147+
```
148+
149+
```
150+
151+
<!-- tabs:end -->
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
bool equalFrequency(string word) {
4+
for (int i = 0; i < word.size(); ++i) {
5+
int cnt[26] = {0};
6+
for (int j = 0; j < word.size(); ++j) {
7+
if (j != i) {
8+
++cnt[word[j] - 'a'];
9+
}
10+
}
11+
unordered_set<int> vis;
12+
for (int v : cnt) {
13+
if (v) {
14+
vis.insert(v);
15+
}
16+
}
17+
if (vis.size() == 1) {
18+
return true;
19+
}
20+
}
21+
return false;
22+
}
23+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func equalFrequency(word string) bool {
2+
for i := range word {
3+
cnt := make([]int, 26)
4+
for j, c := range word {
5+
if j != i {
6+
cnt[c-'a']++
7+
}
8+
}
9+
vis := map[int]bool{}
10+
for _, v := range cnt {
11+
if v > 0 {
12+
vis[v] = true
13+
}
14+
}
15+
if len(vis) == 1 {
16+
return true
17+
}
18+
}
19+
return false
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public boolean equalFrequency(String word) {
3+
for (int i = 0; i < word.length(); ++i) {
4+
int[] cnt = new int[26];
5+
for (int j = 0; j < word.length(); ++j) {
6+
if (j != i) {
7+
++cnt[word.charAt(j) - 'a'];
8+
}
9+
}
10+
Set<Integer> vis = new HashSet<>();
11+
for (int v : cnt) {
12+
if (v > 0) {
13+
vis.add(v);
14+
}
15+
}
16+
if (vis.size() == 1) {
17+
return true;
18+
}
19+
}
20+
return false;
21+
}
22+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def equalFrequency(self, word: str) -> bool:
3+
for i in range(len(word)):
4+
cnt = Counter(word[:i] + word[i + 1 :])
5+
if len(set(cnt.values())) == 1:
6+
return True
7+
return False

0 commit comments

Comments
 (0)