Problem 2000 Reverse Prefix of Word
Table of Contents
Problem Statement
Link - Problem 2000
Question
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"
and ch = "d"
, then you should reverse the segment that starts at 0 and ends at 3 (inclusive). The resulting string will be "dcbaefd"
.
Return the resulting string.
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.
Solution
class Solution {
public:
string reversePrefix(string word, char ch) {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int prefixIndex = INT_MIN;
for(int i = 0;i<word.size();i++)
{
if(word[i]==ch)
{
prefixIndex = i;
break;
}
}
if(prefixIndex == INT_MIN)\
return word;
string ans = "";
for(int i = prefixIndex;i>=0;i--)
ans += word[i];
for(int i = prefixIndex+1;i<word.size();i++)
ans += word[i];
return ans;
}
};
Complexity Analysis
Time
- O(N)Space
- O(N)
Explanation
- Iterate through the string and check if the character
ch
exists inword
. - If not return the
word
as it is. - If it exisits swap construct a new string
ans
in the following fashion. - Append characters from
prefixIndex
to0
, then append characters fromprefixIndex +1
toword.size()
. - Return the
ans
string.
Note
- A solution using STL functions is as follows
class Solution {
public:
string reversePrefix(string word, char ch) {
int j = word.find(ch);
if (j != -1) {
reverse(word.begin(), word.begin() + j + 1);
}
return word;
}
};
Showcases the string manipulation.