Nov
			02
		
				Core Java program to find the longest sub-string in a String without repeated chars.
Hey there! Welcome to ClearUrDoubt.com. In this post, we will look at a Core Java program to find the longest sub-string in a String without repeated chars. Here is the program:
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64  | 
						package com.clearurdoubt; import java.util.Scanner; /**  *   * @author clearurdoubt  *  */ public class FindSubstring { 	public static void main(String[] args) { 		System.out.println("Please enter an input string:");	// request user to provide an empty string 		Scanner in = new Scanner(System.in);	// Scanner instance to read the input from console 		String input = in.nextLine().trim();	// Read the input and trim the spaces around it 		// Find and print the longest substring.				 		System.out.println("The longest substring in the input \"" + input + "\" is \"" + findLongestSubstring(input) + "\""); 		in.close(); 	} 	/** 	 * Method to find the longest substring without repeated characters  	 * @param input string from user 	 * @return 	 */ 	private static String findLongestSubstring(String input) 	{ 		String longest = "";	// Variable for storing the longest substring  		// Loop through the characters from starting to ending  		for(int i=0; i < input.length() - 1; i++) 		{ 			String substr = "";	// Local variable to find the current substring without repeated chars 			// Loop through characters from index "i" till the end of input 			for(int j = i; j < input.length() - 1; j++) 			{ 				char ch = input.charAt(j);	// Current character 				if(substr.indexOf(ch) == -1)	// If substr doesn't contain the current character 				{ 					substr += ch;		// Time to append the current char to substr 				} 				else 				{ 					break;			// Current char already in the substr, so break out the inner loop 				} 			} 			// Time to check if the local substr is longer than "longest" variable  			if(longest.length() < substr.length())	 			{ 				longest = substr; 			} 		} 		return longest;  // Return the longest substring  	} }  | 
					
Output: Happy Learning :). Please leave a reply in case Read more