Advertisement
Guest User

Ali's code for Anagram problem

a guest
Jul 16th, 2014
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace NameAnalyser
  8. {
  9.     class Program
  10.     {
  11.         static void Main()
  12.         {
  13.             Analyser a = new Analyser(@"C:\Users\Ali\Desktop\names.txt");
  14.  
  15.             a.FindTuples();
  16.             int maxSize = a.Tuples.Max(x => x.Count);
  17.             int numberOfMax = a.Tuples.Where(x => x.Count == maxSize).ToArray<List<string>>().Length;
  18.  
  19.             Console.WriteLine(maxSize * numberOfMax);
  20.             Console.ReadLine();
  21.         }
  22.     }
  23.  
  24.     class Analyser
  25.     {
  26.         private List<string> names;
  27.  
  28.         public List<string> Names
  29.         {
  30.             get
  31.             {
  32.                 return names;
  33.             }
  34.         }
  35.  
  36.         private List<List<string>> tuples;
  37.        
  38.         public List<List<string>> Tuples
  39.         {
  40.             get
  41.             {
  42.                 return tuples;
  43.             }
  44.         }
  45.  
  46.         public Analyser(string path)
  47.         {
  48.             names = System.IO.File.ReadAllText(path).Split(new string[1] { "\",\"" }, StringSplitOptions.None).ToList<string>();
  49.             names[0] = names[0].Trim('"');
  50.             names [names.Count - 1] = names[names.Count - 1].Trim('"');
  51.         }
  52.  
  53.         public bool AreAnagrams(string first, string second)
  54.         {
  55.             var firstList = first.ToList<char>();
  56.             var secondList = second.ToList<char>();
  57.  
  58.             while (firstList.Count > 0)
  59.             {
  60.                 if (secondList.Contains(firstList[0]))
  61.                 {
  62.                     secondList.Remove(firstList[0]);
  63.                     firstList.Remove(firstList[0]);
  64.                 }
  65.                 else
  66.                 {
  67.                     return false;
  68.                 }
  69.             }
  70.  
  71.             return true;
  72.         }
  73.  
  74.         public void FindTuples()
  75.         {
  76.             tuples = new List<List<string>>();
  77.            
  78.             for (int i = 0; i < names.Count; i++)
  79.             {
  80.                 List<string> tuple = new List<string>();
  81.                 string name = names[0];
  82.                 names.RemoveAt(0);
  83.                 tuple.Add(name);
  84.  
  85.                 for (int j = 0; j < names.Count; j++)
  86.                 {
  87.                     if (name.Length == names[j].Length && AreAnagrams(names[j], name))
  88.                     {
  89.                         tuple.Add(names[j]);
  90.                         names.RemoveAt(j);
  91.                         j--;
  92.                     }
  93.                 }
  94.  
  95.                 tuples.Add(tuple);
  96.             }
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement