【问题标题】:C# - Check if string contains characters of another string at the same orderC# - 检查字符串是否以相同的顺序包含另一个字符串的字符
【发布时间】:2015-11-30 20:01:06
【问题描述】:

我想检查一个字符串是否包含另一个字符串的字符(返回真或假),但它需要以“正确”的顺序排列,但不一定是连续的。

例子:

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

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

“arandomword”这个词包含“road”这个词的字母,但不能写出来,因为它们的顺序不对。

有人吗?

【问题讨论】:

  • “amaram”怎么样?
  • 我在一些编程竞赛网站上看到了这个任务。你在作弊吗?

标签: c# string


【解决方案1】:

使用正则表达式。在 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”的代码。还需要考虑包含需要分隔的特殊字符的字符串。
  • 或者只是return string.Join(".*", chars.Select(c => Regex.Escape(c.ToString())));,你可以传入string而不是char[],因为string实现IEnumerable<char>。否则非常好。
  • 您可以接受string作为参数,这不会强制调用者转换为char-array,因为string实现了IEnumerable
【解决方案2】:

你可以像这样定义一个扩展方法:

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

【讨论】:

  • 你可以使用IndexOf的重载告诉它索引开始查找,然后你只需要将结果与-1进行比较
  • @juharr 的建议,不仅是一个改进,还修复了word是“ABA”和otherword是“BA”的边界情况
  • 我认为这在循环中缺少对 lastIndex 的赋值。
【解决方案3】:

这样的?

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;
}

【讨论】:

    【解决方案4】:

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

    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;
    

    【讨论】:

      【解决方案5】:

      感谢所有回复。我已经尝试并尝试过,我按照自己的方式做到了。绝对不是最短的方法,但至少它是有效的:

          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;
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-01-08
        • 2013-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多