My solution for this :
#include<stdio.h>
int max(int a,int b)
{
return a>b?a:b;
}
int main()
{
char str[] = "abcdab";
int count=0,max1=0;
int i=0,j,k;
int hash[26];
for(i=0;i<26;i++)
hash[i]=-1;
for(i=0;i<strlen(str);i++)
{
count=0;
for(j=i;hash[str[j]-'a']==-1;j++)
{
hash[str[j]-'a'] = 1;
count++;
}
max1=max(count,max1);
for(k=0;k<26;k++)
hash[k]=-1;
}
printf("%d ",max1);
getch();
return 0;
}
Worst case running time : O(n^2) when string is of the form "abcdeabcde".
Does there exist an O(n) solution for this?
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/algogeeks/-/HoCrZFVsRh8J.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.