C# 如何检查两个字符串是否包含相同的字母
我目前正在完成一个C#编程挑战,我被困在主要部分。应用程序必须包含两个单词,并查看它们是否包含相同的字母。如何检查C# 如何检查两个字符串是否包含相同的字母,c#,regex,C#,Regex,我目前正在完成一个C#编程挑战,我被困在主要部分。应用程序必须包含两个单词,并查看它们是否包含相同的字母。如何检查input1和input2是否包含相同的字母 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Text.RegularExpressions; namespace Words
input1
和input2
是否包含相同的字母
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace Words_With_Enemies
{
class Program
{
static string input1, input2;
public void findLetters()
{
bool regexWord1 = Regex.IsMatch(input1, @"^[a-zA-Z]+$");
}
static void Main(string[] args)
{
Console.WriteLine("Please enter two words");
input1 = Console.ReadLine();
input2 = Console.ReadLine();
Console.WriteLine("You have entered the following two words:");
Console.WriteLine(input1);
Console.WriteLine(input2);
Console.ReadLine();
}
}
}
如果要查找两个字符串中所有字母都相同的,则可以使用from
System.Linq
命名空间:
bool result = input1.Except(input2).Any();
如果它们不包含相同的字母,它将返回true
此类输入的输出如下所示:
苹果,苹果=>True苹果、香蕉=>真的
苹果,Alep=>False
苹果,苹果=>错误 更新:
如果要查找两个字符串中是否包含任何字母,则可以使用: 如果它们至少包含一个相同的字母,它将返回
true
。
此类输入的输出如下所示:
此类输入的输出如下所示:
苹果,苹果=>True苹果,香蕉=>True
苹果,Alep=> 真
苹果、洋葱=>假
其他详细信息:
如果要以不敏感的方式查找结果大小写,则可以将两个代码更改为:
bool result = input1.ToLowerInvariant().Except(input2.ToLowerInvariant()).Any();
bool result = input1.ToLowerInvariant().Intersect(input2.ToLowerInvariant()).Any();
如果要查找两个字符串中所有字母都相同的,则可以使用from
System.Linq
命名空间:
bool result = input1.Except(input2).Any();
如果它们不包含相同的字母,它将返回true
此类输入的输出如下所示:
苹果,苹果=>True苹果、香蕉=>真的
苹果,Alep=>False
苹果,苹果=>错误 更新:
如果要查找两个字符串中是否包含任何字母,则可以使用: 如果它们至少包含一个相同的字母,它将返回
true
。
此类输入的输出如下所示:
此类输入的输出如下所示:
苹果,苹果=>True苹果,香蕉=>True
苹果,Alep=> 真
苹果、洋葱=>假
其他详细信息:
如果要以不敏感的方式查找结果大小写,则可以将两个代码更改为:
bool result = input1.ToLowerInvariant().Except(input2.ToLowerInvariant()).Any();
bool result = input1.ToLowerInvariant().Intersect(input2.ToLowerInvariant()).Any();
基本上,您需要检查两个字符串是否是置换
static private bool isPermutation(string myString1, string myString2)
{
//If the strings are different lengths, they are not
//permutations.
if (myString1.Length != myString2.Length) return false;
//Create an array to count the number of each specific
//character in the strings.
int[] characterCount = new int[256];
int charIndex;
//Populate the array with default value 0.
for (int index = 0; index < 256; index++)
{
characterCount[index] = 0;
}
//Count the number of each character in the first
//string. Add the count to the array.
foreach (char myChar in myString1.ToCharArray())
{
charIndex = (int)myChar;
characterCount[charIndex]++;
}
//Count the number of each character in the second
//string. Subtract the count from the array.
foreach (char myChar in myString2.ToCharArray())
{
charIndex = (int)myChar;
characterCount[charIndex]--;
}
//If the strings are permutations, then each character
//would be added to our character count array and then
//subtracted. If all values in this array are not 0
//then the strings are not permutations of each other.
for (int index = 0; index < 256; index++)
{
if (characterCount[index] != 0) return false;
}
//The strings are permutations of each other.
return true;
}
}
}
静态私有bool isPermutation(字符串myString1、字符串myString2)
{
//如果字符串长度不同,则不相同
//排列。
if(myString1.Length!=myString2.Length)返回false;
//创建一个数组来计算每个特定
//字符串中的字符。
int[]characterCount=新int[256];
内查林德斯;
//使用默认值0填充数组。
对于(int-index=0;index<256;index++)
{
characterCount[索引]=0;
}
//计算第一个字符中每个字符的数目
//字符串。将计数添加到数组中。
foreach(myString1.ToCharArray()中的char myChar)
{
charIndex=(int)myChar;
characterCount[charIndex]++;
}
//计算第二个字符中每个字符的数目
//字符串。从数组中减去计数。
foreach(myString2.ToCharArray()中的char myChar)
{
charIndex=(int)myChar;
characterCount[charIndex]--;
}
//如果字符串是排列,则每个字符
//将被添加到我们的字符计数数组中,然后
//减去。如果此数组中的所有值都不是0
//那么字符串就不是彼此的排列。
对于(int-index=0;index<256;index++)
{
如果(characterCount[index]!=0)返回false;
}
//这些字符串是彼此的排列。
返回true;
}
}
}
基本上,您需要检查两个字符串是否为排列
static private bool isPermutation(string myString1, string myString2)
{
//If the strings are different lengths, they are not
//permutations.
if (myString1.Length != myString2.Length) return false;
//Create an array to count the number of each specific
//character in the strings.
int[] characterCount = new int[256];
int charIndex;
//Populate the array with default value 0.
for (int index = 0; index < 256; index++)
{
characterCount[index] = 0;
}
//Count the number of each character in the first
//string. Add the count to the array.
foreach (char myChar in myString1.ToCharArray())
{
charIndex = (int)myChar;
characterCount[charIndex]++;
}
//Count the number of each character in the second
//string. Subtract the count from the array.
foreach (char myChar in myString2.ToCharArray())
{
charIndex = (int)myChar;
characterCount[charIndex]--;
}
//If the strings are permutations, then each character
//would be added to our character count array and then
//subtracted. If all values in this array are not 0
//then the strings are not permutations of each other.
for (int index = 0; index < 256; index++)
{
if (characterCount[index] != 0) return false;
}
//The strings are permutations of each other.
return true;
}
}
}
静态私有bool isPermutation(字符串myString1、字符串myString2)
{
//如果字符串长度不同,则不相同
//排列。
if(myString1.Length!=myString2.Length)返回false;
//创建一个数组来计算每个特定
//字符串中的字符。
int[]characterCount=新int[256];
内查林德斯;
//使用默认值0填充数组。
对于(int-index=0;index<256;index++)
{
characterCount[索引]=0;
}
//计算第一个字符中每个字符的数目
//字符串。将计数添加到数组中。
foreach(myString1.ToCharArray()中的char myChar)
{
charIndex=(int)myChar;
characterCount[charIndex]++;
}
//计算第二个字符中每个字符的数目
//字符串。从数组中减去计数。
foreach(myString2.ToCharArray()中的char myChar)
{
charIndex=(int)myChar;
characterCount[charIndex]--;
}
//如果字符串是排列,则每个字符
//将被添加到我们的字符计数数组中,然后
//减去。如果此数组中的所有值都不是0
//那么字符串就不是彼此的排列。
对于(int-index=0;index<256;index++)
{
如果(characterCount[index]!=0)返回false;
}
//这些字符串是彼此的排列。
返回true;
}
}
}
除了基于Linq的解决方案外,还有很多蛮力解决方案可以解决这个问题。字符串是一个字符数组,应该足以作为线索。例如,在一个字符串中循环,在另一个字符串中使用contains。除了基于Linq的解决方案外,还有很多蛮力解决方案。字符串是一个字符数组,应该足以作为线索。循环一个字符串并使用conta