Skip to content

Commit d6f770c

Browse files
committed
https://leetcode.cn/problems/reverse-prefix-of-word/
1 parent 138d828 commit d6f770c

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ leetcode 测试
1010

1111
##### 包含的内容如下
1212

13+
https://leetcode.cn/problems/reverse-prefix-of-word/
14+
1315
https://leetcode.cn/problems/counting-words-with-a-given-prefix/
1416

1517
https://leetcode.cn/problems/maximum-product-difference-between-two-pairs/

reverse-prefix-of-word/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function reversePrefix(word: string, ch: string): string {
2+
const index = Array.prototype.findIndex.call(word, (c) => c === ch);
3+
4+
if (index < 0) return word;
5+
return (
6+
Array.from(word.slice(0, index + 1))
7+
.reverse()
8+
.join("") + word.slice(index + 1)
9+
);
10+
}
11+
export default reversePrefix;

0 commit comments

Comments
 (0)