Menu Bar

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Monday, 23 January 2017

Write a Java Program to Find Longest Substring Without Repeating Characters In Java ?


 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
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

public class LongestSubString {
 
 public static void getSubString(String str){
  Map<Character, Integer> map = new LinkedHashMap<>();
  
  String subString = "";
  int subStringLength = 0;
  
  for(int i=0;i<str.length();i++){
   if(map.containsKey(str.charAt(i))){
    i = map.get(str.charAt(i));
    map.clear();
   }
   else{
    map.put(str.charAt(i), i);
   }
   if(map.size() > subStringLength){
    subStringLength = map.size();
   }
  }
  for(Entry<Character, Integer> en : map.entrySet()){
   subString+= en.getKey();
  }
  System.out.println("Input String : "+str+"\nOutput String : "+subString+"\n");
 }
 public static void main(String[] args) {
  LongestSubString.getSubString("pushkarkhosla");
  LongestSubString.getSubString("javaidentifiers");
  
 }
}

Output :

Input String : pushkarkhosla
Output String : rkhosla

Input String : javaidentifiers
Output String : fiers



      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Share this Blog with yours Friends !!

No comments:

Post a Comment