Skip to content

Commit

Permalink
Merge pull request #37 from ivandrofly/anagram-detecting
Browse files Browse the repository at this point in the history
[Anagram] - Add optimized method to detect Anagram.
  • Loading branch information
aalhour committed May 30, 2017
2 parents 93802a9 + 37c7ad8 commit b6b7c19
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions Algorithms/Strings/Permutations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,32 +66,28 @@ public static HashSet<string> ComputeDistinct(string Source)
/// <summary>
/// Determines if the Other string is an anargram of the Source string.
/// </summary>
public static bool IsAnargram(string Source, string Other)
public static bool IsAnargram(string source, string other)
{
if (string.IsNullOrEmpty(Source) || string.IsNullOrEmpty(Other))
if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(other))
return false;
else if (Source.Length != Other.Length)
else if (source.Length != other.Length)
return false;
else if (Source == Other)
else if (source.Equals(other, StringComparison.Ordinal))
return true;

// Begin the Anagram check
// Covnert strings to character arrays
// O(N) space complexity
var sourceCharArray = Source.ToCharArray();
var otherCharArray = Other.ToCharArray();

// Sort both character arrays in place using heapsort
// O(N log N) operation
sourceCharArray.HeapSort<char>(Comparer<char>.Default);
otherCharArray.HeapSort<char>(Comparer<char>.Default);

// One pass scan
// O(N) operation
for (int i = 0; i < sourceCharArray.Length; ++i)
if (sourceCharArray[i] != otherCharArray[i])
return false;

int len = source.Length;
// Hash set which will contains all the characters present in input souce.
var hashSetSourceChars = new HashSet<char>();
for (int i = 0; i < len; i++)
{
hashSetSourceChars.Add(source[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 (!hashSetOther.Contains(source[i])) return false;
}
return true;
}
}
Expand Down

0 comments on commit b6b7c19

Please sign in to comment.