Skip to content

Commit 77e4995

Browse files
committed
feat: add solutions to lc problem: No.2000
No.2000. Reverse Prefix of Word
1 parent 5a3a598 commit 77e4995

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

solution/2000-2099/2000.Reverse Prefix of Word/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,38 @@ func reversePrefix(word string, ch byte) string {
117117
}
118118
```
119119

120+
### **TypeScript**
121+
122+
```ts
123+
function reversePrefix(word: string, ch: string): string {
124+
const chars = word.split('');
125+
const i = word.indexOf(ch);
126+
if (i !== -1) {
127+
let l = 0;
128+
let r = i;
129+
while (l < r) {
130+
[chars[l], chars[r]] = [chars[r], chars[l]];
131+
l++;
132+
r--;
133+
}
134+
}
135+
return chars.join('');
136+
}
137+
```
138+
139+
### **Rust**
140+
141+
```rs
142+
impl Solution {
143+
pub fn reverse_prefix(word: String, ch: char) -> String {
144+
match word.find(ch) {
145+
Some(i) => word[..=i].chars().rev().collect::<String>() + &word[i + 1..],
146+
None => word,
147+
}
148+
}
149+
}
150+
```
151+
120152
### **...**
121153

122154
```

solution/2000-2099/2000.Reverse Prefix of Word/README_EN.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,38 @@ func reversePrefix(word string, ch byte) string {
110110
}
111111
```
112112

113+
### **TypeScript**
114+
115+
```ts
116+
function reversePrefix(word: string, ch: string): string {
117+
const chars = word.split('');
118+
const i = word.indexOf(ch);
119+
if (i !== -1) {
120+
let l = 0;
121+
let r = i;
122+
while (l < r) {
123+
[chars[l], chars[r]] = [chars[r], chars[l]];
124+
l++;
125+
r--;
126+
}
127+
}
128+
return chars.join('');
129+
}
130+
```
131+
132+
### **Rust**
133+
134+
```rs
135+
impl Solution {
136+
pub fn reverse_prefix(word: String, ch: char) -> String {
137+
match word.find(ch) {
138+
Some(i) => word[..=i].chars().rev().collect::<String>() + &word[i + 1..],
139+
None => word,
140+
}
141+
}
142+
}
143+
```
144+
113145
### **...**
114146

115147
```
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
impl Solution {
2+
pub fn reverse_prefix(word: String, ch: char) -> String {
3+
match word.find(ch) {
4+
Some(i) => word[..=i].chars().rev().collect::<String>() + &word[i + 1..],
5+
None => word,
6+
}
7+
}
8+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function reversePrefix(word: string, ch: string): string {
2+
const chars = word.split('');
3+
const i = word.indexOf(ch);
4+
if (i !== -1) {
5+
let l = 0;
6+
let r = i;
7+
while (l < r) {
8+
[chars[l], chars[r]] = [chars[r], chars[l]];
9+
l++;
10+
r--;
11+
}
12+
}
13+
return chars.join('');
14+
}

0 commit comments

Comments
 (0)