linq - Compare if the characters in a string are a subset of a second string in C# -
i'm working on game want check if characters of string contained in string. set first choice want account duplicates. example:
"met".iscontainedwithin("meet"); => true "meet".iscontainedwithin("met"); => false
a multi-set nice sounds c# doesn't have that. iteratively wondering if there simpler way (with linq perhaps). thanks!
edit:
i wasn't clear. want return true regardless of order of letters:
"git".iscontainedwithin("light")=> true "pall".iscontainedwithin("lamp")=> false
this works me:
public static bool iscontainedwithin(this string @this, string container) { var lookup = container.tolookup(c => c); return @this.tolookup(c => c).all(c => lookup[c.key].count() >= c.count()); }
i tested this:
var tests = new [] { "met".iscontainedwithin("meet"), "meet".iscontainedwithin("met"), "git".iscontainedwithin("light"), "pall".iscontainedwithin("lamp"), };
i got these results:
true false true false
Comments
Post a Comment