Skip to content

Commit 9ad6e11

Browse files
committed
feat: add solutions to lc problem: No.0791
No.0791.Custom Sort String
1 parent 8438618 commit 9ad6e11

File tree

6 files changed

+300
-0
lines changed

6 files changed

+300
-0
lines changed

solution/0700-0799/0791.Custom Sort String/README.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,144 @@
4545

4646
<!-- 这里可写通用的实现逻辑 -->
4747

48+
**方法一:哈希表 + 自定义排序**
49+
50+
将字符串中的字符按照 $order$ 中出现的位置(下标)排序,未在 $order$ 中出现的,下标默认视为 $-1$。
51+
52+
时间复杂度 $O(nlogn+m)$,空间复杂度 $O(m)$,其中 $m$ 和 $n$ 分别为 $order$ 和 $s$ 的长度。
53+
54+
**方法二:字符计数**
55+
56+
统计 $s$ 中每个字符的出现次数,存储在 $cnt$ 数组中。
57+
58+
然后把在 $order$ 中出现的字符按照 $order$ 中的顺序排序,剩余字符添加到当前字符串的后面。
59+
60+
时间复杂度 $O(m+n)$,其中 $m$ 和 $n$ 分别为 $order$ 和 $s$ 的长度。空间复杂度 $O(n)$。
61+
4862
<!-- tabs:start -->
4963

5064
### **Python3**
5165

5266
<!-- 这里可写当前语言的特殊实现逻辑 -->
5367

5468
```python
69+
class Solution:
70+
def customSortString(self, order: str, s: str) -> str:
71+
cs = list(s)
72+
m = {v: i for i, v in enumerate(order)}
73+
cs.sort(key=lambda x: m.get(x, -1))
74+
return ''.join(cs)
75+
```
5576

77+
```python
78+
class Solution:
79+
def customSortString(self, order: str, s: str) -> str:
80+
cnt = Counter(s)
81+
ans = []
82+
for c in order:
83+
ans.append(cnt[c] * c)
84+
cnt[c] = 0
85+
for c in s:
86+
ans.append(cnt[c] * c)
87+
cnt[c] = 0
88+
return ''.join(ans)
5689
```
5790

5891
### **Java**
5992

6093
<!-- 这里可写当前语言的特殊实现逻辑 -->
6194

6295
```java
96+
class Solution {
97+
public String customSortString(String order, String s) {
98+
Map<Character, Integer> m = new HashMap<>();
99+
for (int i = 0; i < order.length(); ++i) {
100+
m.put(order.charAt(i), i);
101+
}
102+
List<Character> cs = new ArrayList<>();
103+
for (char c : s.toCharArray()) {
104+
cs.add(c);
105+
}
106+
cs.sort((a, b) -> m.getOrDefault(a, -1) - m.getOrDefault(b, -1));
107+
StringBuilder ans = new StringBuilder();
108+
for (char c : cs) {
109+
ans.append(c);
110+
}
111+
return ans.toString();
112+
}
113+
}
114+
```
115+
116+
```java
117+
class Solution {
118+
public String customSortString(String order, String s) {
119+
int[] cnt = new int[26];
120+
for (char c : s.toCharArray()) {
121+
++cnt[c - 'a'];
122+
}
123+
StringBuilder ans = new StringBuilder();
124+
for (char c : order.toCharArray()) {
125+
while (cnt[c - 'a']-- > 0) {
126+
ans.append(c);
127+
}
128+
}
129+
for (char c : s.toCharArray()) {
130+
while (cnt[c - 'a']-- > 0) {
131+
ans.append(c);
132+
}
133+
}
134+
return ans.toString();
135+
}
136+
}
137+
```
138+
139+
### **C++**
140+
141+
```cpp
142+
class Solution {
143+
public:
144+
string customSortString(string order, string s) {
145+
vector<int> cnt(26);
146+
for (char c : s) ++cnt[c - 'a'];
147+
string ans = "";
148+
for (char c : order) {
149+
while (cnt[c - 'a']-- > 0) {
150+
ans += c;
151+
}
152+
}
153+
for (char c : s) {
154+
while (cnt[c - 'a']-- > 0) {
155+
ans += c;
156+
}
157+
}
158+
return ans;
159+
}
160+
};
161+
```
63162
163+
### **Go**
164+
165+
```go
166+
func customSortString(order string, s string) string {
167+
cnt := make([]int, 26)
168+
for _, c := range s {
169+
cnt[c-'a']++
170+
}
171+
ans := []rune{}
172+
for _, c := range order {
173+
for cnt[c-'a'] > 0 {
174+
ans = append(ans, c)
175+
cnt[c-'a']--
176+
}
177+
}
178+
for _, c := range s {
179+
for cnt[c-'a'] > 0 {
180+
ans = append(ans, c)
181+
cnt[c-'a']--
182+
}
183+
}
184+
return string(ans)
185+
}
64186
```
65187

66188
### **...**

solution/0700-0799/0791.Custom Sort String/README_EN.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,121 @@ Since &quot;d&quot; does not appear in order, it can be at any position in the r
4545
### **Python3**
4646

