代码拉取完成,页面将自动刷新
/***
* Algorithms for computing the list of permutations of a string and checking if two strings are anagrams of each other.
*/
using System;
using System.Collections.Generic;
namespace Algorithms.Strings
{
public static class Permutations
{
/// <summary>
/// Private Helper. Computes permutations recursively for string source.
/// </summary>
private static HashSet<string> _permutations(string source)
{
var perms = new HashSet<string>();
if (source.Length == 1)
{
perms.Add(source);
}
else if (source.Length > 1)
{
int lastIndex = source.Length - 1;
string lastChar = Convert.ToString(source[lastIndex]);
string substring = source.Substring(0, lastIndex);
perms = _mergePermutations(_permutations(substring), lastChar);
}
return perms;
}
/// <summary>
/// Private helper. Merges a set of permutations with a character.
/// </summary>
private static HashSet<string> _mergePermutations(HashSet<string> permutations, string character)
{
var merges = new HashSet<string>();
foreach (var perm in permutations)
{
for (int i = 0; i < perm.Length; ++i)
{
var newMerge = perm.Insert(i, character);
if (!merges.Contains(newMerge))
merges.Add(newMerge);
}
}
return merges;
}
/// <summary>
/// Computes the permutations of a string.
/// </summary>
public static HashSet<string> ComputeDistinct(string source)
{
return _permutations(source);
}
/// <summary>
/// Determines if the Other string is an anargram of the Source string.
/// </summary>
public static bool IsAnargram(string source, string other)
{
if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(other))
return false;
if (source.Length != other.Length)
return false;
if (source.Equals(other, StringComparison.Ordinal))
return true;
int len = source.Length;
// Hash set which will contains all the characters present in input source.
var hashSetSourceChars = new HashSet<char>();
var hashSetOtherChars = new HashSet<char>();
for (int i = 0; i < len; i++)
{
hashSetSourceChars.Add(source[i]);
hashSetOtherChars.Add(other[i]);
}
for (int i = 0; i < len; i++)
{
// Inputs are not Anargram if characers from *other are not present in *source.
if (!hashSetSourceChars.Contains(other[i])) return false;
if (!hashSetOtherChars.Contains(source[i])) return false;
}
return true;
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。