Problem Statement
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 occurrence of ch
(inclusive). If the character ch
does not exist in word
, do nothing.
- For example, if
word = "abcdefd"
andch = "d"
, then you should reverse the segment that starts at0
and ends at3
(inclusive). The resulting string will be"dcbaefd"
.
Return the resulting string.
Examples
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".
Constraints
1 <= word.length <= 250
word
consists of lowercase English letters.ch
is a lowercase English letter.
Intuition
In this problem we have to first determine if the given character ch
is present in the given word
. We can easily do that in 2 ways:
Iterate through each character in the
word
. Ifch
is found then store that index.Use inbuilt function like
find(ch)
to get the index ifch
is present in theword
.
After finding if ch
is present in the word
then we need to reverse the word from 0th index to index where ch is present
. Keeping index of ch to len(word) the same
. This can also be done in two ways:
Use two pointer to reverse the string from
0th
toch
index.Use String slicing
Solution
We will see two solutions to solve this problem:
- Two pointers:
def reverse_prefix(word, ch):
left = 0
# find index of ch
right = word.find(ch)
# if ch is not found then return -1
if right == -1:
return word
# convert word to an array
arr = list(word)
# iterate from 0th index to index of ch
while left < right:
# update left and right index
arr[left], arr[right] = arr[right], arr[left]
# go right by increasing the left pointer
left += 1
# go left by decreasing the right pointer
right -= 1
# in the end join arr to create a string
return ''.join(arr)
- String Slicing
class Solution:
def reversePrefix(self, word: str, ch: str) -> str:
# find position of ch in word
pointer=word.find(ch)
# if ch is not found pointer will be -1
if pointer==-1:
return word
else:
# reverse the word till pointer and append rest of the chars
return word[pointer::-1]+word[pointer+1:]
Time and Space Complexity
Time Complexity:
$$O(n)$$
Since at most we will iterate through word
only once in order to reverse it. This is true for both approaches.
Space Complexity:
- String slicing:
$$O(1)$$
As we don't use any auxiliary space other than the input provided.
- Two pointers
$$O(n)$$
As we convert the word to an array. So the space needed for array will be added to space complexity.
Conclusion
This is a pretty easy string based question. Once we obtain the index of ch
. We just need to reverse the string from 0th index to ch index. This can be done in two ways:
String Slicing
Two pointers
Hope you understood the concept and got something out of this blog.
If you have reached till here please like and comment.