SHARE
TWEET

Untitled

a guest Aug 25th, 2019 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4.  
  5. public class Program
  6. {
  7.     public static void Main()
  8.     {
  9.         foreach (var test in GetTestCases()) {
  10.             var result = IsAnagramPair(test.First, test.Second);
  11.             Console.WriteLine((result == test.Expected) ? "Success" : "FAIL");
  12.         }
  13.     }
  14.        
  15.     // Assumptions:
  16.     // Word or phrase only uses English alphabet.
  17.     // Test cases don't pass in null for first and second.
  18.     public static bool IsAnagramPair(string first, string second)
  19.     {
  20.         var firstPhrase = RemoveNonAlphabeticCharacter(first);
  21.         var secondPhrase = RemoveNonAlphabeticCharacter(second);
  22.        
  23.         if(string.IsNullOrEmpty(firstPhrase) || string.IsNullOrEmpty(secondPhrase) || firstPhrase.Length != secondPhrase.Length){
  24.             return false;
  25.         }
  26.        
  27.         var firstPhraseSorted = SortLetter(firstPhrase.ToLower());
  28.         var secondPhraseSorted = SortLetter(secondPhrase.ToLower());
  29.        
  30.         return firstPhraseSorted == secondPhraseSorted;
  31.     }
  32.    
  33.     public static string RemoveNonAlphabeticCharacter(string phrase) {     
  34.         return Regex.Replace(phrase, @"[^a-z]", "", RegexOptions.IgnoreCase);
  35.     }
  36.    
  37.     public static string SortLetter(string phrase){
  38.         var letters = phrase.ToCharArray();
  39.         Array.Sort(letters);
  40.         return new String(letters);
  41.     }
  42.    
  43.     public static IEnumerable<TestCase> GetTestCases()
  44.     {
  45.         yield return new TestCase { First = "add", Second = "dad", Expected = true };
  46.         yield return new TestCase { First = "aad", Second = "dad", Expected = false };
  47.         yield return new TestCase { First = "Astronomer", Second = "Moon starer", Expected = true };
  48.         yield return new TestCase { First = "thorough", Second = "through", Expected = false };
  49.         yield return new TestCase { First = "Jim Morrison", Second = "Mr. Mojo Risin'", Expected = true };
  50.         yield return new TestCase { First = "", Second = "", Expected = false };
  51.         yield return new TestCase { First = "   ", Second = "   ", Expected = false };
  52.         yield return new TestCase { First = "   1", Second = "1 ", Expected = false };
  53.         yield return new TestCase { First = "\n he\tl o   l", Second = " hello\\n", Expected = false };
  54.         yield return new TestCase { First = "\n he\tl o   l", Second = " hello\n", Expected = true };
  55.     }
  56. }
  57.  
  58. public class TestCase
  59. {
  60.     public string First { get; set; }
  61.     public string Second { get; set; }
  62.     public bool Expected { get; set; }
  63. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top