Skip to content

Commit 1c3f97e

Browse files
committed
feat: add solutions to lc problems: No.1647,2504
* No.1647.Minimum Deletions to Make Character Frequencies Unique * No.2504.Concatenate the Name and the Profession
1 parent 3f6f70e commit 1c3f97e

File tree

9 files changed

+345
-38
lines changed

9 files changed

+345
-38
lines changed

solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/README.md

Lines changed: 145 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,17 @@
5252

5353
<!-- 这里可写通用的实现逻辑 -->
5454

55-
哈希表 + 排序。
55+
**方法一:数组 + 排序**
56+
57+
我们先用一个长度为 $26$ 的数组 `cnt` 统计字符串 $s$ 中每个字母出现的次数。
58+
59+
然后我们对数组 `cnt` 进行倒序排序。定义一个变量 `pre` 记录当前字母的出现次数。
60+
61+
接下来,遍历数组 `cnt` 每个元素 $v$,如果当前 `pre` 等于 $0$,我们直接将答案加上 $v$;否则,如果 $v \geq pre$,我们将答案加上 $v-pre+1$,并且将 `pre` 减去 $1$,否则,我们直接将 `pre` 更新为 $v$。然后继续遍历下个元素。
62+
63+
遍历结束,返回答案即可。
64+
65+
时间复杂度 $O(n + C \times \log C)$,空间复杂度 $O(C)$。其中 $n$ 是字符串 $s$ 的长度,而 $C$ 为字母集的大小。本题中 $C=26$。
5666

5767
<!-- tabs:start -->
5868

