Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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