This is what i had thought of too, btw, we need to update first in all
cases whether the char is same as a previous one or different.

int minD(char *pStr, int len, char a, char b)
{
  if ((!pStr)|| (len <2)) return -1;
  if (a == b) return 0;
  bool foundA=false;
  bool foundB=false;
  int startIndex = -1;
  int minD=INT_MAX;

  while(pStr[i] !='\0')
  {
    if (*pStr == a)
    {
      if (foundA) //already found A
      {  startIndex = i; }
      else if (foundB)
      {  minD = min(minD, i-startIndex-1); foundB = false;foundA = true;
startIndex = i; }
    }
   else (*pStr == b)
   {
      if (foundB) //already found B
      {  startIndex = i; }
      else if (foundA)
      {  minD = min(minD, i-startIndex-1); foundA = false;foundB = true;
startIndex = i; }
   }
   i++;
  }
  if (minD >length) return -1;
  return minD;
}




Best Regards
Ashish Goel
"Think positive and find fuel in failure"
+919985813081
+919966006652


On Sun, Feb 5, 2012 at 11:13 PM, WgpShashank <[email protected]>wrote:

> @ashsih ..here is algo
>
> 1st Traverse array from left side and stop if either *a* or *b *are
> found. Store index of this first occurrence in a variable say first then
> Now traverse *array *after the index *first*. If the element at current
> index *i* matches with either x or y then check if it is different from *
> arr[first]* that we have already found. If it is different then update
> the minimum distance so far we have traversed the array . If it is same
> then update *first * i.e., make *first = i*.
> repeat same again .
>
> time complexity O(N) , space complexity O(1)
>
> Hope you can implement it :) Let me if other way exist to do the same or
> any flaw in approach ?
>
> *Thanks
> Shashank Mani Narayan
> Computer Science & Engineering
> Birla Institute of Technology,Mesra
> ** Founder Cracking The Code Lab  "http://shashank7s.blogspot.com/"*
>
>  --
> 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/-/MUpEZqJS1NcJ.
>
> 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.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
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.

Reply via email to