There is a very simple solution to this problem, Pseudo Code follows: Inputs: str - string, c1,c2 are characters.
*MinDistance(str, c1, c2): initialize RightMostIndex[127] with all entries as -1 initialize curMin as Len(str)+1 For i = 0 to Len(str) - 1 if str[i] is same c1 then if RightMostIndex[c2] != -1 then curMin = minimum(curMin, i - RightMostIndex[c2]) Set RightMostIndex[c1] = i otherwise if str[i] is same as c2 then if RightMostIndex[c1] != -1 then CurMin = minimum(curMin, i - RightMostIndex[c1]) Set RightMostIndex[c2] = i Output CurMin* C++ implementation at http://ideone.com/11H2z Thanks, Rizwan. On Tue, Jun 28, 2011 at 3:54 PM, Gaurav Saxena <grvsaxena...@gmail.com> wrote: > What is the problem with sunny's solution ?? > > On Mon, Jun 20, 2011 at 9:31 AM, RAJAT kumar singh < rajat123si...@gmail.com> > wrote: >> >> #include<stdio.h> >> #include<math.h> >> char >> s[]={'a','x','x','b','a','b','x','x','x','x','x','a','b','a','x','x','b',}; >> >> >> int minimum(char,char,int ); >> int main() >> { >> >> int i=minimum('a','b',17); >> printf("%d",i); >> } >> >> >> int minimum(char a ,char b,int n) >> { >> int i=0; >> int lindex=-1,rindex=-1,min; >> >> min=n; >> while(i<n) >> { >> >> >> >> >> if(s[i]==a) >> { >> lindex=i; >> if(rindex!=-1) >> { >> min=mini(min,abs(lindex-rindex)); >> } >> } >> >> else if(s[i]==b) >> { >> rindex=i; >> if(lindex!=-1) >> { >> min=mini(min,abs(lindex-rindex)); >> } >> >> >> >> } >> >> >> i++; >> } >> >> return min; >> } >> >> >> int mini(int a,int b) >> { >> >> if(a>b) >> return b; >> else >> return a; >> >> >> } >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To post to this group, send email to algogeeks@googlegroups.com. >> To unsubscribe from this group, send email to >> algogeeks+unsubscr...@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/algogeeks?hl=en. > > > > -- > Thanks and Regards , > Gaurav Saxena > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- Thanks and regards Rizwan A Hudda http://sites.google.com/site/rizwanhudda2 -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.