aboutsummaryrefslogtreecommitdiff
path: root/reverse_prefix_word.py
blob: 008d53d42a41a25f4e7c8bcb98c12831b4c8b22d (plain)
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
# 2000. Reverse Prefix of Word

"""
given a 0-indexed string 'word' and a character 'ch', reverse the segment of
'word' that starts at index 0 and ends at the index of the first occurence of
'ch' (inclusively). if the character 'ch' does not exist in 'word', do
nothing. return the resulting string.
"""


class Solution(object):
    def reversePrefix(self, word, ch):
        """
        :type word: str
        :type ch: str
        :rtype: str
        """
        idx = word.find(ch)
        if idx:
            return word[: idx + 1][::-1] + word[idx + 1 :]
        return word


if __name__ == "__main__":
    obj = Solution()
    print(obj.reversePrefix("abcdefd", "d"))
    print(obj.reversePrefix("xyxzxe", "z"))
    print(obj.reversePrefix("abcd", "z"))