Skip to content

Commit

Permalink
Largest string without repetition
Browse files Browse the repository at this point in the history
For every letter looks how far it can go without repeating using previus resoults
  • Loading branch information
NolkMan committed Oct 27, 2018
1 parent d47883e commit 0813290
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Arrays/largest_substr_wo_repetition.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <iostream>

int count [257];

std::string findLargestSubstr(const std::string &str){
// start of current substr, and end of current substr
int s = 0, e = 0;
int maxs=0, maxe=0;

while (s < str.size()){
while (count[str[e]] == 0 && e < str.size()){
count[str[e]] ++;
e++;
}
if (e-s > maxe - maxs){
maxs = s;
maxe = e;
}
count[str[s]] --;
s++;
}

return str.substr(maxs, maxe - maxs);
}

int main(){
std::cout << "Input string: \n";
std::string str;
std::cin >> str;

std::cout << findLargestSubstr(str) << "\n";

return 0;
}

0 comments on commit 0813290

Please sign in to comment.