1. Set firstchar = 'a', secondChar = 'b', charFound = '', charPos =
-1, minDistance = len(input) + 1
2. For index = 0 to len(input) - 1
        a. If input[index] == firstChar || input[index] == secondChar
                i. Set charPos = index
                ii. Set charFound = input[index]
                iii. break
3. If index == len(input)
        Print "Characters do not exist in input"
        Return
4. index++
5. While index < len(input)
        a. If input[index] != charFound && (input[index] == firstChar ||
input[index] == secondChar)
                i. Set temp = index - charPos
                ii. If temp < minDistance
                        Set minDistance = temp
                ii. charFound = input[index]
                iii. charPos = index
           Else If input[index] == charFound
                i. charPos = index
        b. index++
6. If minDistance == -1
        Print "Only one character found in the string : " + charFound
   Else
        Print "Minimum distance = " + minDistance

On Jun 17, 1:25 pm, Ashish Goel <ashg...@gmail.com> wrote:
> will this work 4
>
> axxbxba
> chars r a,b in this str
> Best Regards
> Ashish Goel
> "Think positive and find fuel in failure"
> +919985813081
> +919966006652
>
> On Fri, Jun 17, 2011 at 11:47 PM, sunny agrawal 
> <sunny816.i...@gmail.com>wrote:
>
> > Try this:
>
> > say i is the index of the first occurrence of the first character
> > say j is the index of the first occurrence of the second character
> > say n is length of array
> > int Min = n+1;
>
> > while(i < n && j < n){
> > int Min = min(Min, abs(i-j))
> > if(i < j){
> > find next occurrence of first character
> > }
> > else{
> > find next occurrence of second character
> > }
> > }
>
> > On 6/17/11, Harshal <hc4...@gmail.com> wrote:
> > > Given a character array with a set of characters, there might be
> > repetitions
> > > as well, given two characters, you should give the minimum distance
> > between
> > > these two characters in the whole array. O(n) solution is required.
>
> > > --
> > > Harshal Choudhary,
> > > III Year B.Tech CSE,
> > > NIT Surathkal, Karnataka, India.
>
> > > --
> > > 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.
>
> > --
> > Sunny Aggrawal
> > B-Tech IV year,CSI
> > Indian Institute Of Technology,Roorkee
>
> > --
> > 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.

-- 
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.

Reply via email to