Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
longest_substring_without_repeating_characters.cpp File Reference

Solution for Longest Substring Without Repeating Characters problem. More...

#include <iostream>
#include <unordered_map>
#include <deque>
#include <string>
#include <cassert>
Include dependency graph for longest_substring_without_repeating_characters.cpp:

Classes

class  Longest_Substring
 Class that solves the Longest Substring Without Repeating Characters problem. More...
 

Functions

static void tests ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

Solution for Longest Substring Without Repeating Characters problem.

Problem link: https://leetcode.com/problems/longest-substring-without-repeating-characters/description/

Intuition: 1) The intuition is straightforward and simple. We track the frequency of characters. 2) Since we can't use a string to track the longest substring without repeating characters efficiently (as removing a character from the front of a string isn't O(1)), we optimize the solution using a deque approach.

Approach: 1) Initialize an unordered_map to track the frequency of characters. 2) Use a deque for pushing characters, and update the result deque (res) with the current deque (temp) whenever we find a longer substring. 3) Use a while loop to reduce the frequency from the front, incrementing i, and removing characters from the temp deque as we no longer need them. 4) Return res.size() as we are interested in the length of the longest substring.

Time Complexity: O(N) Space Complexity: O(N)

I hope this helps to understand. Thank you!

Author
Ashish Kumar Sahoo

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on successful execution.
107 {
108 tests(); // run self-test implementations
109 return 0;
110}
static void tests()
Self-test implementations.
Definition longest_substring_without_repeating_characters.cpp:92
Here is the call graph for this function:

◆ tests()

static void tests ( )
static

Self-test implementations.

Returns
void
92 {
94 assert(soln.lengthOfLongestSubstring("abcabcbb") == 3);
95 assert(soln.lengthOfLongestSubstring("bbbbb") == 1);
96 assert(soln.lengthOfLongestSubstring("pwwkew") == 3);
97 assert(soln.lengthOfLongestSubstring("") == 0); // Test case for empty string
98 assert(soln.lengthOfLongestSubstring("abcdef") == 6); // Test case for all unique characters
99 assert(soln.lengthOfLongestSubstring("a") == 1); // Single character
100 std::cout << "All test cases passed!" << std::endl;
101}
Class that solves the Longest Substring Without Repeating Characters problem.
Definition longest_substring_without_repeating_characters.cpp:37
int lengthOfLongestSubstring(std::string s)
Function to find the length of the longest substring without repeating characters.
Definition longest_substring_without_repeating_characters.cpp:44
T endl(T... args)
Here is the call graph for this function: