mirror of
https://github.com/Manoj-HV30/dsa-competitive-programming.git
synced 2026-05-16 19:35:22 +00:00
Leetcode day1
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
class Solution {
|
||||
public:
|
||||
int lengthOfLongestSubstring(string s) {
|
||||
int n =s.length();
|
||||
vector<int>charIndex(128,-1);
|
||||
int left = 0;
|
||||
int maxlen =0;
|
||||
for(int right =0;right<n;right++){
|
||||
if(charIndex[s[right]]>=left){
|
||||
left = charIndex[s[right]]+1;
|
||||
}
|
||||
charIndex[s[right]] = right;
|
||||
maxlen = max(maxlen, right-left+1);
|
||||
}
|
||||
return maxlen;
|
||||
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,22 @@
|
||||
class Solution{
|
||||
public:
|
||||
int LengthOfLongestSubString(string s){
|
||||
int n = s.length();
|
||||
int maxlen = 0;
|
||||
unordered_set<char> charset;
|
||||
int left = 0;
|
||||
|
||||
for(int right = 0;right<n;right++){
|
||||
if(charset.count(s[right])==0){
|
||||
charset.insert(s[right]);
|
||||
maxlen = max(maxlen, right-left+1);
|
||||
}while(charset.count(s[right])){
|
||||
charset.erase(s[left]);
|
||||
left++;
|
||||
}
|
||||
}
|
||||
return maxlen;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
My first sliding window problem.
|
||||
Sliding Window is an algorithmic technique used to process a contiguous subarray or substring by maintaining a moving window over the input, where thewindow expands and/or shrinks while traversing the data to efficiently compute results in linear time.
|
||||
|
||||
When to use Sliding Window :
|
||||
|
||||
>Subarray / substring problems
|
||||
|
||||
>Problems asking for:
|
||||
|
||||
1)longest / shortest window
|
||||
|
||||
2)maximum / minimum sum
|
||||
|
||||
3)unique elements in a range
|
||||
|
||||
4)Continuous range constraints
|
||||
Reference in New Issue
Block a user