-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDAY0074.cpp
More file actions
37 lines (37 loc) · 1.04 KB
/
DAY0074.cpp
File metadata and controls
37 lines (37 loc) · 1.04 KB
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
// 3403. Find the Lexicographically Largest String From the Box I
class Solution {
public:
string answerString(string word, int numFriends) {
// int size=word.size();
// if(numFriends==1) return word;
// string answer="";
// for(int i=0;i<size;i++){
// if(word.substr(i,size-numFriends+1)>answer){
// answer=word.substr(i,size-numFriends+1);
// }
// }
// return answer;
if(numFriends==1) return word;
const size_t size=word.length()-numFriends+1;
const string str=subString(word);
return str.substr(0,min(str.length(),size));
}
string subString(string s){
int i=0,j=1,k=0;
while(j+k<s.length()){
if(s[i+k]==s[j+k]){
k++;
}
else if(s[i+k]>s[j+k]){
j=j+k+1;
k=0;
}
else{
i=max(i+k+1,j);
j=i+1;
k=0;
}
}
return s.substr(i);
}
};