DEV Community

Karleb
Karleb

Posted on

#2000. Reverse Prefix of Word

https://leetcode.com/problems/reverse-prefix-of-word/?envType=daily-question&envId=2024-05-01

/**
 * @param {string} word
 * @param {character} ch
 * @return {string}
 */
var reversePrefix = function (word, ch) {
    if (word.indexOf(ch) === -1) return word

    word = word.split('')
    let idx = word.indexOf(ch)
    let reverse = word.splice(0, idx + 1)
    reverse.reverse()

    let newWord = reverse.concat(word)

    return newWord.join('')
};

Enter fullscreen mode Exit fullscreen mode

Top comments (0)