DEV Community

Discussion on: Daily Coding Puzzles - Nov 4th - Nov 9th

Collapse
 
dance2die profile image
Sung M. Kim

Solved it awhile ago in C#.

Warning: Really ugly...

using System;
using System.Collections.Generic;
using System.Linq;


  public class Scramblies
  {
    public static bool Scramble(string s1, string s2)
    {
      Func<string, Dictionary<char, int>> toMap = s =>
        s.GroupBy(c => c)
        .Select(g => new { g.Key, Count = g.Count() })
        .ToDictionary(pair => pair.Key, pair => pair.Count);

      var map1 = toMap(s1);
      var map2 = toMap(s2);

      foreach (KeyValuePair<char, int> p2 in map2)
      {
        if (!map1.ContainsKey(p2.Key)) return false;

        if (map1[p2.Key] < p2.Value) return false;
      }

      return true;
    }
  }