Print Longest substring without repeating characters
Last Updated :
23 Apr, 2025
Given a string s having lowercase characters, find the length of the longest substring without repeating characters.
Examples:
Input: s = “geeksforgeeks”
Output: 7
Explanation: The longest substrings without repeating characters are “eksforg” and “ksforge”, with lengths of 7.
Input: s = “aaa”
Output: 1
Explanation: The longest substring without repeating characters is “a”
Input: s = “abcdefabcbb”
Output: 6
Explanation: The longest substring without repeating characters is “abcdef”.
Given a string, print the longest substring without repeating characters. For example, the longest substrings without repeating characters for “ABDEFGABEF” are “BDEFGA” and “DEFGAB”, with length 6. For “BBBB” the longest substring is “B”, with length 1. The desired time complexity is O(n) where n is the length of the string.
Examples:
Input : GEEKSFORGEEKS
Output : EKSFORG
Input : ABDEFGABEF
Output : BDEFGA
Approach:
The idea is to traverse the string and for each already visited character store its last occurrence in a hash table(Here unordered_map is used as a hash with key as character and value as its last position). The variable st stores the starting point of the current substring, maxlen stores the length of maximum length substring, and start stores the starting index of maximum length substring. While traversing the string, check whether the current character is present in the hash table or not.
If it is not present, then store the current character in the hash table with value as the current index. If it is already present in the hash table, this means the current character could repeat in the current substring. For this check, if the previous occurrence of character is before or after the starting point st of the current substring. If it is before st, then only update the value in the hash table. If it is after st, then find the length of current substring currlen as i-st, where i is the current index. Compare currlen with maxlen. If maxlen is less than currlen, then update maxlen as currlen and start as st. After complete traversal of the string, the required the longest substring without repeating characters is from s[start] to s[start+maxlen-1].
Implementation:
C++
// C++ program to find and print longest
// substring without repeating characters.
#include <bits/stdc++.h>
using namespace std;
// Function to find and print longest
// substring without repeating characters.
string findLongestSubstring(string str)
{
int i;
int n = str.length();
// starting point of current substring.
int st = 0;
// length of current substring.
int currlen;
// maximum length substring without repeating
// characters.
int maxlen = 0;
// starting index of maximum length substring.
int start;
// Hash Map to store last occurrence of each
// already visited character.
unordered_map<char, int> pos;
// Last occurrence of first character is index 0;
pos[str[0]] = 0;
for (i = 1; i < n; i++) {
// If this character is not present in hash,
// then this is first occurrence of this
// character, store this in hash.
if (pos.find(str[i]) == pos.end())
pos[str[i]] = i;
else {
// If this character is present in hash then
// this character has previous occurrence,
// check if that occurrence is before or after
// starting point of current substring.
if (pos[str[i]] >= st) {
// find length of current substring and
// update maxlen and start accordingly.
currlen = i - st;
if (maxlen < currlen) {
maxlen = currlen;
start = st;
}
// Next substring will start after the last
// occurrence of current character to avoid
// its repetition.
st = pos[str[i]] + 1;
}
// Update last occurrence of
// current character.
pos[str[i]] = i;
}
}
// Compare length of last substring with maxlen and
// update maxlen and start accordingly.
if (maxlen < i - st) {
maxlen = i - st;
start = st;
}
// The required longest substring without
// repeating characters is from str[start]
// to str[start+maxlen-1].
return str.substr(start, maxlen);
}
// Driver function
int main()
{
string str = "GEEKSFORGEEKS";
cout << findLongestSubstring(str);
return 0;
}
Java
// Java program to find
// and print longest substring
// without repeating characters.
import java.util.*;
class GFG{
// Function to find and print longest
// substring without repeating characters.
public static String findLongestSubstring(String str)
{
int i;
int n = str.length();
// Starting point
// of current substring.
int st = 0;
// length of
// current substring.
int currlen = 0;
// maximum length
// substring without
// repeating characters.
int maxlen = 0;
// starting index of
// maximum length substring.
int start = 0;
// Hash Map to store last
// occurrence of each
// already visited character.
HashMap<Character,
Integer> pos = new HashMap<Character,
Integer>();
// Last occurrence of first
// character is index 0;
pos.put(str.charAt(0), 0);
for (i = 1; i < n; i++)
{
// If this character is not present in hash,
// then this is first occurrence of this
// character, store this in hash.
if (!pos.containsKey(str.charAt(i)))
{
pos.put(str.charAt(i), i);
}
else
{
// If this character is present
// in hash then this character
// has previous occurrence,
// check if that occurrence
// is before or after starting
// point of current substring.
if (pos.get(str.charAt(i)) >= st)
{
// find length of current
// substring and update maxlen
// and start accordingly.
currlen = i - st;
if (maxlen < currlen)
{
maxlen = currlen;
start = st;
}
// Next substring will start
// after the last occurrence
// of current character to avoid
// its repetition.
st = pos.get(str.charAt(i)) + 1;
}
// Update last occurrence of
// current character.
pos.replace(str.charAt(i), i);
}
}
// Compare length of last
// substring with maxlen and
// update maxlen and start
// accordingly.
if (maxlen < i - st)
{
maxlen = i - st;
start = st;
}
// The required longest
// substring without
// repeating characters
// is from str[start]
// to str[start+maxlen-1].
return str.substring(start,
start +
maxlen);
}
// Driver Code
public static void main(String[] args)
{
String str = "GEEKSFORGEEKS";
System.out.print(findLongestSubstring(str));
}
}
// This code is contributed by divyeshrabadiya07
Python
# Python3 program to find and print longest
# substring without repeating characters.
# Function to find and print longest
# substring without repeating characters.
def findLongestSubstring(string):
n = len(string)
# starting point of current substring.
st = 0
# maximum length substring without
# repeating characters.
maxlen = 0
# starting index of maximum
# length substring.
start = 0
# Hash Map to store last occurrence
# of each already visited character.
pos = {}
# Last occurrence of first
# character is index 0
pos[string[0]] = 0
for i in range(1, n):
# If this character is not present in hash,
# then this is first occurrence of this
# character, store this in hash.
if string[i] not in pos:
pos[string[i]] = i
else:
# If this character is present in hash then
# this character has previous occurrence,
# check if that occurrence is before or after
# starting point of current substring.
if pos[string[i]] >= st:
# find length of current substring and
# update maxlen and start accordingly.
currlen = i - st
if maxlen < currlen:
maxlen = currlen
start = st
# Next substring will start after the last
# occurrence of current character to avoid
# its repetition.
st = pos[string[i]] + 1
# Update last occurrence of
# current character.
pos[string[i]] = i
# Compare length of last substring with maxlen
# and update maxlen and start accordingly.
if maxlen < i - st:
maxlen = i - st
start = st
# The required longest substring without
# repeating characters is from string[start]
# to string[start+maxlen-1].
return string[start : start + maxlen]
# Driver Code
if __name__ == "__main__":
string = "GEEKSFORGEEKS"
print(findLongestSubstring(string))
# This code is contributed by Rituraj Jain
C#
// C# program to find
// and print longest substring
// without repeating characters.
using System;
using System.Collections.Generic;
class GFG{
// Function to find and
// print longest substring
// without repeating characters.
public static String findlongestSubstring(String str)
{
int i;
int n = str.Length;
// Starting point
// of current substring.
int st = 0;
// length of
// current substring.
int currlen = 0;
// maximum length
// substring without
// repeating characters.
int maxlen = 0;
// starting index of
// maximum length substring.
int start = 0;
// Hash Map to store last
// occurrence of each
// already visited character.
Dictionary<char,
int> pos = new Dictionary<char,
int>();
// Last occurrence of first
// character is index 0;
pos.Add(str[0], 0);
for (i = 1; i < n; i++)
{
// If this character is not present in hash,
// then this is first occurrence of this
// character, store this in hash.
if (!pos.ContainsKey(str[i]))
{
pos.Add(str[i], i);
}
else
{
// If this character is present
// in hash then this character
// has previous occurrence,
// check if that occurrence
// is before or after starting
// point of current substring.
if (pos[str[i]] >= st)
{
// find length of current
// substring and update maxlen
// and start accordingly.
currlen = i - st;
if (maxlen < currlen)
{
maxlen = currlen;
start = st;
}
// Next substring will start
// after the last occurrence
// of current character to avoid
// its repetition.
st = pos[str[i]] + 1;
}
// Update last occurrence of
// current character.
pos[str[i]] = i;
}
}
// Compare length of last
// substring with maxlen and
// update maxlen and start
// accordingly.
if (maxlen < i - st)
{
maxlen = i - st;
start = st;
}
// The required longest
// substring without
// repeating characters
// is from str[start]
// to str[start+maxlen-1].
return str.Substring(start,
maxlen);
}
// Driver Code
public static void Main(String[] args)
{
String str = "GEEKSFORGEEKS";
Console.Write(findlongestSubstring(str));
}
}
// This code is contributed by shikhasingrajput
JavaScript
<script>
// JavaScript program for the above approach
// Function to find and print longest
// substring without repeating characters.
function findLongestSubstring(str)
{
var i;
var n = str.length;
// starting point of current substring.
var st = 0;
// length of current substring.
var currlen;
// maximum length substring without repeating
// characters.
var maxlen = 0;
// starting index of maximum length substring.
var start;
// Hash Map to store last occurrence of each
// already visited character.
var pos = new Map();
// Last occurrence of first character is index 0;
pos.set(str[0], 0);
for (var i = 1; i < n; i++) {
// If this character is not present in hash,
// then this is first occurrence of this
// character, store this in hash.
if (!pos.has(str[i]))
pos.set(str[i],i) ;
else {
// If this character is present in hash then
// this character has previous occurrence,
// check if that occurrence is before or after
// starting point of current substring.
if (pos.get(str[i]) >= st) {
// find length of current substring and
// update maxlen and start accordingly.
currlen = i - st;
if (maxlen < currlen) {
maxlen = currlen;
start = st;
}
// Next substring will start after the last
// occurrence of current character to avoid
// its repetition.
st = pos.get(str[i]) + 1;
}
// Update last occurrence of
// current character.
pos.set(str[i], i);
}
}
// Compare length of last substring with maxlen and
// update maxlen and start accordingly.
if (maxlen < i - st) {
maxlen = i - st;
start = st;
}
// The required longest substring without
// repeating characters is from str[start]
// to str[start+maxlen-1].
return str.substr(start,maxlen);
}
var str = "GEEKSFORGEEKS";
document.write(findLongestSubstring(str));
// This code is contributed by SoumikMondal
</script>
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(n)
Another Approach:
- Initialize a hash set to keep track of the characters that have been visited and two variables l and r to mark the left and right ends of the current substring. Also, initialize variables maxL, maxR, and maxStr to keep track of the longest substring found so far.
- Iterate through the string using a while loop until the right pointer r reaches the end of the string.
- Check if the character s[r] is present in the hash set. If it's not present, insert it into the hash set and move the right pointer r to the next character.
- If the character s[r] is already present in the hash set, it means that the current substring has repeating characters. In this case, remove the character s[l] from the hash set and move the left pointer l to the next character. Repeat this step until the character s[r] is no longer present in the hash set.
- Check if the length of the current substring (r - l + 1) is greater than the length of the longest substring found so far (maxStr). If it is, update maxStr to the length of the current substring, and update maxL and maxR to the left and right pointers l and r.
- Once the loop is finished, use the maxL and maxR indices to print the longest substring without repeating characters.
Below is the implementation of above approach:
C++
#include <iostream>
#include <unordered_set>
using namespace std;
void printLongestSubstring(string s) {
int n = s.size();
int l = 0, r = 0;
unordered_set<char> visited;
int maxStr = 0;
int maxL = 0, maxR = 0;
while (r < n) {
if (visited.find(s[r]) == visited.end()) {
visited.insert(s[r]);
if (r - l + 1 > maxStr) {
maxStr = r - l + 1;
maxL = l;
maxR = r;
}
r++;
}
else {
visited.erase(s[l]);
l++;
}
}
for (int i = maxL; i <= maxR; i++) {
cout << s[i];
}
cout << endl;
}
int main() {
string str = "GEEKSFORGEEKS";
printLongestSubstring(str);
return 0;
}
Java
import java.util.HashSet;
public class Main {
public static void printLongestSubstring(String s)
{
int n = s.length();
int l = 0, r = 0;
HashSet<Character> visited = new HashSet<>();
int maxStr = 0;
int maxL = 0, maxR = 0;
while (r < n) {
if (!visited.contains(s.charAt(r))) {
visited.add(s.charAt(r));
if (r - l + 1 > maxStr) {
maxStr = r - l + 1;
maxL = l;
maxR = r;
}
r++;
}
else {
visited.remove(s.charAt(l));
l++;
}
}
for (int i = maxL; i <= maxR; i++) {
System.out.print(s.charAt(i));
}
System.out.println();
}
public static void main(String[] args)
{
String str = "GEEKSFORGEEKS";
printLongestSubstring(str);
}
}
// This code is contributed by Prajwal Kandekar
Python
def printLongestSubstring(s: str) -> None:
n = len(s)
l, r = 0, 0
visited = set()
maxStr = 0
maxL, maxR = 0, 0
while r < n:
if s[r] not in visited:
visited.add(s[r])
if r - l + 1 > maxStr:
maxStr = r - l + 1
maxL, maxR = l, r
r += 1
else:
visited.remove(s[l])
l += 1
print(s[maxL:maxR+1])
str = "GEEKSFORGEEKS"
printLongestSubstring(str)
C#
using System;
using System.Collections.Generic;
namespace LongestSubstring
{
class Program
{
static void printLongestSubstring(string s)
{
int n = s.Length;
int l = 0, r = 0;
HashSet<char> visited = new HashSet<char>();
int maxStr = 0;
int maxL = 0, maxR = 0;
while (r < n)
{
if (!visited.Contains(s[r]))
{
visited.Add(s[r]);
if (r - l + 1 > maxStr)
{
maxStr = r - l + 1;
maxL = l;
maxR = r;
}
r++;
}
else
{
visited.Remove(s[l]);
l++;
}
}
for (int i = maxL; i <= maxR; i++)
{
Console.Write(s[i]);
}
Console.WriteLine();
}
static void Main(string[] args)
{
string str = "GEEKSFORGEEKS";
printLongestSubstring(str);
Console.ReadKey();
}
}
}
JavaScript
function printLongestSubstring(s) {
const n = s.length;
let l = 0;
let r = 0;
const visited = new Set();
let maxStr = 0;
let maxL = 0;
let maxR = 0;
while (r < n) {
// Check if the character at s[r] has not been visited
if (!visited.has(s[r])) {
visited.add(s[r]);
// Calculate the length of the current substring
if (r - l + 1 > maxStr) {
maxStr = r - l + 1;
maxL = l;
maxR = r;
}
r++;
} else {
// If the character at s[r] has been visited, remove s[l] from the set
visited.delete(s[l]);
l++;
}
}
// Print the longest substring without repeating characters
let longestSubstring = '';
for (let i = maxL; i <= maxR; i++) {
longestSubstring += s[i];
}
console.log(longestSubstring);
}
// Main function
const str = "GEEKSFORGEEKS";
printLongestSubstring(str);
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem