Skip to content

Commit

Permalink
Merge pull request #524 from priya-19054/main
Browse files Browse the repository at this point in the history
Long_Substr_w/o_Repeat_Char
  • Loading branch information
gantavyamalviya committed Oct 5, 2022
2 parents a7116d6 + 99688b0 commit 929cd42
Showing 1 changed file with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
public:
int lengthOfLongestSubstring(string str) {
int n = str.size();
map<char, int> mp;
int maxm = 0, prev = -1;

for (int i = 0; i < n; i++)
{
if (mp.count(str[i]) == 0)
{
mp.insert({str[i], i});
}
else
{
prev = max(prev, mp[str[i]]);
mp[str[i]] = i;
}
maxm = max(maxm, i - prev);
}
return maxm;
}
};

// The idea is checking whenever we find any letter which is repeating we can find previous position and find the diffrence
// we can check "repeating" by maintain hash functions like map

0 comments on commit 929cd42

Please sign in to comment.