C# - 检查一个字符串是否按照另一个字符串的顺序包含相应的字符

6

我希望检查一个字符串是否包含另一个字符串的字符(返回 true 或 false),但需要以“正确”的顺序,但不一定是连续的。

例如:

String firstWord = "arm";
String secondWord = "arandomword"; //TRUE - ARandoMword

String thirdWord = "road"; //FALSE - ARanDOmword

单词 "arandomword" 包含了单词 "road" 的字母,但是无法写出来,因为它们的顺序不正确。

有人能帮忙吗?


“amaram” 是什么意思? - Giorgi Nakeuri
我在某些编程竞赛网站上看到了这个任务。你在作弊吗? - Vlad
5个回答

6

使用正则表达式。在LinqPad中编写简单的代码以通过您的测试:

void Main()
{
    String firstWord = "arm";
    String secondWord = "arandomword"; //TRUE - ARandoMword

    String thirdWord = "road";

    Regex.IsMatch(secondWord,makeRegex(firstWord.ToCharArray())).Dump();
}

// Define other methods and classes here
String makeRegex(char[] chars)
{
    StringBuilder sb = new StringBuilder();
    foreach (var element in chars.Select(c => Regex.Escape(c.ToString()))
        .Select(c => c + ".*"))
    {
        sb.Append(element);
    }
    return sb.ToString();
}

你真的应该展示一下将“arm”转换为“a.*r.*m”的代码。同时,需要考虑包含需要分隔符的特殊字符的字符串。 - juharr
或者只需返回string.Join(“.*”,chars.Select(c => Regex.Escape(c.ToString())));,您可以直接传递一个string,而不是一个char [],因为string实现了IEnumerable <char>。否则非常好。 - juharr
你可以接受string作为参数,这不会强制调用者将其转换为char数组,因为string实现了IEnumerable - nozzleman

5

您可以像这样定义扩展方法:

public static class StringExtensions
{
    public static bool ContainsWord(this string word, string otherword)
    {
        int currentIndex = 0;

        foreach(var character in otherword)
        {
            if ((currentIndex = word.IndexOf(character, currentIndex)) == -1)
                return false;
        }

        return true;
    }
}

并将其称为表达性:

String firstWord = "arm";
String secondWord = "arandomword"; //TRUE - ARandoMword
String thirdWord = "road"; //FALSE - ARanDOmword

var ret = secondWord.ContainsWord(firstWord); // true
var ret2 = thirdWord.ContainsWord(firstWord); // false

1
你可以使用IndexOf的重载方法来告诉它开始查找的索引,然后你只需要将结果与-1进行比较。 - juharr
1
@juharr的建议不仅是一种改进,而且还修复了边界情况,其中word为"ABA",otherword为"BA"。 - Fede
1
我认为在循环中缺少对lastIndex的赋值。 - Ron Deijkers

2
像这样的内容吗?
bool HasLettersInOrder(string firstWord, string secondWord)
{
    int lastPos = -1;
    foreach (char c in firstWord)
    {
        lastPos++;
        while (lastPos < secondWord.Length && secondWord[lastPos] != c)
            lastPos++;
        if (lastPos == secondWord.Length)
            return false;
    }
    return true;
}

2

我现在无法检查,但大致如下:

int i = 0, j = 0;

while(i < first.Length && j < second.Length)
{
   while(first[i] != second[j] && j < second.Length) j++;
   i++;
   j++
}

bool b = i == first.Length;

0

感谢所有的回复。我已经尝试了很多次,最终用自己的方式完成了它。虽然这不是最短的方法,但至少它能够正常工作:

    public static Boolean checkWords(String smallerWord, String biggerWord)
    {
        int position = 0;
        bool gotFirst = false;
        bool gotAnother = false;
        int positionBigger = 0;
        String word = "";

        for(int positionSmaller = 0; positionSmaller < smallerWord.Length; positionSmaller++)
        {
            if(!gotFirst)
            {
                if(biggerWord.Contains(smallerWord[positionSmaller].ToString()))
                {
                    position = biggerWord.IndexOf(smallerWord[positionSmaller].ToString());
                    gotFirst = true;
                    word = smallerWord[positionSmaller].ToString();
                }
            }
            else
            {
                gotAnother = false;
                positionBigger = position + 1;

                while(!gotAnother)
                {
                    if(positionBigger < biggerWord.Length)
                    {
                        if(biggerWord[positionBigger].ToString().Equals(smallerWord[positionSmaller].ToString()))
                        {
                            position = positionBigger;
                            gotAnother = true;
                            word += smallerWord[positionSmaller].ToString();
                        }
                        else
                        {
                            positionBigger++;
                        }
                    }
                    else
                    {
                        gotAnother = true;
                    }
                }
            }
        }

        if(smallerWord.Equals(word))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接