2000. Reverse Prefix of Word

Given a string, revers its prefix ended with a specified character

Given a string ans a specified character, reverse the prefix that is ended with the specified character.

Intuition

Simulate the process.

Approach

find the first occurrence of the specified character, then reverse the prefix.

Complexity

  • Time complexity: find the first occurrence of the specified character. $$O(n)$$

  • Space complexity: no extra spaces are needed. $$O(1)$$

Code

1
2
3
4
5
6
7
8
9
class Solution {
public:
    string reversePrefix(string word, char ch) {
        auto pos = word.find(ch);
        if (pos == word.npos)   return word;
        reverse(word.begin(), word.begin() + pos + 1);
        return word;
    }
};
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy