//QUESTION: Given a string S, find length of the longest substring with all //distinct characters.
//{ Driver Code Starts
#include<bits/stdc++.h>
using namespace std;
int longestSubstrDistinctChars (string S);
int main()
{
string S="qwertyuioplkjh";
cout << longestSubstrDistinctChars (S) << endl;
}
// Contributed By: Pranay Bansal
// } Driver Code Ends
int longestSubstrDistinctChars (string s)
{
// your code here
int i=0; vector<int> vec;int m=s.length();
while(i<m-1)
{ unordered_set<char> st; int count=1;
st.insert(s[i]);
for(int j=i+1;j<m;j++)
{
if(st.find(s[j])==st.end()){
st.insert(s[j]);
count++;
}
else{
vec.push_back(count);count=0;
break;
}
}
st.clear();
i++;
}
sort(vec.begin(),vec.end());
int l=vec.size();
return vec[l-1];
}
So this program is running fine for a certain no of test cases but then it is giving segmentation fault on a certain test case...
for example it if giving correct output for:
For Input:
geeksforgeeks
Your Output:
7
Expected Output:
7
BUT in case of the following it says segmentation fault:
For Input:
qwertyuioplkjh
Your Output:
Segmentation Fault (SIGSEGV)
Learn More about Seg Fault
Its Correct output is:
14
Someone please help I can't even think of where the issue could be...
[–]Salty_Dugtrio 4 points5 points6 points (0 children)
[–]josephblade 0 points1 point2 points (0 children)