Java Program To Find Longest Substring Without Repeated Character | Ashok IT

Java Program To Find Longest Substring Without Repeated Character | Ashok IT

Ashok IT

4 года назад

45,687 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@gaddamrishikasri2238
@gaddamrishikasri2238 - 18.05.2023 13:44

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!!!!!!!!!!!!

Ответить
@RichaBehera129
@RichaBehera129 - 06.04.2023 06:31

why we are removing the map

Ответить
@maheshmagarde2143
@maheshmagarde2143 - 22.12.2022 18:42

Sir I written same program it is giving wrong answer if I pass string "aabb" it rutering 1 insted of 2.....?

Ответить
@NishantKumar0786
@NishantKumar0786 - 08.11.2022 20:48

nice logic

Ответить
@vaibhavnirmal1
@vaibhavnirmal1 - 29.10.2022 10:16

Thank you sir this was amazing series on string!

Ответить
@PBtelugu
@PBtelugu - 14.08.2022 09:57

Super Logic sir

Ответить
@manishjoshi539
@manishjoshi539 - 11.07.2022 12:02

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;
}

Ответить
@rakesh-lw6qt
@rakesh-lw6qt - 18.06.2022 19:01

else block is not clear , why are you getting index value and setting it to i

Ответить
@my_love_sanatan
@my_love_sanatan - 01.12.2021 08:54

Map will clear whole values inside Map??

Ответить
@amithens792
@amithens792 - 22.11.2021 19:17

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;
}

Ответить
@ayushsingh3967
@ayushsingh3967 - 07.10.2021 17:54

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.

Ответить
@kogulselvanathan4218
@kogulselvanathan4218 - 09.06.2021 20:23

Hi, sir while explaining use debugging mode so that it will easy to understand more clearly. Anyways, nice tutorial.

Ответить
@maheshbabu9909
@maheshbabu9909 - 29.03.2021 09:53

wow ur awesome sir

Ответить
@rahulsingh-jg9wt
@rahulsingh-jg9wt - 23.02.2021 11:22

Thanks

Ответить