This is the Java Program to Find the Longest SubString Without Any Repeated Characters
Given a string, find and print the longest substring without any repeating characters, that is all the characters in the substring must be unique.
Example:
String x = “ABBCDFGHC”
Output = “BCDFGH”
Create a stack and push the first character of the string on it. Now, iterate through the string, and check whether the current character of the string is present in the stack or not. If it is present, update the longest substring as necessary.
Here is the source code of the Java Program to Find the Longest SubString Without Any Repeated Characters. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.
// Java Program to Find the Longest SubString Without Any Repeated Characters.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Stack;
public class LongestNonRepeatedSubstring {
// Function to find the longest non-repeating substring
static String longestSubstring(String str){
Stack<Character> stack = new Stack<>();
int last = 1;
int prev = 0;
String ans = "";
int i;
int max = -1;
stack.push(str.charAt(0));
for(i=1; i<str.length(); i++){
if(stack.search(str.charAt(i)) != -1){
if((i-prev) > max){
ans = str.substring(prev, i);
max = i-prev;
}
prev = i;
stack.clear();
stack.push(str.charAt(i));
}
else{
stack.push(str.charAt(i));
}
}
if((i-prev) > max){
ans = str.substring(prev, i);
}
return ans;
}
// Function to read input and display the output
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
System.out.println("Enter the text string");
try{
str = br.readLine();
}catch (Exception e){
System.out.println("An error occurred");
return;
}
String ans = longestSubstring(str);
System.out.println("The longest substring without any repeated"
+ " characters is " + ans);
}
}
1. In function longestSubstring(), a stack is created to hold the characters and the first character of the string is pushed onto it.
2. The variables prev and last are used to store the starting and ending indexes of the substring and max stores the length of the longest substring.
3. The loop for(i=1; i<str.length(); i++) iterates through the string.
4. The condition if(stack.search(str.charAt(i)) != -1) checks if the current character of the string is already present in the stack.
5. If it is, the condition if((i-prev) > max) checks if the length of the current substring from prev to i is greater than the previous substring.
6. If it is, the max and ans variables are updated accordingly and the stack is cleared.
7. In case of the condition is point 4 failing, the current character is pushed onto the stack.
8. The longest substring is returned.
Time Complexity: O(n) where n is the length of the string.
Case 1 (Simple Test Case): Enter the text string ABBCDFGHC The longest substring without any repeated characters is BCDFGH Case 2 (Simple Test Case - another example): Enter the text string XXYYZUIOPZQWERTYKLA The longest substring without any repeated characters is ZQWERTYKLA
Sanfoundry Global Education & Learning Series – Java Programs.
- Check Programming Books
- Practice BCA MCQs
- Practice Programming MCQs
- Check Java Books
- Apply for Java Internship