HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
1. Feel Free to ask and submit anything on and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.

corejava-string-handling: Find anagrams of words

Iqubal 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. Iqubal, 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 Iqubal 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 Iqubal has chosen. As answer can get huge, print it modulo 10^9 + 7 Sample Input/Output: 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

corejava x 336
string-handling x 34
Posted On : 2017-02-13 20:39:33.0
profile Raksha - Raksha


import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class Anagrams {
public static void main(String[] args) {
Scanner input=new Scanner(;
String inputString[]=input.nextLine().split(" ");
int T=Integer.parseInt(inputString[0]);
if(1 <= T && T <= 500){
Set<String> uniqueWordsSet=new HashSet<String>();
List<Integer> listCount=new ArrayList<Integer>();
List<String> list=new ArrayList<String>();
StringBuilder temp=null;
for(int i=1;i<=T;i++){
temp=new StringBuilder(inputString[i]);
uniqueWordsSet.addAll(doPerm(list,temp,inputString[i].length()));//remove all duplicates

System.out.println(listCount.toString().replace("[","").replace("]","").replaceAll(",", ""));
System.out.println("Invalid Input!");


private static List<String> doPerm(List<String> list,StringBuilder str, int index){

if(index <= 0)
else { //recursively solve this by placing all other chars at current first pos
doPerm(list,str, index-1);
int currPos = str.length()-index;
for (int i = currPos+1; i < str.length(); i++) {//start swapping all other chars with current first char
swap(str,currPos, i);
doPerm(list,str, index-1);
swap(str,i, currPos);//restore back my string buffer
return list;

private static void swap(StringBuilder str, int pos1, int pos2){
char t1 = str.charAt(pos1);
str.setCharAt(pos1, str.charAt(pos2));
str.setCharAt(pos2, t1);


Posted On : 2017-02-15 00:30:09
Satisfied : 1 Yes  0 No
profile Rishi Kumar - Rishi Kumar
Reply This Thread

Post Answer
Please Login First to Post Answer: Login login with facebook -