TEST PROBLEM (difficulty = easy) ANAGRAMS
An anagram is a word formed from another by rearranging its letters, using all the original letters exactly once; for example, orchestracan be rearranged into carthorse.
Write a function that checks if two words are each other's anagrams.
For example, AreStringsAnagrams("momdad", "dadmom") should return true as arguments are anagrams.
POSSIBLE SOLUTION (100%)
using System;
public class AreAnagrams
{
public static bool AreStringsAnagrams(string a, string b)
{
if (string.IsNullOrEmpty(a) || string.IsNullOrEmpty(b) || (a.Length != b.Length))return false;
foreach (char c in b)
{
int i= a.IndexOf(c);
if (i>=0) a = a.Remove(i,1);
else return false;
}
return string.IsNullOrEmpty(a);
}
public static void Main(string[] args)
{
Console.WriteLine(AreStringsAnagrams("momdad", "dadmom"));
}
}
PR
No comments:
Post a Comment