我有两个字符串:第一个字符串的值为 "catdog",第二个字符串的值为 "got"。
我正在尝试查找一个正则表达式,告诉我是否在 "catdog" 中包含了 "got" 的字母。我特别想避免有重复字母的情况。例如,我知道 "got" 是匹配的,但是 "gott" 不匹配,因为 "catdog" 中没有两个 "t"。
编辑:
根据下面 Adam 的回复,这是我在解决方案中使用的 C# 代码。感谢所有回复的人。
注意:我必须将 char 转换为 int 并减去 97 才能获得数组的适当索引。在我的情况下,字母始终是小写。
我正在尝试查找一个正则表达式,告诉我是否在 "catdog" 中包含了 "got" 的字母。我特别想避免有重复字母的情况。例如,我知道 "got" 是匹配的,但是 "gott" 不匹配,因为 "catdog" 中没有两个 "t"。
编辑:
根据下面 Adam 的回复,这是我在解决方案中使用的 C# 代码。感谢所有回复的人。
注意:我必须将 char 转换为 int 并减去 97 才能获得数组的适当索引。在我的情况下,字母始终是小写。
private bool CompareParts(string a, string b)
{
int[] count1 = new int[26];
int[] count2 = new int[26];
foreach (var item in a.ToCharArray())
count1[(int)item - 97]++;
foreach (var item in b.ToCharArray())
count2[(int)item - 97]++;
for (int i = 0; i < count1.Length; i++)
if(count2[i] > count1[i])
return false;
return true;
}