Skip to content

Commit e637d4f

Browse files
committed
feat: add solutions to lc problem: No.0791
No.0791.Custom Sort String
1 parent 0d7fb1c commit e637d4f

File tree

4 files changed

+191
-0
lines changed

4 files changed

+191
-0
lines changed

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,82 @@ func customSortString(order string, s string) string {
194194
}
195195
```
196196

197+
### **TypeScript**
198+
199+
```ts
200+
function customSortString(order: string, s: string): string {
201+
const toIndex = (c: string) => c.charCodeAt(0) - 'a'.charCodeAt(0);
202+
const n = order.length;
203+
const d = new Array(26).fill(n);
204+
for (let i = 0; i < n; i++) {
205+
d[toIndex(order[i])] = i;
206+
}
207+
return [...s].sort((a, b) => d[toIndex(a)] - d[toIndex(b)]).join('');
208+
}
209+
```
210+
211+
```ts
212+
function customSortString(order: string, s: string): string {
213+
const toIndex = (c: string) => c.charCodeAt(0) - 'a'.charCodeAt(0);
214+
const count = new Array(26).fill(0);
215+
for (const c of s) {
216+
count[toIndex(c)]++;
217+
}
218+
const ans: string[] = [];
219+
for (const c of order) {
220+
const i = toIndex(c);
221+
ans.push(c.repeat(count[i]));
222+
count[i] = 0;
223+
}
224+
for (let i = 0; i < 26; i++) {
225+
if (!count[i]) continue;
226+
ans.push(String.fromCharCode('a'.charCodeAt(0) + i).repeat(count[i]));
227+
}
228+
return ans.join('');
229+
}
230+
```
231+
232+
### **Rust**
233+
234+
```rust
235+
impl Solution {
236+
pub fn custom_sort_string(order: String, s: String) -> String {
237+
let n = order.len();
238+
let mut d = [n; 26];
239+
for (i, c) in order.as_bytes().iter().enumerate() {
240+
d[(c - b'a') as usize] = i;
241+
}
242+
let mut ans = s.chars().collect::<Vec<_>>();
243+
ans.sort_by(|&a, &b| d[(a as u8 - 'a' as u8) as usize].cmp(&d[(b as u8 - 'a' as u8) as usize]));
244+
ans.into_iter().collect()
245+
}
246+
}
247+
```
248+
249+
```rust
250+
impl Solution {
251+
pub fn custom_sort_string(order: String, s: String) -> String {
252+
let mut count = [0; 26];
253+
for c in s.as_bytes() {
254+
count[(c - b'a') as usize] += 1;
255+
}
256+
let mut ans = String::new();
257+
for c in order.as_bytes() {
258+
for _ in 0..count[(c - b'a') as usize] {
259+
ans.push(char::from(*c));
260+
}
261+
count[(c - b'a') as usize] = 0;
262+
}
263+
for i in 0..count.len() {
264+
for _ in 0..count[i] {
265+
ans.push(char::from(b'a' + i as u8));
266+
}
267+
}
268+
ans
269+
}
270+
}
271+
```
272+
197273
### **...**
198274

199275
```

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,82 @@ func customSortString(order string, s string) string {
171171
}
172172
```
173173

174+
### **TypeScript**
175+
176+
```ts
177+
function customSortString(order: string, s: string): string {
178+
const toIndex = (c: string) => c.charCodeAt(0) - 'a'.charCodeAt(0);
179+
const n = order.length;
180+
const d = new Array(26).fill(n);
181+
for (let i = 0; i < n; i++) {
182+
d[toIndex(order[i])] = i;
183+
}
184+
return [...s].sort((a, b) => d[toIndex(a)] - d[toIndex(b)]).join('');
185+
}
186+
```
187+
188+
```ts
189+
function customSortString(order: string, s: string): string {
190+
const toIndex = (c: string) => c.charCodeAt(0) - 'a'.charCodeAt(0);
191+
const count = new Array(26).fill(0);
192+
for (const c of s) {
193+
count[toIndex(c)]++;
194+
}
195+
const ans: string[] = [];
196+
for (const c of order) {
197+
const i = toIndex(c);
198+
ans.push(c.repeat(count[i]));
199+
count[i] = 0;
200+
}
201+
for (let i = 0; i < 26; i++) {
202+
if (!count[i]) continue;
203+
ans.push(String.fromCharCode('a'.charCodeAt(0) + i).repeat(count[i]));
204+
}
205+
return ans.join('');
206+
}
207+
```
208+
209+
### **Rust**
210+
211+
```rust
212+
impl Solution {
213+
pub fn custom_sort_string(order: String, s: String) -> String {
214+
let n = order.len();
215+
let mut d = [n; 26];
216+
for (i, c) in order.as_bytes().iter().enumerate() {
217+
d[(c - b'a') as usize] = i;
218+
}
219+
let mut ans = s.chars().collect::<Vec<_>>();
220+
ans.sort_by(|&a, &b| d[(a as u8 - 'a' as u8) as usize].cmp(&d[(b as u8 - 'a' as u8) as usize]));
221+
ans.into_iter().collect()
222+
}
223+
}
224+
```
225+
226+
```rust
227+
impl Solution {
228+
pub fn custom_sort_string(order: String, s: String) -> String {
229+
let mut count = [0; 26];
230+
for c in s.as_bytes() {
231+
count[(c - b'a') as usize] += 1;
232+
}
233+
let mut ans = String::new();
234+
for c in order.as_bytes() {
235+
for _ in 0..count[(c - b'a') as usize] {
236+
ans.push(char::from(*c));
237+
}
238+
count[(c - b'a') as usize] = 0;
239+
}
240+
for i in 0..count.len() {
241+
for _ in 0..count[i] {
242+
ans.push(char::from(b'a' + i as u8));
243+
}
244+
}
245+
ans
246+
}
247+
}
248+
```
249+
174250
### **...**
175251

176252
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
impl Solution {
2+
pub fn custom_sort_string(order: String, s: String) -> String {
3+
let mut count = [0; 26];
4+
for c in s.as_bytes() {
5+
count[(c - b'a') as usize] += 1;
6+
}
7+
let mut ans = String::new();
8+
for c in order.as_bytes() {
9+
for _ in 0..count[(c - b'a') as usize] {
10+
ans.push(char::from(*c));
11+
}
12+
count[(c - b'a') as usize] = 0;
13+
}
14+
for i in 0..count.len() {
15+
for _ in 0..count[i] {
16+
ans.push(char::from(b'a' + i as u8));
17+
}
18+
}
19+
ans
20+
}
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function customSortString(order: string, s: string): string {
2+
const toIndex = (c: string) => c.charCodeAt(0) - 'a'.charCodeAt(0);
3+
const count = new Array(26).fill(0);
4+
for (const c of s) {
5+
count[toIndex(c)]++;
6+
}
7+
const ans: string[] = [];
8+
for (const c of order) {
9+
const i = toIndex(c);
10+
ans.push(c.repeat(count[i]));
11+
count[i] = 0;
12+
}
13+
for (let i = 0; i < 26; i++) {
14+
if (!count[i]) continue;
15+
ans.push(String.fromCharCode('a'.charCodeAt(0) + i).repeat(count[i]));
16+
}
17+
return ans.join('');
18+
}

0 commit comments

Comments
 (0)