🔥 Welcome to Ashok IT..!! 👉 Register Here For Online Training : bit.ly/4dBYJbX 👉 Follow us in Instagram : bit.ly/3jzEKl8 👉 Follow Us in WhatsApp : bit.ly/49wCWje 👉 Visit Our Website : ashokit.in/
public static int findL(String s) { int l = s.length(); int a_pointer = 0; int b_pointer = 0; int max = 0; HashSet hashSet = new HashSet(); while (b_pointer < l) { if (!hashSet.contains(s.charAt(b_pointer))) { hashSet.add(s.charAt(b_pointer)); b_pointer++; max = Math.max(hashSet.size(), max); } else { hashSet.remove(s.charAt(a_pointer)); // a_pointer++; // to iterate to next pointer since the current pointer is removed in the previous step } } return max; }
Your code would get fail if input string is like s ="abcbdaac" because you'r replacing the i value with map.get(ch) but in this case i should be replaced as map.get(ch) -1 as it has to include character "c" when second b is get encountered.
Ther's no problem with the code WRT to your input i.e "abcbdaac" I'm getting the correct output longest substring is : [c, b, d, a] longest substring length is : 4 NOTE: based on your input ---> i = map.get(ch); duplicate 'b' found at 3rd index but from the String first time 'b' occurred at 1st index and due to i++ loop control will be increased to i=2 and at second index we have 'c' finally getting the output as expected.
@@thiestmayank can you share your code, it's failing for me with input abcbdaac import java.util.*; class Main { public static void main(String[] args) { String str="abcbdaac"; char ch[]=str.toCharArray(); HashMapmap=new LinkedHashMap(); for(int i=0;i
the code will fail for ip:abac it gives 2 as op but the correct ans is 3 beacuse bac is longest sub string with no repeated character ...in your code we are not taking previous character!!!!!!!!!!!!
🔥 Welcome to Ashok IT..!!
👉 Register Here For Online Training : bit.ly/4dBYJbX
👉 Follow us in Instagram : bit.ly/3jzEKl8
👉 Follow Us in WhatsApp : bit.ly/49wCWje
👉 Visit Our Website : ashokit.in/
Excellent explanation.Esay approach.Thank you.
You are welcome!
public static int findL(String s) {
int l = s.length();
int a_pointer = 0;
int b_pointer = 0;
int max = 0;
HashSet hashSet = new HashSet();
while (b_pointer < l) {
if (!hashSet.contains(s.charAt(b_pointer))) {
hashSet.add(s.charAt(b_pointer));
b_pointer++;
max = Math.max(hashSet.size(), max);
} else {
hashSet.remove(s.charAt(a_pointer)); //
a_pointer++; // to iterate to next pointer since the current pointer is removed in the previous step
}
}
return max;
}
it's only good when you want an int type return value but this program won't be able to print the subarray string.
Thank you sir this was amazing series on string!
Really helpful :), thanks for this video.
Please contact our admin team : 9985396677
Your code would get fail if input string is like s ="abcbdaac" because you'r replacing the i value with map.get(ch) but in this case i should be replaced as map.get(ch) -1 as it has to include character "c" when second b is get encountered.
Even replacing it by map.get(ch) -1 doest yeild the proper output
Ther's no problem with the code WRT to your input i.e "abcbdaac" I'm getting the correct output
longest substring is : [c, b, d, a]
longest substring length is : 4
NOTE: based on your input ---> i = map.get(ch); duplicate 'b' found at 3rd index but from the String first time 'b' occurred at 1st index
and due to i++ loop control will be increased to i=2 and at second index we have 'c'
finally getting the output as expected.
@@thiestmayank can you share your code, it's failing for me with input abcbdaac
import java.util.*;
class Main {
public static void main(String[] args) {
String str="abcbdaac";
char ch[]=str.toCharArray();
HashMapmap=new LinkedHashMap();
for(int i=0;i
Hi, sir while explaining use debugging mode so that it will easy to understand more clearly. Anyways, nice tutorial.
Super Logic sir
import java.util.LinkedHashMap;
import java.util.Map;
class Solution {
public static void lengthOfLongestSubstring(String s) {
String longestSubstring = null;
int lengthOfLongestSubstring = 0;
Map map = new LinkedHashMap();
char[] arr = s.toCharArray();
int start = 0;
for (int i = 0; i < arr.length; i++) {
char ch = arr[i];
if (map.containsKey(ch)) {
start = Math.max(start, map.get(ch) + 1);
}
map.put(ch, i);
if (i - start + 1 > lengthOfLongestSubstring) {
lengthOfLongestSubstring = i - start + 1;
longestSubstring = s.substring(start, i + 1);
}
}
System.out.println(lengthOfLongestSubstring);
System.out.println(longestSubstring);
}
public static void main(String[] args) {
lengthOfLongestSubstring("rsdhghkhjdrsbng");
}
}
proper working code for all string inputs
🔥 Please follow us in WhatsApp channel : bit.ly/49wCWje
Thanks
wow ur awesome sir
nice logic
Map will clear whole values inside Map??
Yes, it should otherwise repeated chars will be strored
the code will fail for ip:abac it gives 2 as op but the correct ans is 3 beacuse bac is longest sub string with no repeated character ...in your code we are not taking previous character!!!!!!!!!!!!
Please Contact Our Admin Team:wa.me/+919985396677
👉 Subscribe To Our TH-cam Channel: bit.ly/41IHJdj
This code is failing.
Kinda too time taking approach, but working
String s1 = "abac";
String longestStr = "";
String currentStr = "";
Set charSet = new LinkedHashSet();
for(int i = 0; i < s1.length(); i++) {
for(int j = i; j < s1.length(); j++) {
char ch = s1.charAt(j);
if( !charSet.contains(ch) ) {
charSet.add(ch);
}
else if( charSet.contains(ch) )
{
currentStr = charSet.toString();
if(currentStr.length() > longestStr.length()) {
longestStr = currentStr;
}
charSet.clear();
currentStr = "";
break;
}
}
}
System.out.println(longestStr);
else block is not clear , why are you getting index value and setting it to i
If char repeated we should clear it otherwise we may get chars repeated and here taking index to start freshly to check for substring
Even I got the same question ? It's not working fine.
@@gauravpalkar3885true
Hi Sir,
This code is not working:
public static void main(String [] args) {
lengthoflongestsubstring("java");
}
public static void lengthoflongestsubstring(String s) {
String longestsubstring = null;
int longestsubstringlength = 0;
Map map = new LinkedHashMap();
char [] arr = s.toCharArray();
for (int i = 0; i < arr.length; i++) {
char ch = arr[i];
if (!map.containsKey(ch)) {
map.put(ch, i);
}
else {
i= map.get(ch);
map.clear();
}
if (map.size()>longestsubstringlength) {
longestsubstringlength = map.size();
longestsubstring = map.keySet().toString();
}
}}
please contact our admin team : +91 9985396677
i am not able to understand what else part doing
please contact our admin team : +91 9985396677
why we are removing the map
Please Contact Our Admin Team:wa.me/+919985396677
👉 Subscribe To Our TH-cam Channel: bit.ly/41IHJdj
Sir I written same program it is giving wrong answer if I pass string "aabb" it rutering 1 insted of 2.....?
The answer is 1 only since it should be "without repeating the characters " . Here "aa" substring is invalid. The correct answer is "a"
answer is 2 which is ab
Yes it is giving wrong result. I changed the program and its now working fine.
This code wont work for string ABCDEFGABEF
🔥 Please follow us in WhatsApp channel : bit.ly/49wCWje