Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ca7155d

Browse files
committedSep 24, 2021
Add solution #2000
1 parent db8159c commit ca7155d

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
1472|[Design Browser History](./1472-design-browser-history.js)|Medium|
9595
1598|[Crawler Log Folder](./1598-crawler-log-folder.js)|Easy|
9696
1880|[Check if Word Equals Summation of Two Words](./1880-check-if-word-equals-summation-of-two-words.js)|Easy|
97+
2000|[Reverse Prefix of Word](./2000-reverse-prefix-of-word.js)|Easy|
9798

9899
## License
99100

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* 2000. Reverse Prefix of Word
3+
* https://leetcode.com/problems/reverse-prefix-of-word/
4+
* Difficulty: Easy
5+
*
6+
* Given a 0-indexed string word and a character ch, reverse the segment of word that
7+
* starts at index 0 and ends at the index of the first occurrence of ch (inclusive).
8+
* If the character ch does not exist in word, do nothing.
9+
*
10+
* For example, if word = "abcdefd" and ch = "d", then you should reverse the segment
11+
* that starts at 0 and ends at 3 (inclusive). The resulting string will be "dcbaefd".
12+
*/
13+
14+
/**
15+
* @param {string} word
16+
* @param {character} ch
17+
* @return {string}
18+
*/
19+
var reversePrefix = function(word, ch) {
20+
const index = word.indexOf(ch) + 1;
21+
return word.slice(0, index).split('').reverse().join('') + word.slice(index);
22+
};

0 commit comments

Comments
 (0)
Please sign in to comment.