class Solution { public int lengthOfLongestSubstring(String s) { int i=0,j=0,max=0; HashSet seen= new HashSet(); //sliding window-first stays,second expands //j is for all distinct charecters while(j
Thanks bro. It is my second day of searching solution of this problem but not able to understand it. you made me understand the logic and thank you for not making your code look fancy*...
great explanation But I coded it it without using second while loop ---------------------------------------- class Solution { public int lengthOfLongestSubstring(String st) { Set s = new HashSet();
int count = 0; int max = 0; int left = 0 ,right=0;
Here is my solution without using 2nd while loop : class Solution { public int lengthOfLongestSubstring(String s) { int right=0,left=0; Setst = new HashSet(); int max=0; while(right
good way to implement the idea, do 1 favour also please explain with dry run also,i was stuck in the last step when Left is at a and we have 2 b remaining
else { while (seen.contains(c)) { seen.remove(s.charAt(left)); left++; } } in the else part we can use inbuilt contains method of Set Following is the complete code public int lengthOfLongestSubstring(String s) { int left = 0; int right = 0; Set seen = new HashSet(); int max = 0; while (right < s.length()) { char c = s.charAt(right); if (seen.add(c)) { max = Math.max((right - left) + 1, max); right++; } else { while (seen.contains(c)) { seen.remove(s.charAt(left)); left++; } } } return max; }
@@thelazymim9338 yes bro cpp is like all are mixture not easily understandable Java syntax are lengthy but anyone can easily understood what can happen
Please anyone can explain this??? Input: s = "pwwkew" Output: 3 Explanation: The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
I think sliding window is the best solution because we are searching substring and we store count in array But try with stack if you can think of solution with it and send it to me
After spending 4hours in depression for understanding this concept, finally landed here...crisp and clear!
Same here
same
excellent !!! 5 min mei samj gya
great
for me no one explained this question better than you!
class Solution {
public int lengthOfLongestSubstring(String s) {
int i=0,j=0,max=0;
HashSet seen= new HashSet();
//sliding window-first stays,second expands
//j is for all distinct charecters
while(j
Very very brilliant answer
And how to print the longest string??
Thanks bro. It is my second day of searching solution of this problem but not able to understand it. you made me understand the logic and thank you for not making your code look fancy*...
Glad it helped
Brilliant explanation as always.
Greate Explanation in one pass only I understood all thanks bro!!!!
Nice explanations
Very good video. Please continue the great work..
very good explainetion
Thank you
great explanation But I coded it it without using second while loop
----------------------------------------
class Solution {
public int lengthOfLongestSubstring(String st) {
Set s = new HashSet();
int count = 0;
int max = 0;
int left = 0 ,right=0;
while(rightmax){
max=count;
}
}
return max;
}
}
but i think now runtime increases
actually a great explanation
Here is my solution without using 2nd while loop :
class Solution {
public int lengthOfLongestSubstring(String s)
{
int right=0,left=0;
Setst = new HashSet();
int max=0;
while(right
brilliant explanation bhaiya
Nice Explanation bhai
Really help full sir
best video so far!!
Well explained..🙌
Good work 👍
Your explanation is tooo good sir thank you 😊
Most welcome
Great content
very nice explanation. Thanks
You are welcome
What's substring of dvdf?
Do we really need nested while loop?
Awesome video, please keep it up.
Thanks, will do!
good way to implement the idea, do 1 favour also please explain with dry run also,i was stuck in the last step when Left is at a and we have 2 b remaining
Best explanation
Please do a video for Longest Repeating Character Replacement also
Very good video. Please continue the great work sir...
Thanks srinivas
class Solution {
public int lengthOfLongestSubstring(String s) {
int left = 0 , right = 0;
int maxLen = 0;
HashSet set =new HashSet();
while(righttrue
maxLen = Math.max(maxLen , right-left+1);
right++;
}else{
while(set.contains(c)){
set.remove(s.charAt(left));
left++;
}
}
}
return maxLen;
}
}
Although explanation is good but there is one correction brute force approach time complexity would be 0(n3)
I guess yes , if he consider substring function complexity
function getLongestSubstring(str){
let arr = str.split("");
let maxRes=''
let start =0;
let hashMap = new Map()
for(let end =0; end < arr.length; end++){
if(hashMap.has(arr[end]) && hashMap.get(arr[end]) >= start){
start = hashMap.get(arr[end]) +1
}
hashMap.set(arr[end], end);
let mystring = str.substring(start, end+1);
if(maxRes.length < mystring.length){
maxRes = mystring
}
}
return maxRes
}
If want to print the longest string then how it will with given code
Bro please upload your upcoming videos in English
else {
while (seen.contains(c)) {
seen.remove(s.charAt(left));
left++;
}
}
in the else part we can use inbuilt contains method of Set
Following is the complete code
public int lengthOfLongestSubstring(String s) {
int left = 0;
int right = 0;
Set seen = new HashSet();
int max = 0;
while (right < s.length()) {
char c = s.charAt(right);
if (seen.add(c)) {
max = Math.max((right - left) + 1, max);
right++;
} else {
while (seen.contains(c)) {
seen.remove(s.charAt(left));
left++;
}
}
}
return max;
}
Hey
class Solution {
public int lengthOfLongestSubstring(String s) {
Set st=new HashSet();
int left=0;
int right=0;
int max=0;
while(right
1st View 1st Comment 🎈
🙂 Thanks piyush
workable c++ easy solution:
class Solution {
public:
int lengthOfLongestSubstring(string s) {
unordered_set Set;
int left=0, right=0, maxlen = 0;
while(right < s.size()){
auto it = Set.find(s[right]);
if(it == Set.end()){
if(right-left+1 > maxlen){
maxlen = right-left+1;
}
Set.insert(s[right]);
right++;
}
else{
Set.erase(s[left]);
left++;
}
}
return maxlen;
}
};
Check time complexity?
If possible use cpp, most of the people prefer that for cp over any other language...btw nicely explained👍
Sorry Not Most People. Java syntax is easily readable.
@@thelazymim9338 yes bro cpp is like all are mixture not easily understandable Java syntax are lengthy but anyone can easily understood what can happen
No no Java
Hello I need help here
YOur return the length here , but if we want the string how should we take
Join discussion grp
return the HashSet object but again the function return type should change to HashSet from int.
Please anyone can explain this???
Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
because we have to find substring not subsequence. substrings are continuous while subsequence may not
isko stack se bhi kr sakte hai??
I think sliding window is the best solution because we are searching substring and we store count in array
But try with stack if you can think of solution with it and send it to me
time complexity O(n) nahi ho sakti aise
you are using nested while loop. How is time complexity o(n) here. it should be O(n*n)
yes
✅ Useful Links
𝐈𝐧𝐬𝐭𝐚𝐠𝐫𝐚𝐦 - instagram.com/codingwithprakash/
𝐖𝐡𝐚𝐭𝐬𝐀𝐩𝐩𝐂𝐡𝐚𝐧𝐧𝐞𝐥 - whatsapp.com/channel/0029VaACtTa4tRrpDCQc5f44
𝐋𝐢𝐧𝐤𝐞𝐝𝐢𝐧 - www.linkedin.com/in/prakash-shukla/