-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Create Longest_Substring_Without_Repeating_Characters.cpp #2761
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
8a09256
Create Longest_Substring_Without_Repeating_Characters.cpp
ashish5kmax 8985cab
Update and rename Longest_Substring_Without_Repeating_Characters.cpp …
ashish5kmax 91d2870
Update Longest_Substring_Without_Repeating_Characters.cpp
ashish5kmax 67328fa
Update Longest_Substring_Without_Repeating_Characters.cpp
ashish5kmax bbbff8a
Update Longest_Substring_Without_Repeating_Characters.cpp
ashish5kmax 70cc2e8
Merge branch 'master' into patch-2
ashish5kmax 39dbca3
Update and rename Longest_Substring_Without_Repeating_Characters.cpp …
ashish5kmax 77fd643
Update and rename Longest_Substring_Without_Repeating_Characters.cpp …
ashish5kmax e1bf9a0
Update longest_substring_without_repeating_characters.cpp
ashish5kmax db4768d
Update longest_substring_without_repeating_characters.cpp
ashish5kmax f819c2a
Merge branch 'master' into patch-2
ashish5kmax b988cb5
Update longest_substring_without_repeating_characters.cpp
ashish5kmax 754ab2f
Update longest_substring_without_repeating_characters.cpp
ashish5kmax fc22bc2
Merge branch 'master' into patch-2
ashish5kmax d82b3ff
Merge branch 'master' into patch-2
ashish5kmax b1291d4
Merge branch 'master' into patch-2
ashish5kmax 7a88f0b
Merge branch 'master' into patch-2
realstealthninja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
115 changes: 115 additions & 0 deletions
115
others/longest_substring_without_repeating_characters.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/** | ||
* @file | ||
* @brief Solution for Longest Substring Without Repeating Characters problem. | ||
* @details | ||
* Problem link: https://leetcode.com/problems/longest-substring-without-repeating-characters/description/ | ||
* | ||
* Intuition: | ||
* The intuition is straightforward and simple. We track the frequency of characters. | ||
* 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) | ||
* | ||
* Examples: | ||
* - testcase1: s = "abcabcbb", output = 3. | ||
* - testcase2: s = "bbbbb", output = 1. | ||
* - testcase3: s = "pwwkew", output = 3. | ||
* | ||
* I hope this helps to understand. | ||
* Thank you! | ||
**/ | ||
|
||
// ----------------- Header files ---------------------------------- | ||
ashish5kmax marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#include <iostream> // for input and output read/write. | ||
#include <unordered_map> // to use it for character frequency. | ||
#include <deque> // for push and pop operations at O(1) time. | ||
#include <string> // for taking string as input. | ||
|
||
using namespace std; // using the namespace standard to reduce redundant usage of std:: | ||
ashish5kmax marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
//------------------ Longest_Substring Class ------------------------------- | ||
/** | ||
* @class Solution | ||
* @brief Class that solves the Longest Substring Without Repeating Characters problem. | ||
*/ | ||
class Longest_Substring { | ||
public: | ||
/** | ||
* @brief Function to find the length of the longest substring without repeating characters. | ||
* @param s Input string. | ||
* @returns Length of the longest substring. | ||
*/ | ||
int lengthOfLongestSubstring(string s) { | ||
// If the size of string is 1, then it will be the answer. | ||
if (s.size() == 1) return 1; | ||
|
||
// Map used to store the character frequency. | ||
unordered_map<char, int> m; | ||
int n = s.length(); | ||
|
||
// Deque to remove from back if repeating characters are present. | ||
deque<char> temp; | ||
deque<char> res; | ||
int i, j; | ||
|
||
// Sliding window approach using two pointers. | ||
for (i = 0, j = 0; i < n && j < n;) { | ||
m[s[j]]++; | ||
|
||
// If repeating character found, update result and remove from the front. | ||
if (m[s[j]] > 1) { | ||
if (temp.size() > res.size()) { | ||
res = temp; | ||
} | ||
|
||
while (m[s[j]] > 1) { | ||
temp.pop_front(); | ||
m[s[i]]--; | ||
i++; | ||
} | ||
} | ||
|
||
// Add the current character to the deque. | ||
temp.push_back(s[j]); | ||
j++; | ||
} | ||
|
||
// Final check to update result. | ||
if (temp.size() > res.size()) { | ||
res = temp; | ||
} | ||
|
||
return res.size(); // Return the length of the longest substring. | ||
} | ||
}; | ||
|
||
//-------------------- Main function ------------------------------- | ||
ashish5kmax marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* @brief Main function. | ||
* @returns 0 on successful execution. | ||
ashish5kmax marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
int main() { | ||
// Create an object of the Solution class to call the function. | ||
Longest_Substring s; | ||
|
||
// User inputted string. | ||
string str; | ||
cout << "Enter the string: " << endl; | ||
cin >> str; | ||
|
||
// Function call to get the length of the longest substring without repeating characters. | ||
cout << soln.lengthOfLongestSubstring(str) << endl; | ||
|
||
return 0; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.