L3. Longest Substring Without Repeating Characters | 2 Pointers and Sliding Window Playlist

preview_player
Показать описание


Рекомендации по теме
Комментарии
Автор

whenever I am looking for an explanation to a problem and I search it on youtube and if I don't find a solution by striver I get disappointed...Love from Flipkart!

kuldeepdixit
Автор

Hello Bhaiya!!
From past few weeks i was not able to watch any of your videos.
Was going through depression .

But the moment i joined the journey again with you is a blessing.
You have a different aura ....just looking at you i just simply forget everything and a new spark is ignited within me.

Thank alot Bhaiya for your efforts and helping me!!

hashcodez
Автор

The new look of the DSA sheet and the whole website is just way too awesome sir.

shwetanshusinha
Автор

I just cant express my gratitude in words towards this man . the way he simplifies every single thing makes everything appear so easy . I am so glad that I found this channel.

pavitrasingh
Автор

hi Striver,
Your Graph, DP, binary search playlist are amazing. Please create a playlist for String as well.

shivamjaiswal
Автор

I solved it on my own! Your teaching is amazing bhaiya!!!

simple_user
Автор

Hi Striver, I'm not sure if this comment will reach you or not, but I just want to say one thing: I've been watching your videos for more than a year now and have covered a huge part of the graph and dynamic programming playlists. Whenever I watch your videos, I really fall in love with your teaching style, and of course, your smile, and sometimes your small jokes. I enjoy your videos, and it feels like you're not just my tutor; it feels like you're someone very close to me, teaching me with fun and pleasure. However, in your recent playlists, I totally miss that. It feels like you're not as friendly anymore; you're just a teacher like any other tutor. Whenever I watch videos from this playlist, sometimes I wonder what happened to you. Why are you so quiet now? Why don't you joke around anymore? After all, I love your work; it's just my opinion.

tanvirahammed
Автор

1 fix in the brute force is that you need to fill the arrays with 0 every time youre moving i. so heres the correctedd brute.public int s) {
int[] arr = new int[256];
int max = 0;
int n = s.length();

for (int i = 0; i < n; i++) {
// Reset the array for each new starting point
Arrays.fill(arr, 0);

for (int j = i; j < n; j++) {
// If character at j has been seen, break the loop
if (arr[s.charAt(j)] == 1) {
break;
}

// Otherwise, add the character to substring
int len = j - i + 1;
max = Math.max(len, max);
// Remember it
arr[s.charAt(j)] = 1;
}
}
return max;
}

rsiszkk
Автор

Loved your explaination. Bhaiya instead of using a HashMap we can also use a Set. It will save some more memory.
class Solution {
public int s) {
int i = 0, j = 0, max = 0;
Set<Character> set = new HashSet<>();
while (j < s.length())
{
if (!set.contains(s.charAt(j)))
{
set.add(s.charAt(j));
j++;
max = Math.max(max, set.size());
}
else
{
set.remove(s.charAt(i));
i++;
}
}
return max;
}
}

divyanshushukla
Автор

i think this is overcomplicating things. The easy code could be:
int n = s.length();
int left = 0;
int right = 0;
int maxLen = 0;
map<char, int> mpp;
int len=0;
while (right < n) {
if (mpp.find( s[right] ) != mpp.end() ) {
left++;
right=left;
mpp.clear();

}
else{
len++;
mpp[s[right]] = right;
maxLen=max(right-left+1, maxLen);
right++;
}

}
return maxLen;

AyushEditz-hspf
Автор

You are unstoppable... 🙏🙏 You are the best 🙏🙏

_sujatachandra
Автор

Bhaiya mene isko freq array banakar easy way me kardiya.
class Solution {
public:
int s) {
vector<int> freq(256, 0);

int i = 0, j = 0;
int maxi = 0;
int n = s.length();

while (j < n) {
if (freq[s[j]] == 0) {
freq[s[j]]++;
maxi = max(maxi, j - i + 1);
j++;
} else {
freq[s[i]]--;
i++;
}
}
return maxi;
}
};

technologicalvivek
Автор

with this solution we can make time complexity as O(n) and space complexity O(n)
Set<Character> set=new HashSet<>();

int left=0;
int right=0;
int mxLen=0;
String result = "";
while(right<str.length()) {
{
set.add(str.charAt(right));
if(right-left+1 >mxLen) {
mxLen= right- left +1;
result=str.substring(left, right+1);
}
right++;
}else {

left++;

}
}
return result;
}

heershah
Автор

I think you haven't attached this youtube link in your take youforward site.

adilkevin
Автор

East and west striver bhaiya is best ❤❤

monikayadav-wbpu
Автор

00:06 Finding longest substring without repeating characters
02:46 Using 2 Pointers for Substring Generation
04:59 Using hashing to find the longest substring without repeating characters
07:36 Optimizing algorithm using two pointers and sliding window approach
10:02 Understanding two pointer and sliding window approach
12:17 Determine longest substring without repeating characters using hashmap and sliding window
14:45 Updating characters in a sliding window to find longest substring without repeats.
17:06 Sliding window technique for finding longest substring without repeating characters.
19:10 Algorithm explanation and time complexity analysis
21:42 Explanation of sliding window algorithm with two pointer

PrinceKumar-efxf
Автор

Bhaiya wala code { BY USING MAP NAAM KI ARRAY } :--
class Solution {

public int s) {
int l=0;
int r=0;
int[] map=new int[256];
Arrays.fill(map, -1);
int maxlen=0;
while(r<s.length()){
if(map[s.charAt(r)]==-1){
map[s.charAt(r)]=r; //Now index of map will represent character aur value represent karegi index of string ko.
}
else{
l=Math.max(l, map[s.charAt(r)]+1);
map[s.charAt(r)]=r;
}
maxlen=Math.max(maxlen, r-l+1);
r++;
}
return maxlen;

}
}

HASHMAP WALA CODE :---
int l=0;
int r=0;
HashMap <Character, Integer> mpp=new HashMap<>();
int[] mpp=new int[256];
Arrays.fill(mpp, -1);
int maxlen=0;
while(r<s.length()){
// agar map m value exist nhi krti
mpp.put(s.charAt(r), r);

}
else{
// l=mpp.get(s.charAt(r))+1;
l = Math.max(l, mpp.get(s.charAt(r)) + 1); // iska reason video m hi battaya h
mpp.put(s.charAt(r), r);

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

}

ARRAY WALA CODE JYADA FAST HOGA HASHMAP WALE SE🙃

ManishKumar-dkhl
Автор

vector< int > mpp(256, -1);
int l=0;
int r=0;
int maxlength=0;
while(r<s.size()) {
if(mpp[s[r]]!=-1) {
if(mpp[s[r]]>=l) l = mpp[s[r]] +1;
}

mpp[s[r]] = r;
maxlength = max(maxlength, r-l+1);
r++;
}
return maxlength;

Divyanshu-qn
Автор

Very, very smart case where left has to be the rightmost. eg checking in abc, if left is already at 4 and prior presence of b is at 2, we need not update left to 3, instead it should be max of prior instance+1, already present left

theee_
Автор

int s)
{
if(s.size()==0)return 0;
int i=0;
int j=0;
int maxi=1;
int n=s.size();
unordered_map<char, int>mp;
while(j<n)
{

{
i=mp[s[j]]+1;
}
maxi=max(maxi, j-i+1);
mp[s[j]]=j;
j++;
}
return maxi;

}
finally accepted.
Thankyou Striver😊for your effort.
i like and appreciate you from bottom of my heart.❤

aniketkumar