Комментарии:
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!!!!!!!!!!!!
Ответитьwhy we are removing the map
ОтветитьSir I written same program it is giving wrong answer if I pass string "aabb" it rutering 1 insted of 2.....?
Ответитьnice logic
ОтветитьThank you sir this was amazing series on string!
ОтветитьSuper Logic sir
Ответитьpublic static int findL(String s) {
int l = s.length();
int a_pointer = 0;
int b_pointer = 0;
int max = 0;
HashSet<Character> 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;
}
else block is not clear , why are you getting index value and setting it to i
ОтветитьMap will clear whole values inside Map??
Ответитьpublic static void main(String[] args){
String str = "abcbdaac";
String longestSubString = "";
int maxLength = 0;
for(int i=0; i<str.length(); i++){
for(int j=i; j<str.length(); j++){
if(!isRepeatedCharacterPresent(str.substring(i, j+1))){
if(str.substring(i, j+1).length() > maxLength){
longestSubString = str.substring(i, j+1);
maxLength = str.substring(i, j+1).length();
}
}
}
}
System.out.println("Longest substring without repeated char is: "+longestSubString);
System.out.println("Length: "+maxLength);
}
public static boolean isRepeatedCharacterPresent(String str){
char[] arr = str.toCharArray();
for(int i=0; i<arr.length; i++){
for(int j=i+1; j<arr.length; j++){
if(arr[i]==arr[j])
return true;
}
}
return false;
}
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.
ОтветитьHi, sir while explaining use debugging mode so that it will easy to understand more clearly. Anyways, nice tutorial.
Ответитьwow ur awesome sir
ОтветитьThanks
Ответить