Skip to content

Commit bbbff8a

Browse files
authored
Update Longest_Substring_Without_Repeating_Characters.cpp
* Well documented and tested code for sliding window algorithm. * Thank you.
1 parent 67328fa commit bbbff8a

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

sliding-window/Longest_Substring_Without_Repeating_Characters.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,23 @@
11
/** Leetcode problem
22
* Author : @ashish5kmax (Ashish Kumar Sahoo)
3-
43
* 3. Longest_Substring_Without_Repeating_Characters
54
* Problem link :- https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
6-
75
* Intiuation :
86
Intuition is strightforward and simple, we track the frequencey and as we know we can't use string to track longest substring without repeating characters, as poping a char from front of string is not in O(1) which could be optimized by deque approch.
9-
107
* Approch :
118
1) At first we initialize a unordered_map to track the frequncy and then.
129
2) We initialize deque for pushing the characters and if temp > res we update our res deque to temp as we need longest substring here.
1310
3) And while loop is used to reduce the frequency from front doing i++ and removing charecters from temp deque as we no longer need them.
1411
4) return res.size() as we only need size not string.
15-
1612
* Time Complexity :
1713
O(N)
1814
1915
* Space Complexity :
2016
O(N)
21-
2217
* Examples:-
2318
testcase1:- s = "abcabcbb", o/p = 3.
2419
testcase2:- s = "bbbbb", o/p = 1.
2520
testcase3:- s = "pwwkew", o/p = 3.
26-
2721
* I hope this helps to understand.
2822
* Thank you!!
2923
**/
@@ -32,6 +26,9 @@
3226
#include <iostream> // for input and output read/write.
3327
#include <unordered_map> // to use it for character frequency.
3428
#include <deque> // for push and pop operations at O(1) time.
29+
#include <string> // for taking string as input
30+
31+
using namespace std; // using the namspace standard to reduce the redundent usage of std:: when calling functions (basically reduicng an extra overhead).
3532

3633
class Solution {
3734
public:
@@ -80,7 +77,7 @@ int main() {
8077
// object of class Solution created for function call.
8178
Solution soln;
8279

83-
80+
8481
// user inputted string.
8582
string s;
8683
cout<<"Enter the string : "<<endl;

0 commit comments

Comments
 (0)