Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created March 27, 2016 20:53
Show Gist options
  • Save jianminchen/8f6bd4631f0b5f0bdee7 to your computer and use it in GitHub Desktop.
Save jianminchen/8f6bd4631f0b5f0bdee7 to your computer and use it in GitHub Desktop.
SherlockAndAnagram - use advanced C# programming expression - Lambda ?
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution
{
static void Main(String[] args)
{
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
int cases = int.Parse(Console.ReadLine());
for (int c = 0; c < cases; c++)
{
var s = Console.ReadLine();
var d = new Dictionary<string, int>();
for (int i = 0; i < s.Length; i++)
{
for (int j =1; j + i <=s.Length; j++)
{
var ss = new string(s.Substring(i, j).OrderBy(x=>x).ToArray());
if (d.ContainsKey(ss))
d[ss]++;
else
d[ss] = 1;
}
}
int total = 0;
foreach (var kvp in d)
{
for (int j = 1; j < kvp.Value; j++)
{
total += j;
}
}
Console.WriteLine(total);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment