Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Java String Exercises: Find length of the longest substring of a given string without repeating characters

Java String: Exercise-37 with Solution

Write a Java program to find length of the longest substring of a given string without repeating characters.

Note:
1) Given string consists of English letters, digits, symbols and spaces.
2) 0 <= Given string length <= 5 * 104

Difficulty: Medium.
Company: Amazon, Google, Bloomberg, Microsoft, Adobe, Apple, Oracle, Facebook and more.

Input String : pickoutthelongestsubstring
The longest substring : [u, b, s, t, r, i, n, g]
The longest Substring Length : 8

Input String : ppppppppppppp
The longest substring : [p]
The longest Substring Length : 1

Input String : Microsoft
The longest substring : [M, i, c, r, o, s]
The longest Substring Length : 6

Sample Solution:

Java Code:

import java.util.LinkedHashMap;
public class Main {
 static void longestSubstring(String inputString) {
  char[] arr1 = inputString.toCharArray();
  String long_str = null;
  int str_length = 0;
  LinkedHashMap < Character, Integer >
  charPosMap = new LinkedHashMap < Character, Integer >();
  for (int i = 0; i < arr1.length; i++) {
   char ch = arr1[i];
   if (!charPosMap.containsKey(ch)) {
    charPosMap.put(ch, i);
   } else {
    i = charPosMap.get(ch);
    charPosMap.clear();
   }
   if (charPosMap.size() > str_length) {
    str_length = charPosMap.size();
    long_str = charPosMap.keySet().toString();
   }
  }
  System.out.println("Input String : " + inputString);
  System.out.println("The longest substring : " + long_str);
  System.out.println("The longest Substring Length : " + str_length);
 }
 public static void main(String[] args) {
  longestSubstring("pickoutthelongestsubstring");
  longestSubstring("ppppppppppppp");
  longestSubstring("Microsoft");  
 }
}

Sample Output:

Input String : pickoutthelongestsubstring
The longest substring : [u, b, s, t, r, i, n, g]
The longest Substring Length : 8
Input String : ppppppppppppp
The longest substring : [p]
The longest Substring Length : 1
Input String : Microsoft
The longest substring : [M, i, c, r, o, s]
The longest Substring Length : 6

Flowchart:

Flowchart: Java String Exercises - Find length of the longest substring of a given string without repeating characters.

Java Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a Java program to check whether two strings are interliving of a given string. Assuming that the unique characters in both strings.
Next: Write a Java program to print after removing duplicates from a given string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Java: Tips of the Day

How to sort an ArrayList?

Collections.sort(testList);
Collections.reverse(testList);

That will do what you want. Remember to import Collections though!

Ref: https://bit.ly/32urdSe