【问题标题】:How check if letters are in string?如何检查字母是否在字符串中?
【发布时间】:2011-12-24 20:40:59
【问题描述】:

这是一个很难问的问题,但我会尝试。 我有我的 4 个字母 m u g o 。我也有免费的字符串字(s)。
比方说:ogoggmuogss。我正在寻找任何明智的方法来检查我是否可以仅使用我的字母来构造 word(s)。请注意,我们使用过一次g,我们将无法再次使用它。

og - possible because we need only **g** and **o**
ogg - not possible we took **o** and **g**, need the second **g**
muogss - not possible we took all, need also additional **s**

所以我的策略是把我的字母放到 char 数组中,一个一个地删除,然后检查剩下多少个来构建单词(s)。但是是否可以在几行中以某种方式使用,我不知道 - 正则表达式?

【问题讨论】:

  • "有些人在遇到问题时会想“我知道,我会使用正则表达式。”现在他们有两个问题。 - 杰米·扎温斯基
  • 这可能会有所帮助:stackoverflow.com/questions/541954
  • 世界上只有10种人:懂二进制的和不懂二进制的——小题外话:)

标签: c# string algorithm letters


【解决方案1】:

你的方法只有几行......

   public static bool CanBeMadeFrom(string word, string letters)
    {
        foreach (var i in word.Select(c => letters.IndexOf(c, 0)))
        {
            if (i == -1) return false;
            letters = letters.Remove(i, 1);
        }
        return true;
    }

【讨论】:

  • 这个 LINQ 很棒 :) 谢谢先生!
【解决方案2】:

这是一个简单的方法: 对于您的源词,创建一个大小为 26 的数组,并使用它来计算每个字母出现的次数。 对字典中的每个单词执行相同的操作。 然后比较两者。 如果每个字母在字典单词中出现的次数少于或等于源单词的次数,则可以使用它来制作该单词。如果没有,那么它不能。

C-Sharpish 伪代码:(可能无法按书面方式编译)

/** Converts characters to a 0 to 25 code representing alphabet position.
    This is specific to the English language and would need to be modified if used
    for other languages. */
int charToLetter(char c) {
    return Char.ToUpper(c)-'A';
}

/** Given a source word and an array of other words to check, returns all 
    words from the array which can be made from the letters of the source word. */
ArrayList<string> checkSubWords(string source, string[] dictionary) {

    ArrayList<string> output = new ArrayList<string>();

    // Stores how many of each letter are in the source word.
    int[] sourcecount = new int[26];  // Should initialize to 0, automatically
    foreach (char c in source) {
        sourcecount[c]++;
    }

    foreach (string s in dictionary) {

        // Stores how many of each letter are in the dictionary word.
        int[] dictcount = new int[26]; // Should initialize to 0, automatically
        foreach (char c in s) {
            dictcount[c]++;
        }

        // Then we check that there exist no letters which appear more in the 
        // dictionary word than the source word.
        boolean isSubword = true;
        for (int i=0;i<26;i++) {
            if (dictcount[i] > sourcecount[i]) {
                isSubword = false;
            }
        }

        // If they're all less than or equal to, then we add it to the output.
        if (isSubWord) {
            output.add(s);
        }
    }
    return output;
}

【讨论】:

  • @L.B 您可以通过更改字母的大小和 charToLetter 函数来修改它以适用于其他拼音字母。因为我不能很快识别出哪个字母是哪个,我不确定那是简体中文、繁体中文、日文汉字、平假名还是片假名。但如果它是大的之一,您可能还想切换到哈希表,否则您将需要检查一个非常非常稀疏的数组。这会使代码稍微复杂一些,但同样的基本方法也可以工作。
  • @KeithIrwin 你太认真了。这只是一种乐趣。但是 UTF32 的哈希表/数组会非常大:)。
【解决方案3】:

如果您对单词的定义是可用字符的任意排列,那么您为什么需要正则表达式?只需确保您使用每个字符一次。正则表达式不知道什么是“正确的单词”,最好避免在算法中使用无效字符,而不是使用正则表达式AND来确保你没有不要使用它们。

【讨论】:

    猜你喜欢
    • 2013-04-01
    • 2020-12-12
    • 2013-08-10
    • 1970-01-01
    • 2019-03-31
    • 2014-01-01
    • 2015-01-04
    • 1970-01-01
    相关资源
    最近更新 更多