re: Daily Coding Puzzles - Nov 4th - Nov 9th VIEW POST

re: Thursday Scramblies (5 KYU): Complete the function scramble(str1, str2) that returns true if a portion of str1 characters can be rearranged to m...

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;
code of conduct - report abuse