C#,320个字节
using System.Linq;s=>u=>p(u.ToArray(),0,u.Length-1).Any(p=>s.Contains(p));w=(c,a,b)=>{if (a!=b)(var t=c[a];c[a]=c[b];c[b]=t;)};System.Collections.Generic.IEnumerable<string>p(char[]l,int k,int m){if(k==m)yield return new string(l);else for(int i=k;i<=m;){w(l,k,i);foreach(var c in p(l,k+1,m))yield return c;w(l,k,i++);}}
我敢肯定,计算排列的时间会短很多,但目前还不知道如何。
格式化/完整版本:
void test()
{
Func<string, Func<string, bool>> f = s => u =>
p(u.ToArray(), 0, u.Length - 1).Any(p => s.Contains(p));
Console.WriteLine(f("Hello World!")("d!rl"));
Console.WriteLine(f("Programming Puzzles & Code Golf")("Pog"));
Console.WriteLine(f("asdfhytgju1234")("ghjuyt"));
}
System.Collections.Generic.IEnumerable<string>p(char[] l, int k, int m)
{
Action<char[], int, int> w = (c, a, b) =>
{
if (a != b)
{
var t = c[a];
c[a] = c[b];
c[b] = t;
}
};
if (k == m)
yield return new string(l);
else
for (int i = k; i <= m;)
{
w(l, k, i);
foreach (var c in p(l, k + 1, m))
yield return c;
w(l, k, i++);
}
}