-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion63.cpp
44 lines (31 loc) · 875 Bytes
/
question63.cpp
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<bits/stdc++.h>
using namespace std;
class Solution {
public:
string reversePrefix(string word, char ch) {
vector<char> prefix;
bool found = false;
for(auto i : word){
prefix.push_back(i);
if(i == ch){
found = true;
break;
}
}
if(!found) return word;
reverse(prefix.begin(), prefix.end());
string result;
for(auto i : prefix){
result.push_back(i);
}
for(int i = prefix.size(); i < word.size(); i++){
result.push_back(word[i]);
}
return result;
// 2nd approach to solve this question
// int pos = word.find(ch);
// if(pos < 0) return word;
// reverse(word.begin(), word.begin() + pos + 1);
// return word;
}
};