Skip to content

added Longest Substring Without Repeating Character in CPP #17

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions CPP/Longest_Substring_Without_Repeating_Characters.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* @author: Jatin Garg
* github(https://github.com/jatin-773762)
**/

#include<bits/stdc++.h>
using namespace std;

/**
* Test Cases:
* 1) s= "abcabcbb"
* output: 3 (abc)
* 2) s="bbbbb"
* output: 1 (b)
**/

int lengthOfLongestSubstring(string s) {

// check if the string is empty or not
if(s.empty()){
return 0;
}
// vector to store the presence of character
vector<bool> vis(256);
for(int i=0;i<256;i++){
vis[i]=false;
}
int ans=1;
int count=0;
for(int i=0;i<s.length();i++){
for(int i=0;i<256;i++){
vis[i]=false;
}
vis[s[i]]=true;
int j;
for(j=i+1;j<s.length();j++){
if(vis[s[j]]==false){
vis[s[j]]=true;
continue;
}
// if the character in substring repeats the loop breaks
break;
}
// "j-i" is the length of the current substring
ans = max(ans,j-i);
}
return ans;

}

int main(){


string s;
// input string
cin>>s;

// prints the length of the longest substring with no repeating characters
cout<<lengthOfLongestSubstring(s);
}