Determine whether each character in the first string can be uniquely replaced by a character in the second string so that the two strings are equal

Viewed 130

Give two strings of equal size. Determine whether each character in the first string can be uniquely replaced by a character in the second string so that the two strings are equal. Display also the corresponding character pairs between the two strings. The code works well now.

Example 1:

For input data:

aab
ttd

The console will display:

True
a => t
b => d

Example 2:

For input data:

tab
ttd

The console will display:

False

In the second example the answer is false because there is no unique correspondence for the character 'a': both 't' and 'd' correspond to it.

This is my code:

using System;

namespace problemeJM { class Program { static void Main(string[] args) { string firstPhrase = Convert.ToString(Console.ReadLine()); string secondPhrase = Convert.ToString(Console.ReadLine()); string aux1 = string.Empty, aux2 = string.Empty; bool x = true;

        for (int i = 0; i < firstPhrase.Length; i++)
        {
            if (!aux1.Contains(firstPhrase[i]))
            {
                aux1 += firstPhrase[i];
            }
        }
        for (int i = 0; i < secondPhrase.Length; i++)
        {
            if (!aux2.Contains(secondPhrase[i]))
            {
                aux2 += secondPhrase[i];
            }
        }
        if (aux1.Length != aux2.Length)
        {
            
            Console.WriteLine("False");
            
        }
        else
        {
            for (int i = 0; i < firstPhrase.Length - 2; i++)
            {
                for (int j = 1; j < secondPhrase.Length - 1; j++)
                {
                    if (firstPhrase[i] == firstPhrase[j] && secondPhrase[i] == secondPhrase[j])
                    {
                        x = true;
                    }
                    else if (firstPhrase[i] != firstPhrase[j] && secondPhrase[i] != secondPhrase[j])
                    {
                        x = true;
                    }
                    else if (firstPhrase[i] == firstPhrase[j] && secondPhrase[i] != secondPhrase[j])
                    {
                        x = false;
                        break;
                    }
                    else if (firstPhrase[i] != firstPhrase[j] && secondPhrase[i] == secondPhrase[j])
                    {
                        x = false;
                        break;
                    }
                }
            }
        
        
        Console.WriteLine(x);
        aux1 = string.Empty;
        aux2 = string.Empty;
        if (x == true)
        {
            for (int i = 0; i < firstPhrase.Length; i++)
            {
                if (!aux1.Contains(firstPhrase[i]))
                {
                    aux1 += firstPhrase[i];
                }
            }
            for (int i = 0; i < secondPhrase.Length; i++)
            {
                if (!aux2.Contains(secondPhrase[i]))
                {
                    aux2 += secondPhrase[i];
                }
            }

            for (int i = 0; i <= aux1.Length - 1; i++)
            {
                for (int j = 1; j <= aux2.Length; j++)
                {
                    if (aux1[i] == aux1[j] && aux2[i] == aux2[j])
                    {

                        Console.WriteLine(aux1[i] + " => " + aux2[i]);
                        break;
                    }
                    else if (aux1[i] != aux1[j] && aux2[i] != aux2[j])
                    {
                        Console.WriteLine(aux1[i] + " => " + aux2[i]);
                        break;
                    }
                 }
              }
            }
        }           
    }    
}

}

1 Answers

I think you should use a Dictionary<char, char> as commented. But you need to check if there's a unique mapping in both string, so from s1 to s2 and from s2 to s1:

static bool UniqueMapping(string s1, string s2)
{
    int length = Math.Min(s1.Length, s2.Length);
    var dict = new Dictionary<char, char>(length);
    for (int i = 0; i < length; i++)
    {
        char c1 = s1[i];
        char c2 = s2[i];
        bool contained = dict.TryGetValue(c1, out char c);
        if (contained && c2 != c)
        {
            return false;
        }

        dict[c1] = c2;
    }

    return true;
}

Here are your samples. Note that i use UniqueMapping twice(if true after 1st):

static void Main(string[] args)
{
    var items = new List<string[]> { new[]{ "aab", "ttd" }, new[] { "tab", "ttd" }, new[] { "ala bala portocala", "cuc dcuc efghficuc" }, new[] { "ala bala portocala", "cuc dcuc efghijcuc" } };
    foreach (string[] item in items)
    {
        bool result = UniqueMapping(item[0], item[1]);
        if(result) result = UniqueMapping(item[1], item[0]);
        Console.WriteLine($"Word 1 <{item[0]}> Word 2 <{item[1]}> UniqueMapping? {result}");
    }
}

.NET Fiddle: https://dotnetfiddle.net/4DtIyH

Related