带有特殊字符的C#单词排列

问题描述 投票:0回答:1

我正在尝试创建Word“密码”的排列,我已设法使用下面的代码创建该单词的所有排列。目前这适用于大写和小写。我需要包含具有特殊字符的排列,例如我将拥有“p @ ssword”。我用“@”和“o”“0”替换“a”的地方。

知道如何扩展我的实现吗?

 public static void WritePermutations(string pwd)
    {
        pwd = pwd.ToLower();
        var myDict = new Dictionary<int, string>();
        int Count = 0;

        var results =
            from e in Enumerable.Range(0, 1 << pwd.Length)
            let p =
            from b in Enumerable.Range(0, pwd.Length)
            select (e & (1 << b)) == 0 ? (char?)null : pwd[b]
            select string.Join(string.Empty, p);

        foreach (string s in results)
        {
            string newValue = pwd;
            s.ToLower();
            foreach (char c in s)
            {
                var Old = c.ToString().ToLower();
                var New = c.ToString().ToUpper();
                newValue = ReplaceFirstOccurrence(newValue, Old, New);
                Count++;
            }

            myDict.Add(Count, newValue);
        }

            foreach (var cred in myDict)
            {

                Console.WriteLine(cred.Value);
            }
    }

  public static string ReplaceFirstOccurrence(string Source, string Find, string Replace)
    {
        int Place = Source.IndexOf(Find);
        string result = Source.Remove(Place, Find.Length).Insert(Place, Replace);
        return result;
    }
c# .net permutation
1个回答
2
投票

如何从这些声明和函数开始:

    List<HashSet<char>> _charCombinations = new List<HashSet<char>> {
        new HashSet<char> {'a','@'},
        new HashSet<char> {'o', '0'},
    };

    HashSet<char> GetAlternatives(char c)
    {
        var result = new HashSet<char>();
        foreach (var hashSet in _charCombinations)
        {
            if (hashSet.Contains(c))
            {
                foreach (char c2 in hashSet)
                    result.Add(c2);
            }
        }

        if (char.IsLetter(c))
        {
            result.Add((String.Empty + c).ToUpper()[0]);
            result.Add((String.Empty + c).ToLower()[0]);
        }
        else if (false) // any other char.Is-based logic
        {

        }
        result.Add(c);
        return result;
    }

    IEnumerable<string> GetTransformations(string s, int start)
    {
        char c = s[start - 1];
        foreach (var c2 in GetAlternatives(c))
        {
            if (start == s.Length)
                yield return String.Empty + c2;
            else
            {
                var e = GetTransformations(s, start + 1).GetEnumerator();
                while (e.MoveNext())
                    yield return  c2 + e.Current;

            }
        }
    }

然后你可以像这样使用它们:

        var e = GetTransformations("password", 1).GetEnumerator();
        var result = new List<string>();
        while (e.MoveNext())
            result.Add(e.Current);
        result.Sort((a,b) => string.CompareOrdinal(a, b));

这会产生576个字符串(这里列出的时间太长),这正是您对8字母字的期望,其中6个字符有2种可能性,另外2个有3种可能性,即2x2x2x2x2x2x3x3

© www.soinside.com 2019 - 2024. All rights reserved.