-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreversePrefixOfWord.js
More file actions
34 lines (28 loc) · 958 Bytes
/
reversePrefixOfWord.js
File metadata and controls
34 lines (28 loc) · 958 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* @param {string} word
* @param {character} ch
* @return {string}
*/
var reversePrefix = function (word, ch) {
let findIndexHere = word.indexOf(ch)
let sliceAndReverse = word.slice(0, findIndexHere + 1).split('').reverse()
let sliceSecond = word.slice(findIndexHere + 1).split('')
return sliceAndReverse.concat(sliceSecond).join('')
};
/*
Example 1:
Input: word = "abcdefd", ch = "d"
Output: "dcbaefd"
Explanation: The first occurrence of "d" is at index 3.
Reverse the part of word from 0 to 3 (inclusive), the resulting string is "dcbaefd".
Example 2:
Input: word = "xyxzxe", ch = "z"
Output: "zxyxxe"
Explanation: The first and only occurrence of "z" is at index 3.
Reverse the part of word from 0 to 3 (inclusive), the resulting string is "zxyxxe".
Example 3:
Input: word = "abcd", ch = "z"
Output: "abcd"
Explanation: "z" does not exist in word.
You should not do any reverse operation, the resulting string is "abcd".
*/