【问题标题】: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判断第一个字符串中的每个字符是否可以唯一地替换为第二个字符串中的一个字符,使得两个字符串相等
【发布时间】:2022-01-19 16:00:08
【问题描述】:

编辑:所以我花了更多时间思考并找到了解决方案,如果您需要它的代码,我对其进行了编辑,所以现在是正确的,没有错误。感谢所有试图提供帮助的人,祝您有美好的一天!

给出两个大小相等的字符串。确定第一个字符串中的每个字符是否可以唯一地替换为第二个字符串中的字符,从而使两个字符串相等。同时显示两个字符串之间对应的字符对。

示例 1:

对于输入数据:

aab
ttd

控制台会显示:

True
a => t
b => d

示例 2:

对于输入数据:

tab
ttd

控制台会显示:

False

在第二个例子中,答案是错误的,因为字符“a”没有唯一的对应关系:“t”和“d”都对应它。

这是我的代码:

using System;

命名空间问题eJM { 课堂节目 { 静态无效主要(字符串 [] 参数) { string firstPhrase = Convert.ToString(Console.ReadLine()); 字符串 secondPhrase = Convert.ToString(Console.ReadLine()); 字符串 aux1 = string.Empty,aux2 = string.Empty; 布尔 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;
                    }
                 }
              }
            }
        }           
    }    
}

}

【问题讨论】:

  • 我想我会用字典来做这个;同时对两个数组进行步进,以 array1char 作为键,array2char 作为值填充字典。如果我添加了一些已经存在的东西(提示;TryGetValue 或 ContainsKey,然后再添加并相应地更改行为),那么已经存在的值必须与当前的 array2char 相同,否则提前退出循环并返回假。如果到达循环的末尾,则返回 true。字典包含您将打印的映射,如果它是真的。看起来像家庭作业;不打算编码它,GL!
  • 提示:使用Dictionary&lt;char, char&gt;

标签: c# arrays string algorithm sorting


【解决方案1】:

我认为您应该使用Dictionary&lt;char, char&gt; 的评论。但是您需要检查两个字符串中是否存在唯一映射,因此从 s1 到 s2 以及从 s2 到 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;
}

这是您的样品。请注意,我使用UniqueMapping 两次(如果true 在第一次之后):

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 小提琴:https://dotnetfiddle.net/4DtIyH

【讨论】:

  • 非常感谢您的回答,我还是有点问题。我使用了代码,但它说“程序不包含适合入口点的静态'Main'方法”。什么意思?你能帮帮我吗?
  • @LaurențiuCozma:当然,您需要将该方法添加到您的 Program 类中。保留你的 main 方法并在那里添加我的第二个代码 sn-p。
  • 我的代码不能正常工作,如果我保留我的代码,我仍然会得到错误的答案。
  • 你可以删除你的代码
  • 当我写“ala bala portocala”和“cuc dcuc efghficuc”时它可以工作,但是当我写“ala bala portocala”和“cuc dcuc efghijcuc”时它不起作用(我应该得到它“假”,但它仍然是真的)。​​
猜你喜欢
  • 1970-01-01
  • 2014-03-07
  • 2016-03-29
  • 2020-02-14
  • 2018-04-23
  • 2011-02-27
  • 2021-10-10
  • 1970-01-01
  • 2013-03-11
相关资源
最近更新 更多