Personal Website and Computer Science TUR EN

Interview Question Longest Substring Without Repeating Characters

Last update: 11/11/2016 2:00:00 AM


The problem is to find length of the longest substring without repeating characters in a given string. In order to solve this problem, each unique, distinct character in each substring is stored in a collection such as Set until same character is already found in the same sequence. After that it is passed into another substring group.

For example when input is abcabcbb,

Problem #3 - Longest Substring Without Repeating Characters

Description: Given a string, find the length of the longest substring without repeating characters.

Difficulty: Medium

Solution in Java

package com.fatihkabakci.Medium.LongestSubStr;

import java.util.HashSet;
import java.util.Set;

 * @author fkabakci
 * Problem Description: Given a string, find the length of the longest substring without repeating characters.
 * Examples:
 * Given "abcabcbb", the answer is "abc", which the length is 3.
 * Given "bbbbb", the answer is "b", with the length of 1.
 * Given "pwwkew", the answer is "wke", with the length of 3. 
 * Solution:
 * 1. Get each of character from i to n in the string
 * 2. Check if the set has already that
 * 	  if so, 
 * 			 ignore rest of the other characters in the current group and, 
 *           pass to sub group which is from i + 1 to n in the string.
 * 			 Also remove all data in the set.
 * 	  if not, 
 * 			 add the current character into the set, and 
 * 			 continue with the next character in the same group.
 * 3. Update always length of the longest substring
 * 4. Return length of the longest one
public class LongestSubStringWoRep {
	public static int lengthOfLongestSubstring(String s) {
		if (s.isEmpty() || s.length() == 1)
			return s.length();

		Set<Character> alphaSet = new HashSet<Character>();
		int i = 0, j = 0, maxLength = 1, n = s.length();
		while (j < n) {
			char c = s.charAt(j);
			if (alphaSet.contains(c)) {
				j = i++;
			} else {
			int currentLength = alphaSet.size();
			if (maxLength < currentLength)
				maxLength = currentLength;
		return maxLength;

	public static void main(String[] args) {


Similar Problems

#5 Longest Palindromic Substring

There has been no comment yet


   Please verify the image

The Topics in Computer Science

Search this site for


Software & Algorithms


In mathematics and computer science, an algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning.

Programming Languages


A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. It can be used to create programs to control the behavior of a machine. Java,C, C++,C#



A database is an organized collection of data. The data are typically organized to model aspects of reality in a way that supports processes requiring information.



Computer hardware is the collection of physical elements that constitutes a computer system. Computer hardware refers to the physical parts or components of a computer such as the monitor, memory, cpu.

Web Technologies


Web development is a broad term for the work involved in developing a web site for the Internet or an intranet. Html,Css,JavaScript,ASP.Net,PHP are one of the most popular technologies. J2EE,Spring Boot, Servlet, JSP,JSF, ASP

Mobile Technologies


Mobile application development is the process by which application software is developed for low-power handheld devices, such as personal digital assistants, enterprise digital assistants or mobile phones. J2ME



A computer network or data network is a telecommunications network that allows computers to exchange data. In computer networks, networked computing devices pass data to each other along data connections.

Operating Systems


An operating system is software that manages computer hardware and software resources and provides common services for computer programs. The OS is an essential component of the system software in a computer system. Linux,Windows

Computer Science


Computer science is the scientific and practical approach to computation and its applications.A computer scientist specializes in the theory of computation and the design of computational systems.