Longest Substring Without Repeating Characters | Leetcode 3 | Sliding window
Vložit
- čas přidán 6. 09. 2024
- Time Complexity : O(n)
Space Complexity : O(1)
Problem Link : leetcode.com/p...
C++ Code Link : github.com/Ayu...
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
🔥🔥🔥🔥👇👇👇
Join telegram channel for more updates on placement preparation : t.me/AyushiSha...
Checkout the series: 🔥🔥🔥
👉Interview Experiences : • Interview Experiences
👉 Array: • Arrays
👉 Linked List : • Linked List
👉 Heap : • Heap
👉 Recursion : • Recursion
👉 Stack and Queue : • Stack And Queue
👉 Greedy : • Greedy
👉 Dynamic Programming : • Dynamic Programming
👉 Leetcode contests : • Leetcode contests
👉 Leetcode June Challenge : • Leetcode June Challenge
👉 Leetcode July Challenge : • Leetcode July Challenge
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;
}
thank you for sharing your code and helping others :)
after wasting approx 5 hours on you tube i found the genuine logic here, thank you mam. greaat....
glad it was helpful :)
This can help someone :)
class Solution {
public:
int lengthOfLongestSubstring(string s) {
//creating a set to store the list
unordered_set ans;
int value=0;
int i=0,j=0;
while(i
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).
After wasting a lot of time i found A helpful video .
thanks❤❤
welcome, glad u found it helpful :)
I have already done this problem using hashmap.but without sliding window.thank you for this optimization of code
Welcome Venkat, glad it was helpful :)
can u explain your hashmap approach ?
hi aushi, using two while loop may increase time complexity else we can use hash map for storing index of characters
Waiting for Chennai vlog part 2 😍
❤️❤️🙌🏻☺️
int lengthOfLongestSubstring(string s)
{
unordered_map umap;
int n = s.length();
int ans=0;
int start=0;
for (int i = 0; i < n; i++) {
if(umap.find(s[i])!=umap.end()){
// 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;
}
Time complexity worst case will be O(n^2) right? Coz two while loops
crystal clear explanation mam 👍👍
thank you :)
Solve Today's Leetcode Challenge(Erasure Problem,) by using this approach, thankyou keep up the good work
Thanks ☺️
for java coders, there will be slight changes:-
public int lengthOfLongestSubstring(String 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;
}
Thanks ma'am
Most welcome 😊
Di should i buy coding ninja dsa course or study from free resource . pls response
The course is good and you can learn from free resources also, so it totally depends on you :)
@AyushiSharmaDSA Your solution seems better then what available in general platform. What resource you have used to train yourself, could u make a video of it or if already there then link pls?
In gfg this code is not working
This code gives wrong answer for aabccbb
Great explanation
Thanks Jai :)
it is giving runtime error for string containing space
The solution for the test case 'pwwkew' is not giving the correct answer.
very well explained ...........100/100
thank you :)
int lengthOfLongestSubstring(string s) {
unordered_mapmp;
int i=0,j=0,ans=0;
int n=s.length();
while(j1){
mp[s[i]]--;
i++;
}
ans=max(ans,j-i+1);
j++;
}
return ans;
}
};
Thanks di
Welcome Aman 😊
how is taking count[a] ?
Thank for crisp solution 😌😌
Welcome Kishor, glad it was helpful 😊
Nice video
Thanks Harish :)
that code gives wrong ans in leetcode
Nice explanation
Nice Explanation
Try this also.
Longest Repeating Character Replacement
Thanks, sure 😊
Can we take char array of 128 size??
very well explained!
Thank you , Glad it was helpful!
👍👍
😊😊
Java code is not working in testcase "pwwkew". can any one help me.
public int lengthOfLongestSubstring(String 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;
}
In second while loop it's count[s.charAt(i)] not index. And why are you taking extra variable index
This solution is not working for pwwkew case
@@abhisheknavhal1039 in line 8, it will be ************** count[s.charAt(i)]--; ******************************will be there
Thanks a ton!
Welcome :)
i am waiting for this
Thanks Dhanraj :)
You're best😁
Thanks Deeptimayee 😊
👍👍👍👍👍👍👍👍👍👍👍👍👍👍
Thanks Amit :)
thanks
]
welcome :)
Didi if possible pls give java code also
You could've just took i and j. Why did you made it look difficult. We are beginners that's why we are here watching your videos. Please take care of it next time. Nice Explanation tho
Hindi Java
czcams.com/video/hG2Fg6FlRA0/video.htmlsi=DlUeAE29pJsP5viX
please make in hindi we are more comfortable in it .
nice explanation
thank you, glad you liked it :)
thanks ma'am
Welcome 🤗
thanku di
Welcome Kunal :)