Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
anmolrishi#11 
to find largest sub string with no repetition
  • Loading branch information
somiljain7 authored Oct 27, 2018
1 parent d47883e commit 7a257d2
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions a.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

#include <bits/stdc++.h>

using namespace std;

string LongestSubstring(string str)
{
int i;
int n = str.length();


int st = 0;
int currlen;

int maxlen = 0;


int start;
unordered_map<char, int> pos;
pos[str[0]] = 0;

for (i = 1; i < n; i++) {


if (pos.find(str[i]) == pos.end())
pos[str[i]] = i;

else {

if (pos[str[i]] >= st) {

currlen = i - st;
if (maxlen < currlen) {
maxlen = currlen;
start = st;
}


st = pos[str[i]] + 1;
}


pos[str[i]] = i;
}
}


if (maxlen < i - st) {
maxlen = i - st;
start = st;
}


return str.substr(start, maxlen);
}

int main()
{
string str = "somiljain";
cout <<LongestSubstring(str);
return 0;
}

0 comments on commit 7a257d2

Please sign in to comment.