Find the minimum distance between two specified characters in a given string

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Minimum distance = " + GetMinimumDistance("bbxaayybb", 'a', 'b'));
        }
        public static int GetMinimumDistance(string str, char ch1, char ch2)
        {
            int distance = -1; int flag = 0; int x = -1; int y = -1;
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] == ch1 || str[i] == ch2)
                {
                    //Initialize
                    if (flag == 0)
                    {
                        if (str[i] == ch1)
                        {
                            flag = 1; x = i;
                        }
                        else
                        {
                            flag = 2; y = i;
                        }
                    }
                    else
                    {
                        //Encountered second character after first
                        if (flag == 1 && str[i] == ch2)
                        {
                            y = i; flag = 2;
                        }
                        //Encountered first character after second
                        else if (flag == 2 && str[i] == ch1)
                        {
                            x = i; flag = 1;
                        }
                        //Encountered first character again after first
                        else if (flag == 1 && str[i] == ch1)
                        {
                            x = i;
                        }
                        //Encountered second character again after second
                        else if (flag == 2 && str[i] == ch2)
                        {
                            y = i;
                        }
                    }
                    //Check Distance
                    if (x != -1 && y != -1)
                    {
                        if (distance == -1)
                        {
                            distance = Math.Abs(x - y);
                        }
                        else
                        {
                            if (distance > Math.Abs(x - y))
                            {
                                distance = Math.Abs(x - y);
                            }
                        }
                    }
                }
            }
            return distance;
        }
    }
}

Output
======
Minimum distance = 2
Press any key to continue . . .
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

One Response to Find the minimum distance between two specified characters in a given string

  1. Saikat says:

    Here is another logic:
    =======================
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
    using System.Diagnostics;
    using System.IO;
    using System.Text.RegularExpressions;
    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    string str = "bbxaayybb";
    char ch1 = 'a';
    char ch2 = 'b';
    Console.WriteLine(GetMinAbsoluteDistance(str, ch1, ch2));
    }

    public static int GetMinAbsoluteDistance(string str, char ch1, char ch2)
    {
    int count1 = 0, finalCount = 0, flag = 0;
    for (int i = 0; i < str.Length; i++)
    {
    if (str[i] == ch1)
    {
    for (int j = i + 1; j < str.Length; j++)
    {
    if (str[j] == ch2)
    {
    count1 = j – i;
    if (flag == 0)
    {
    finalCount = count1;
    flag = 1;
    }
    else
    {
    if (finalCount > count1)
    {
    finalCount = count1;
    }
    }
    break;
    }
    }
    }
    else
    {
    if (str[i] == ch2)
    {
    for (int j = i + 1; j < str.Length; j++)
    {
    if (str[j] == ch1)
    {
    count1 = j – i;
    if (flag == 0)
    {
    finalCount = count1;
    flag = 1;
    }
    else
    {
    if (finalCount > count1)
    {
    finalCount = count1;
    }
    }
    break;
    }
    }
    }
    }
    }
    return finalCount;
    }
    }
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s