@@ -63,9 +73,24 @@
6373
```python
6474
class Solution:
6575
def minDeletions(self, s: str) -> int:
66-
counter = Counter(s)
67-
vals = [v for v in counter.values()]
68-
vals.sort(reverse=True)
76+
cnt = Counter(s)
77+
ans, pre = 0, inf
78+
for v in sorted(cnt.values(), reverse=True):
79+
if pre == 0:
80+
ans += v
81+
elif v >= pre:
82+
ans += v - pre + 1
83+
pre -= 1
84+
else:
85+
pre = v
86+
return ans
87+
```
88+
89+
```python
90+
class Solution:
91+
def minDeletions(self, s: str) -> int:
92+
cnt = Counter(s)
93+
vals = sorted(cnt.values(), reverse=True)
6994
ans = 0
7095
for i in range(1, len(vals)):
7196
while vals[i] >= vals[i - 1] and vals[i] > 0:
@@ -81,15 +106,15 @@ class Solution:
81106
```java
82107
class Solution {
83108
public int minDeletions(String s) {
84-
int[] counter = new int[26];
85-
for (char c : s.toCharArray()) {
86-
++counter[c - 'a'];
109+
int[] cnt = new int[26];
110+
for (int i = 0; i < s.length(); ++i) {
111+
++cnt[s.charAt(i) - 'a'];
87112
}
88-
Arrays.sort(counter);
113+
Arrays.sort(cnt);
89114
int ans = 0;
90115
for (int i = 24; i >= 0; --i) {
91-
while (counter[i] >= counter[i + 1] && counter[i] > 0) {
92-
--counter[i];
116+
while (cnt[i] >= cnt[i + 1] && cnt[i] > 0) {
117+
--cnt[i];
93118
++ans;
94119
}
95120
}
@@ -98,6 +123,116 @@ class Solution {
98123
}
99124
```
100125

126+
```java
127+
class Solution {
128+
public int minDeletions(String s) {
129+
int[] cnt = new int[26];
130+
for (int i = 0; i < s.length(); ++i) {
131+
++cnt[s.charAt(i) - 'a'];
132+
}
133+
Arrays.sort(cnt);
134+
int ans = 0, pre = 1 << 30;
135+
for (int i = 25; i >= 0; --i) {
136+
int v = cnt[i];
137+
if (pre == 0) {
138+
ans += v;
139+
} else if (v >= pre) {
140+
ans += v - pre + 1;
141+
--pre;
142+
} else {
143+
pre = v;
144+
}
145+
}
146+
return ans;
147+
}
148+
}
149+
```
150+
151+
### **C++**
152+
153+
```cpp
154+
class Solution {
155+
public:
156+
int minDeletions(string s) {
157+
vector<int> cnt(26);
158+
for (char& c : s) ++cnt[c - 'a'];
159+
sort(cnt.rbegin(), cnt.rend());
160+
int ans = 0;
161+
for (int i = 1; i < 26; ++i) {
162+
while (cnt[i] >= cnt[i - 1] && cnt[i] > 0) {
163+
--cnt[i];
164+
++ans;
165+
}
166+
}
167+
return ans;
168+
}
169+
};
170+
```
171+
172+
```cpp
173+
class Solution {
174+
public:
175+
int minDeletions(string s) {
176+
vector<int> cnt(26);
177+
for (char& c : s) ++cnt[c - 'a'];
178+
sort(cnt.rbegin(), cnt.rend());
179+
int ans = 0, pre = 1 << 30;
180+
for (int& v : cnt) {
181+
if (pre == 0) {
182+
ans += v;
183+
} else if (v >= pre) {
184+
ans += v - pre + 1;
185+
--pre;
186+
} else {
187+
pre = v;
188+
}
189+
}
190+
return ans;
191+
}
192+
};
193+
```
194+
195+
### **Go**
196+
197+
```go
198+
func minDeletions(s string) (ans int) {
199+
cnt := make([]int, 26)
200+
for _, c := range s {
201+
cnt[c-'a']++
202+
}
203+
sort.Sort(sort.Reverse(sort.IntSlice(cnt)))
204+
for i := 1; i < 26; i++ {
205+
for cnt[i] >= cnt[i-1] && cnt[i] > 0 {
206+
cnt[i]--
207+
ans++
208+
}
209+
}
210+
return
211+
}
212+
```
213+
214+
```go
215+
func minDeletions(s string) (ans int) {
216+
cnt := make([]int, 26)
217+
for _, c := range s {
218+
cnt[c-'a']++
219+
}
220+
sort.Sort(sort.Reverse(sort.IntSlice(cnt)))
221+
pre := 1 << 30
222+
for _, v := range cnt {
223+
if pre == 0 {
224+
ans += v
225+
} else if v >= pre {
226+
ans += v - pre + 1
227+
pre--
228+
} else {
229+
pre = v
230+
}
231+
}
232+
return
233+
}
234+
```
235+
101236
### **TypeScript**
102237

103238
```ts

solution/1600-1699/1647.Minimum Deletions to Make Character Frequencies Unique/README_EN.md

Lines changed: 134 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,24 @@ Note that we only care about characters that are still in the string at the end
5353
```python
5454
class Solution:
5555
def minDeletions(self, s: str) -> int:
56-
counter = Counter(s)
57-
vals = [v for v in counter.values()]
58-
vals.sort(reverse=True)
56+
cnt = Counter(s)
57+
ans, pre = 0, inf
58+
for v in sorted(cnt.values(), reverse=True):
59+
if pre == 0:
60+
ans += v
61+
elif v >= pre:
62+
ans += v - pre + 1
63+
pre -= 1
64+
else:
65+
pre = v
66+
return ans
67+
```
68+
69+
```python
70+
class Solution:
71+
def minDeletions(self, s: str) -> int:
72+
cnt = Counter(s)
73+
vals = sorted(cnt.values(), reverse=True)
5974
ans = 0
6075
for i in range(1, len(vals)):
6176
while vals[i] >= vals[i - 1] and vals[i] > 0:
@@ -69,20 +84,130 @@ class Solution:
6984
```java
7085
class Solution {
7186
public int minDeletions(String s) {
72-
int[] counter = new int[26];
73-
for (char c : s.toCharArray()) {
74-
++counter[c - 'a'];
87+
int[] cnt = new int[26];
88+
for (int i = 0; i < s.length(); ++i) {
89+
++cnt[s.charAt(i) - 'a'];
7590
}
76-
Arrays.sort(counter);
91+
Arrays.sort(cnt);
7792
int ans = 0;
7893
for (int i = 24; i >= 0; --i) {
79-
while (counter[i] >= counter[i + 1] && counter[i] > 0) {
80-
--counter[i];
94+
while (cnt[i] >= cnt[i + 1] && cnt[i] > 0) {
95+
--cnt[i];
96+
++ans;
97+
}
98+
}
99+
return ans;
100+
}
101+
}
102+
```
103+
104+
```java
105+
class Solution {
106+
public int minDeletions(String s) {
107+
int[] cnt = new int[26];
108+
for (int i = 0; i < s.length(); ++i) {
109+
++cnt[s.charAt(i) - 'a'];
110+
}
111+
Arrays.sort(cnt);
112+
int ans = 0, pre = 1 << 30;
113+
for (int i = 25; i >= 0; --i) {
114+
int v = cnt[i];
115+
if (pre == 0) {
116+
ans += v;
117+
} else if (v >= pre) {
118+
ans += v - pre + 1;
119+
--pre;
120+
} else {
121+
pre = v;
122+
}
123+
}
124+
return ans;
125+
}
126+
}
127+
```
128+
129+
### **C++**
130+
131+
```cpp
132+
class Solution {
133+
public:
134+
int minDeletions(string s) {
135+
vector<int> cnt(26);
136+
for (char& c : s) ++cnt[c - 'a'];
137+
sort(cnt.rbegin(), cnt.rend());
138+
int ans = 0;
139+
for (int i = 1; i < 26; ++i) {
140+
while (cnt[i] >= cnt[i - 1] && cnt[i] > 0) {
141+
--cnt[i];
81142
++ans;
82143
}
83144
}
84145
return ans;
85146
}
147+
};
148+
```
149+
150+
```cpp
151+
class Solution {
152+
public:
153+
int minDeletions(string s) {
154+
vector<int> cnt(26);
155+
for (char& c : s) ++cnt[c - 'a'];
156+
sort(cnt.rbegin(), cnt.rend());
157+
int ans = 0, pre = 1 << 30;
158+
for (int& v : cnt) {
159+
if (pre == 0) {
160+
ans += v;
161+
} else if (v >= pre) {
162+
ans += v - pre + 1;
163+
--pre;
164+
} else {
165+
pre = v;
166+
}
167+
}
168+
return ans;
169+
}
170+
};
171+
```
172+
173+
### **Go**
174+
175+
```go
176+
func minDeletions(s string) (ans int) {
177+
cnt := make([]int, 26)
178+
for _, c := range s {
179+
cnt[c-'a']++
180+
}
181+
sort.Sort(sort.Reverse(sort.IntSlice(cnt)))
182+
for i := 1; i < 26; i++ {
183+
for cnt[i] >= cnt[i-1] && cnt[i] > 0 {
184+
cnt[i]--
185+
ans++
186+
}
187+
}
188+
return
189+
}
190+
```
191+
192+
```go
193+
func minDeletions(s string) (ans int) {
194+
cnt := make([]int, 26)
195+
for _, c := range s {
196+
cnt[c-'a']++
197+
}
198+
sort.Sort(sort.Reverse(sort.IntSlice(cnt)))
199+
pre := 1 << 30
200+
for _, v := range cnt {
201+
if pre == 0 {
202+
ans += v
203+
} else if v >= pre {
204+
ans += v - pre + 1
205+
pre--
206+
} else {
207+
pre = v
208+
}
209+
}
210+
return
86211
}
87212
```
88213

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int minDeletions(string s) {
4+
vector<int> cnt(26);
5+
for (char& c : s) ++cnt[c - 'a'];
6+
sort(cnt.rbegin(), cnt.rend());
7+
int ans = 0;
8+
for (int i = 1; i < 26; ++i) {
9+
while (cnt[i] >= cnt[i - 1] && cnt[i] > 0) {
10+
--cnt[i];
11+
++ans;
12+
}
13+
}
14+
return ans;
15+
}
16+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func minDeletions(s string) (ans int) {
2+
cnt := make([]int, 26)
3+
for _, c := range s {
4+
cnt[c-'a']++
5+
}
6+
sort.Sort(sort.Reverse(sort.IntSlice(cnt)))
7+
for i := 1; i < 26; i++ {
8+
for cnt[i] >= cnt[i-1] && cnt[i] > 0 {
9+
cnt[i]--
10+
ans++
11+
}
12+
}
13+
return
14+
}

0 commit comments

Comments
 (0)