Java Program To Find Longest Substring Without Repeated Character | Ashok IT
ฝัง
- เผยแพร่เมื่อ 5 ก.ย. 2024
- #java #javascript #javaprogrammer #javaprogrammig #javadevelopment #ashokit
✍️✍️ Register Here For Online Training : bit.ly/4dBYJbX
** For Online Training ► Call: +91-6301921083
Subscribe to our channel and hit the bell 🔔🔔🔔 icon to get video updates.
💡 Visit Our Website
For Online Training: www.ashokit.in
💡 About Ashok IT :
Ashok IT is the No.1 quality training institute in India for the candidates who want to build their future in Information Technology. We are into online training, class room training, corporate training and one to one training with more passion and dedication. Ashok IT aims in providing best quality realtime oriented trainings on C, C++, Java, Spring , Spring REST, Spring Cloud, Microservices, Python, DJango, .Net, Angular, React JS, Salesforce, , Testing, Android, Docker, Kubernates, Manual Testing, Selenium and Digital Marketing.
-----------------------------------------------------------------------------------
💡 Our Online Training Features
🎈 Training with Real-time Working Professionals
🎈 Industry Matching use cases
🎈 Live Coding
🎈 Real-time Environment
🎈 Class Notes
🎈 Doubts Clarifications in Each Session
-----------------------------------------------------------------------------------
💡 Contact details:
☎ WhatsApp Number: +91-6301921083
► Website : www.ashokit.in
► Join with us in Telegram : bit.ly/3iP2KyA
► Like us in Facebook : bit.ly/3dw6R0A
► Follow us in Instagram : bit.ly/3jzEKl8
► Follow us in Twitter : bit.ly/2SDqdK4
🔥 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/
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.
Excellent explanation.Esay approach.Thank you.
You are welcome!
Thank you sir this was amazing series on string!
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.
Thanks
Hi, sir while explaining use debugging mode so that it will easy to understand more clearly. Anyways, nice tutorial.
Map will clear whole values inside Map??
Yes, it should otherwise repeated chars will be strored
wow ur awesome sir
Super Logic sir
i am not able to understand what else part doing
please contact our admin team : +91 9985396677
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);
nice logic
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
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
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
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