如何检查,如果字母是字符串? [英] How check if letters are in string?

查看:119
本文介绍了如何检查,如果字母是字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这挺难的问题要问,但是我会尽力。 我有我的4个字母 M U 先按g 0 。我也有免费的串词(取值)。
Let'say: OG OGG muogss 。我在寻找任何明智的方法来检查,如果我可以构建字(取值),只用我的信。请大家注意,我们使用了一次先按g ,我们将无法再使用。

It quite hard question to ask but I will try. I have my 4 letters m u g o . I have also free string word(s).
Let'say: og ogg muogss. I am looking for any wise method to check if I can construct word(s) using only my letters. Please take notice that we used once g we won't be able to use it again.

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**

所以我的策略是把我的信件,字符数组,并删除逐一检查,建立这个词多少左(取值)。但是,它可以使用某种方式的几行字,我不知道 - 正则表达式?

So my tactic is take my letters to char array and remove one by one and check how many left to build the word(s). But is it possible to use somehow in few lines, i do not know - regex ?

推荐答案

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

your method is only a few lines...

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

这篇关于如何检查,如果字母是字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