4747
```python
48+
class Solution:
49+
def customSortString(self, order: str, s: str) -> str:
50+
cs = list(s)
51+
m = {v: i for i, v in enumerate(order)}
52+
cs.sort(key=lambda x: m.get(x, -1))
53+
return ''.join(cs)
54+
```
4855

56+
```python
57+
class Solution:
58+
def customSortString(self, order: str, s: str) -> str:
59+
cnt = Counter(s)
60+
ans = []
61+
for c in order:
62+
ans.append(cnt[c] * c)
63+
cnt[c] = 0
64+
for c in s:
65+
ans.append(cnt[c] * c)
66+
cnt[c] = 0
67+
return ''.join(ans)
4968
```
5069

5170
### **Java**
5271

5372
```java
73+
class Solution {
74+
public String customSortString(String order, String s) {
75+
Map<Character, Integer> m = new HashMap<>();
76+
for (int i = 0; i < order.length(); ++i) {
77+
m.put(order.charAt(i), i);
78+
}
79+
List<Character> cs = new ArrayList<>();
80+
for (char c : s.toCharArray()) {
81+
cs.add(c);
82+
}
83+
cs.sort((a, b) -> m.getOrDefault(a, -1) - m.getOrDefault(b, -1));
84+
StringBuilder ans = new StringBuilder();
85+
for (char c : cs) {
86+
ans.append(c);
87+
}
88+
return ans.toString();
89+
}
90+
}
91+
```
92+
93+
```java
94+
class Solution {
95+
public String customSortString(String order, String s) {
96+
int[] cnt = new int[26];
97+
for (char c : s.toCharArray()) {
98+
++cnt[c - 'a'];
99+
}
100+
StringBuilder ans = new StringBuilder();
101+
for (char c : order.toCharArray()) {
102+
while (cnt[c - 'a']-- > 0) {
103+
ans.append(c);
104+
}
105+
}
106+
for (char c : s.toCharArray()) {
107+
while (cnt[c - 'a']-- > 0) {
108+
ans.append(c);
109+
}
110+
}
111+
return ans.toString();
112+
}
113+
}
114+
```
115+
116+
### **C++**
117+
118+
```cpp
119+
class Solution {
120+
public:
121+
string customSortString(string order, string s) {
122+
vector<int> cnt(26);
123+
for (char c : s) ++cnt[c - 'a'];
124+
string ans = "";
125+
for (char c : order) {
126+
while (cnt[c - 'a']-- > 0) {
127+
ans += c;
128+
}
129+
}
130+
for (char c : s) {
131+
while (cnt[c - 'a']-- > 0) {
132+
ans += c;
133+
}
134+
}
135+
return ans;
136+
}
137+
};
138+
```
54139
140+
### **Go**
141+
142+
```go
143+
func customSortString(order string, s string) string {
144+
cnt := make([]int, 26)
145+
for _, c := range s {
146+
cnt[c-'a']++
147+
}
148+
ans := []rune{}
149+
for _, c := range order {
150+
for cnt[c-'a'] > 0 {
151+
ans = append(ans, c)
152+
cnt[c-'a']--
153+
}
154+
}
155+
for _, c := range s {
156+
for cnt[c-'a'] > 0 {
157+
ans = append(ans, c)
158+
cnt[c-'a']--
159+
}
160+
}
161+
return string(ans)
162+
}
55163
```
56164

57165
### **...**
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
string customSortString(string order, string s) {
4+
vector<int> cnt(26);
5+
for (char c : s) ++cnt[c - 'a'];
6+
string ans = "";
7+
for (char c : order) {
8+
while (cnt[c - 'a']-- > 0) {
9+
ans += c;
10+
}
11+
}
12+
for (char c : s) {
13+
while (cnt[c - 'a']-- > 0) {
14+
ans += c;
15+
}
16+
}
17+
return ans;
18+
}
19+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func customSortString(order string, s string) string {
2+
cnt := make([]int, 26)
3+
for _, c := range s {
4+
cnt[c-'a']++
5+
}
6+
ans := []rune{}
7+
for _, c := range order {
8+
for cnt[c-'a'] > 0 {
9+
ans = append(ans, c)
10+
cnt[c-'a']--
11+
}
12+
}
13+
for _, c := range s {
14+
for cnt[c-'a'] > 0 {
15+
ans = append(ans, c)
16+
cnt[c-'a']--
17+
}
18+
}
19+
return string(ans)
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public String customSortString(String order, String s) {
3+
int[] cnt = new int[26];
4+
for (char c : s.toCharArray()) {
5+
++cnt[c - 'a'];
6+
}
7+
StringBuilder ans = new StringBuilder();
8+
for (char c : order.toCharArray()) {
9+
while (cnt[c - 'a']-- > 0) {
10+
ans.append(c);
11+
}
12+
}
13+
for (char c : s.toCharArray()) {
14+
while (cnt[c - 'a']-- > 0) {
15+
ans.append(c);
16+
}
17+
}
18+
return ans.toString();
19+
}
20+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def customSortString(self, order: str, s: str) -> str:
3+
cnt = Counter(s)
4+
ans = []
5+
for c in order:
6+
ans.append(cnt[c] * c)
7+
cnt[c] = 0
8+
for c in s:
9+
ans.append(cnt[c] * c)
10+
cnt[c] = 0
11+
return ''.join(ans)

0 commit comments

Comments
 (0)