我是一个编程新手,我被一个练习困住了。
我需要确定第一个字符串中的每个字符是否可以被第二个字符串中的字符唯一地替换。两个字符串的长度相等。
例如:"aabc ea"one_answers"ddtf hd",则结果应为:
True
a => d
b => t
c => f
=>
e => h
如果我有"abac "ddtf ",结果应为:
假
Since "abac "one_answers"滴滴涕";不要找一个独一无二的替代品。
using System;
namespace UniqueStrings
{
class Program
{
static void Main(string[] args)
{
string firstPhrase = Console.ReadLine();
string secondPhrase = Console.ReadLine();
bool result = false;
int charsCount = 0;
char[] firstPhraseChars = new char[firstPhrase.Length];
char[] secondPhraseChars = new char[secondPhrase.Length];
if (firstPhrase.Length != secondPhrase.Length)
{
result = false;
}
for (int i = 0; i < firstPhrase.Length; i++)
{
if (firstPhrase[i] == firstPhraseChars[i])
{
firstPhraseChars[i] = firstPhrase[i];
secondPhraseChars[i] = secondPhrase[i];
}
for (int j = 0; j < secondPhrase.Length; j++)
{
if (secondPhrase[j] == secondPhraseChars[j])
{
firstPhraseChars[j] = firstPhrase[j];
secondPhraseChars[j] = secondPhrase[j];
result = false;
}
else
{
result = true;
}
}
}
for (int i = 0; i < firstPhrase.Length; i++)
{
if (result == false)
{
firstPhraseChars[charsCount] = firstPhrase[i];
secondPhraseChars[charsCount] = secondPhrase[i];
charsCount++;
}
}
if (result == false)
Console.WriteLine(result);
else
{
Console.WriteLine(result);
for (int i = 0; i < firstPhrase.Length; i++)
{
Console.WriteLine(firstPhrase[i] + " => " + secondPhrase[i]);
}
}
Console.Read();
}
}
}
有人能告诉我我做错了什么吗?我不知道了,我觉得这个代码永远不会工作。这事总得有个解决办法,我不明白。
我不应该使用LINQ, list或dictionary,只能使用System。
如果有人有其他问题,请提出来。
非优化解示例
using System;
namespace UniqueStrings
{
class Program
{
static bool CheckStringSimilarity(string firstPhrase, string secondPhrase)
{
if (firstPhrase.Length != secondPhrase.Length)
{
return false;
}
var length = firstPhrase.Length;
for (var i =0; i<length; i++)
{
for(var j=0; j<length; j++ )
{
if((firstPhrase[i] == firstPhrase[j]) && (secondPhrase[i] != secondPhrase[j]))
{
return false;
}
if((firstPhrase[i] != firstPhrase[j]) && (secondPhrase[i] == secondPhrase[j]))
{
return false;
}
}
}
return true;
}
static void Main(string[] args)
{
Console.WriteLine($"CheckStringSimilarity('aaa','bbb') = {CheckStringSimilarity("aaa", "bbb")}");
Console.WriteLine($"CheckStringSimilarity('aaab','bbbc') = {CheckStringSimilarity("aaab", "bbbc")}");
Console.WriteLine($"CheckStringSimilarity('rrt','aze') = {CheckStringSimilarity("rrt", "aze")}");
Console.WriteLine($"CheckStringSimilarity('rrt dd','aad aa') = {CheckStringSimilarity("rrt dd", "aad aa")}");
}
}
}
演示在第一个for循环中,结果总是为真,因为两个if语句总是返回false。当if语句比较它们的值时&;firstphrasecars [i]&;和"secondPhraseChars [j]";总是空的,因为在比较之前从来没有在这些数组中放入任何东西。
希望这篇文章能帮到你,但不要透露太多;)
你可以找一个反例:如果你找到了,通讯员不存在:
private static bool HasCorrespondence(string left, string right) {
if (left == null)
return right == null;
if (right == null)
return false;
if (left.Length != right.Length)
return false;
// known correspondence
Dictionary<char, char> correspondence = new Dictionary<char, char>();
for (int i = 0; i < left.Length; ++i)
if (correspondence.TryGetValue(left[i], out char expected)) {
// counter example: we want expected, but have right[i]
if (expected != right[i])
return false;
}
else
// we have nothing for left[i], so we can add (left[i], right[i]) pair
correspondence.Add(left[i], right[i]);
// no counter example exists, return true
return true;
}
如果禁止使用Dictionary
,可以通过array:
private static bool HasCorrespondence(string left, string right) {
if (left == null)
return right == null;
if (right == null)
return false;
if (left.Length != right.Length)
return false;
int[] correspondence = new int[char.MaxValue];
for (int i = 0; i < left.Length; ++i)
if (correspondence[left[i]] != 0) {
if (correspondence[left[i]] != right[i])
return false;
}
else
correspondence[left[i]] = right[i];
return true;
}
如果您正在寻找一对一的对应,您只需检查两次">
private static bool HasOneToOne(string left, string right) =>
HasCorrespondence(left, right) &&
HasCorrespondence(right, left);