-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Length of Longest sub array without repeating characters (Window Sliding) #8699
Open
yellowberard
wants to merge
8
commits into
girlscript:Competitive_Programming
Choose a base branch
from
yellowberard:Competitive_Programming
base: Competitive_Programming
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a7c8f4c
Added Spiral Matrix Program
yellowberard 76903ae
Changes in spiral matrix
yellowberard be1b39d
Made changes in code format and input/output
yellowberard daad682
Merge branch 'girlscript:Competitive_Programming' into Competitive_Prβ¦
yellowberard 0908619
Update Spiral_Matrix.md
yellowberard 9025499
Update Spiral_Matrix.md
yellowberard adeff39
Merge branch 'girlscript:Competitive_Programming' into Competitive_Prβ¦
yellowberard 085966b
Create longest_sub_without_repeat.md
yellowberard 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
60 changes: 60 additions & 0 deletions
60
Competitive_Programming/C++/Array/longest_sub_without_repeat.md
This file contains 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,60 @@ | ||
# Length of the Longest Substring without Repeating Characters | ||
|
||
Here we will see a program to find the length of Longest substring without repeating characters. | ||
|
||
## Explanation | ||
In this program we are using the concept of Window sliding i.e. Whenever we see repetition, we remove the previous occurrence and slide the window. | ||
A loop is runniig from i=0 to n=sizeof(str). | ||
In side this loop one more loop is running from j=i till n. | ||
We define vector with all the value as false and keep it makin true for the characters that are visited once in the loop. | ||
|
||
## Code | ||
``` cpp | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int Substtr(string str) | ||
{ | ||
int n = str.size(); | ||
int result = 0; | ||
|
||
for (int i = 0; i < n; i++) { | ||
|
||
vector<bool> visited(256); | ||
|
||
for (int j = i; j < n; j++) { | ||
|
||
if (visited[str[j]] == true) // If current character is visited | ||
break; // Break the loop | ||
|
||
else { // Else update the result if | ||
res = max(res, j - i + 1); // this window is larger, and mark | ||
visited[str[j]] = true; // current character as visited. | ||
} | ||
} | ||
|
||
visited[str[i]] = false; // Remove the first character of previous window | ||
} | ||
return result; | ||
} | ||
|
||
int main() | ||
{ | ||
string str = "girlscriptwinterofcontributing"; | ||
cout << "The string used is " << str << endl; | ||
int length =Substtr(str); | ||
cout << "The length of the longest non-repeating " | ||
"character substring is " | ||
<< length; | ||
return 0; | ||
} | ||
|
||
``` | ||
## Time and Space Complexity | ||
**Time Complexity**: O(n^2) | ||
**Space Complexity**:O(n) | ||
|
||
## Input/Output | ||
**Input**: s = "abcabcbb" | ||
**Output**: 3 | ||
**Explanation**: The answer is "abc", with the length of 3. |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please explain the approach in detail with snippet examples as this is a basic problem.