Skip to content

Instantly share code, notes, and snippets.

@phaniram
Created February 1, 2012 20:50
Show Gist options
  • Save phaniram/1719231 to your computer and use it in GitHub Desktop.
Save phaniram/1719231 to your computer and use it in GitHub Desktop.
Codechef-FebContest-Word Counting
package com.cyp.codechef.febcontest;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Scanner;
class WCOUNT {
int len;
BigInteger mod;
HashMap<Character,Integer> hm=new HashMap<Character,Integer>();
public WCOUNT(String word) {
len=word.length();
char[] arr=word.toCharArray();
for(char ch:arr)
{
if(hm.containsKey(ch))
{
hm.put(ch,hm.get(ch)+1);
}
else
{
hm.put(ch, 1);
}
}
mod=new BigInteger("1000000007");
}
private void logic() {
BigInteger fact=factorial(len);
BigInteger denom=new BigInteger("1");
for(int val:hm.values())
{
denom=denom.multiply(factorial(val));
}
BigInteger fin=fact.divide(denom);
fin=fin.mod(mod);
System.out.println(fin);
}
public static BigInteger factorial(int i) {
BigInteger n = BigInteger.valueOf(i);
while (--i > 0)
n = n.multiply(BigInteger.valueOf(i));
return n;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int num_cases=sc.nextInt();
while(--num_cases>=0)
{
String word=sc.next();
WCOUNT wc=new WCOUNT(word);
wc.logic();
}
}
}
@phaniram
Copy link
Author

phaniram commented Feb 1, 2012

Chef has decided to retire and settle near a peaceful beach. He had always been interested in literature & linguistics. Now when he has leisure time, he plans to read a lot of novels and understand structure of languages. Today he has decided to learn a difficult language called Smeagolese. Smeagolese is an exotic language whose alphabet is lowercase and uppercase roman letters. Also every word on this alphabet is a meaningful word in Smeagolese. Chef, we all know is a fierce learner - he has given himself a tough exercise. He has taken a word and wants to determine all possible anagrams of the word which mean something in Smeagolese. Can you help him ?
Input

Input begins with a single integer T, denoting the number of test cases. After that T lines follow each containing a single string S - the word chef has chosen. You can assume that 1 <= T <= 500 and 1 <= |S| <= 500. You can also assume that no character repeats more than 10 times in the string.
Output

Output one line per test case - the number of different words that are anagrams of the word that chef has chosen. As answer can get huge, print it modulo 10^9 + 7
Example

Input:
4
ab
aa
aA
AAbaz

Output:
2
1
2
60
Description: In first case "ab" & "ba" are two different words. In third case, note that A & a are different alphabets and hence "Aa" & "aA" are different words.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment