locked
Unique correspondence for characters RRS feed

  • Question

  • I need to 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.

    Like this:

    for "eef" and "ddg" 

    Result :

    True
    e => d  
    f =>g

    If I have: "efe" and "ddg" the result is "False"  because "e" it has "d" and "g" . (strings are equal in lenght)

     
    string firstWord = Console.ReadLine();
                string secondWord = Console.ReadLine();
    
                char[] firstPhraseChars = new char[firstWord.Length];
                char[] secondPhraseChars = new char[secondWord.Length];
                int charsCount = 0;
               
                 
                for (int i = 0; i< firstWord.Length && i< secondWord.Length; i+=2)
                {
                    char first = firstWord[i];
                    char firstofSecond = secondWord[i];
                    for(int j = i+1; j< firstWord.Length && j<secondWord.Length; j++,i++)
                    {
                        char second = firstWord[j];
                        char secondofSecond = secondWord[j];
    
                        if (first == second && firstofSecond == secondofSecond)
                        {
    
                            firstPhraseChars[charsCount] = firstWord[i];
                            secondPhraseChars[charsCount] = secondWord[i];
                            charsCount++;
                            
                            break;
    
                        }
                    }
    
    
                }
              
    
                for (int a = 0; a < firstPhraseChars.Length -1; a++)
                {
    
                    if ((firstWord.IndexOf(firstPhraseChars[a]) >= 0) && (secondWord.IndexOf(secondPhraseChars[a]) >= 0))
                    {
                        Console.WriteLine(true);
                        Console.WriteLine(firstPhraseChars[a] + " => " + secondPhraseChars[a]);
                        
                    }
                    if((firstWord.IndexOf(firstPhraseChars[a]) < 0) && (secondWord.IndexOf(secondPhraseChars[a]) < 0))
                    {
                        Console.WriteLine(firstPhraseChars[a] + " => " + secondPhraseChars[a]);
                    }
                    
                }
    
                    Console.WriteLine(false);

    Sunday, September 13, 2020 12:02 PM

Answers

  • Hi,

    Is this homework?

    We usually do not give a complete case for homework, but give some ideas and guidance.

    I try to make my thinking as clear as possible.

    1. It is still to judge whether the two strings are equal in length, if they are not equal, return false directly.

    2. Create two char arrays based on this length, and then double loop, just like your code, but the loop conditions are different.

                for (int i = 0; i < firstWord.Length; i++)
                {
                    firstCharArray[i] = firstWord[i];
                    secondCharArray[i] = secondWord[i];
                    for (int j = i + 1; j < firstWord.Length; j++)
                    {

    The first pair of characters can be placed directly into the array, and it will be the reference for our comparison.

    In the inner loop, the important thing is the comparison condition,

    first == second && firstofSecond == secondofSecond

    is one of the conditions that meets the conditions,

    first != second && firstofSecond != secondofSecond

    also meets the conditions.

    So when is the condition not met?

    Only when a pair is equal but a pair is not equal, like this:

    a a

    c d

    or

    a c

    d d

    When this happens, the structure of the two strings must be different.

    At this time, this method does not need to continue, you can directly return false. Otherwise, you can add the two variables of the inner loop to the previous char array.

    3. Assuming the string structure is the same, when the loop ends, we will get two complete char arrays, but there will still be some problems.

    Because if the two strings are "abca" and "cdec", when outputting the final comparison, there will be two "a => c", which need to be deduplicated.

    Create two char arrays, and then double loop to compare the pairs one by one.

    At this time, result1[j] == str1[i] && result2[j] == str2[i] will be used.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 16, 2020 2:19 AM

All replies

  • The problem is that I can't return the characters in desired order
    Sunday, September 13, 2020 4:10 PM
  • Hi keeponfalling,

    Thank you for posting here.

    According to my understanding, you seem to want to determine whether two strings have the same structure, right?

    If so, please try this code.

            static void Main(string[] args)
            {
                Console.WriteLine("Input:");
                string firstWord = Console.ReadLine();
                string secondWord = Console.ReadLine();
                Console.WriteLine(DoWork(firstWord,secondWord));
    
    
                Console.WriteLine("Press any key to continue...");
                Console.ReadLine();
    
            }
            public static bool DoWork(string firstWord,string secondWord)
            {
                if (firstWord.Length != secondWord.Length)
                    return false;
                var characterMap = new Dictionary<char, char>();
                for (int i = 0; i < firstWord.Length; i++)
                {
                    char char1 = firstWord[i];
                    char char2 = secondWord[i];
                    if (!characterMap.ContainsKey(char1))
                    {
                        if (characterMap.ContainsValue(char2))
                            return false;
                        characterMap[char1] = char2;
                    }
                    else
                    {
                        if (char2 != characterMap[char1])
                            return false;
                    }
                }
    
                foreach (var item in characterMap)
                {
                    Console.WriteLine(item.Key +" => "+item.Value);
                }
                return true;
            }

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, September 14, 2020 2:46 AM
  • Hello, and thank you for your time to look out my question. 

    I forgot to say that I have to use only System not list or dictionary, linq and others. The course assignment requirements. I think that is more about logic  


    Monday, September 14, 2020 7:21 AM
  • Hi,

    Is this homework?

    We usually do not give a complete case for homework, but give some ideas and guidance.

    I try to make my thinking as clear as possible.

    1. It is still to judge whether the two strings are equal in length, if they are not equal, return false directly.

    2. Create two char arrays based on this length, and then double loop, just like your code, but the loop conditions are different.

                for (int i = 0; i < firstWord.Length; i++)
                {
                    firstCharArray[i] = firstWord[i];
                    secondCharArray[i] = secondWord[i];
                    for (int j = i + 1; j < firstWord.Length; j++)
                    {

    The first pair of characters can be placed directly into the array, and it will be the reference for our comparison.

    In the inner loop, the important thing is the comparison condition,

    first == second && firstofSecond == secondofSecond

    is one of the conditions that meets the conditions,

    first != second && firstofSecond != secondofSecond

    also meets the conditions.

    So when is the condition not met?

    Only when a pair is equal but a pair is not equal, like this:

    a a

    c d

    or

    a c

    d d

    When this happens, the structure of the two strings must be different.

    At this time, this method does not need to continue, you can directly return false. Otherwise, you can add the two variables of the inner loop to the previous char array.

    3. Assuming the string structure is the same, when the loop ends, we will get two complete char arrays, but there will still be some problems.

    Because if the two strings are "abca" and "cdec", when outputting the final comparison, there will be two "a => c", which need to be deduplicated.

    Create two char arrays, and then double loop to compare the pairs one by one.

    At this time, result1[j] == str1[i] && result2[j] == str2[i] will be used.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 16, 2020 2:19 AM
  • Thank you for your help, you are the best! :)

    I change my code and manage to solve 90% of the problem.

    * Is a course assigment 


    Wednesday, September 16, 2020 11:09 AM