Longest Substring Without Repeating Characters | Leetcode 3 | Sliding window

preview_player
Показать описание
Time Complexity : O(n)
Space Complexity : O(1)


Please like, share and subscribe if you found the video useful. Feel free to ask in comments section if you have any doubts. :)

#DataStructuresAndAlgorithms
#Leetcode
#interviewpreparation
#AyushiSharma
Longest Substring Without Repeating Characters solution
Longest Substring Without Repeating Characters Leetcode
Longest Substring Without Repeating Characters C++
Longest Substring Without Repeating Characters Java
Longest Substring Without Repeating Characters Python

🔥🔥🔥🔥👇👇👇

Checkout the series: 🔥🔥🔥
LIKE | SHARE | SUBSCRIBE 🔥🔥😊
Рекомендации по теме
Комментарии
Автор

Thanks. Here is the Java code if someone needs it -

public int maxSubString(String s) {
int[] count = new int[256];
int l = 0;
int r = 0;
int ans = 0;

while (r < s.length()) {
char currChar = s.charAt(r);
count[currChar]++;

while (count[currChar] > 1) {
count[s.charAt(l)]--;
l++;
}

ans = Math.max(ans, r - l + 1);
r++;
}
return ans;
}

jasper
Автор

after wasting approx 5 hours on you tube i found the genuine logic here, thank you mam. greaat....

azhargayawi
Автор

This can help someone :)



class Solution {
public:
int s) {
//creating a set to store the list
unordered_set<char> ans;
int value=0;
int i=0, j=0;
while(i<s.length() && j<s.length()){
if(ans.find(s[j])==ans.end())
//if the character is not present in the list then we insert the char into the list and update the right pointer.
{
ans.insert(s[j++]);
//we update the length of the current longest substring
value=max(value, j-i);
}else{
//if the character is found for the second time then we stop the process and increase the left pointer by 1 by erasing the left most character.
ans.erase(s[i++]);
}
}
//returning the length of the longest substring
return value;
}
};

cs_soldier
Автор

I have already done this problem using hashmap.but without sliding window.thank you for this optimization of code

venkatvasamsetti
Автор

well explained solution, in particular I like your making use of blackboard to go over approach to solution first, helps us a lot. 😀

RajeshS-nj
Автор

for java coders, there will be slight changes:-


public int s) {
int[] arr = new int[128];
int n = s.length();
int len = 0;
for(int i = 0, j = 0; j < n; j++){
int c = s.charAt(j);
arr[c]++;
if(arr[c] > 1){
while(arr[c] > 1){
arr[s.charAt(i)]--;
i++;
}
}
len = Math.max(len, j - i + 1);
}
return len;
}

harshvardhan
Автор

After wasting a lot of time i found A helpful video .
thanks❤❤

FootB_shorts
Автор

Solve Today's Leetcode Challenge(Erasure Problem, ) by using this approach, thankyou keep up the good work

BADASSBLADE
Автор

int s)
{
unordered_map<char, int> umap;
int n = s.length();
int ans=0;
int start=0;

for (int i = 0; i < n; i++) {

// update the start index to the next character after the last occurrence
start = max(start, umap[s[i]] + 1);
}
umap[s[i]] = i;
ans=max(ans, i-start+1);
}
return ans;
}

luckilygamer
Автор

Nice Explanation
Try this also.
Longest Repeating Character Replacement

arqamazmi
Автор

Good explanation but I guess the space complexity mentioned in the description should be O(k), where k are the unique characters and not O(1).

coding
Автор

hi aushi, using two while loop may increase time complexity else we can use hash map for storing index of characters

Darshan-prk
Автор

Di should i buy coding ninja dsa course or study from free resource . pls response

abhishekprasad
Автор

it is giving runtime error for string containing space

jagmohangautam
Автор

Java code is not working in testcase "pwwkew". can any one help me.

public int s) {

int count[] = new int[256];

int maxLen = 0, i=0, j=0;

while(j < s.length()){
int index = s.charAt(j);
count[index]++;

while(count[index] > 1){
count[index]--;
i++;
}

maxLen = Math.max(maxLen, ( j-i)+1);

j++;
}

return maxLen;
}

sumitpal
Автор

Time complexity worst case will be O(n^2) right? Coz two while loops

priyankakataria
Автор

int s) {
unordered_map<char, int>mp;
int i=0, j=0, ans=0;
int n=s.length();
while(j<n){
mp[s[j]]++;
while(mp[s[j]]>1){
mp[s[i]]--;
i++;
}
ans=max(ans, j-i+1);
j++;
}
return ans;
}
};

vinaykulkarni
Автор

The solution for the test case 'pwwkew' is not giving the correct answer.

abhisheknavhal
Автор

This code gives wrong answer for aabccbb

vamsiv
Автор

Didi if possible pls give java code also

NikhilKumar-brnj