-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #524 from priya-19054/main
Long_Substr_w/o_Repeat_Char
- Loading branch information
Showing
1 changed file
with
26 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
Leetcode-solutions/Binary-search/Longest Substring Without Repeating Characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |