1. Engineering
  2. Computer Science
  3. solve the anagram using big integer anagram class import javautilscanner...

Question: solve the anagram using big integer anagram class import javautilscanner...

Question details

Solve the anagram using big integer

Anagram class:

import java.util.Scanner;
import java.io.*;

public class AnagramcountingLong
{
 
   public static void main (String[] args) throws IOException
   {    
//   Scanner scan = new Scanner (System.in);  
     Scanner scan = new Scanner (new File("test.txt"));
 
     String word;
     
     int size = (int)'z' - (int)'A' + 1;
     int[] counts;
     int low = (int)'A';
     
     long result;
     
     while (scan.hasNext())
     {
       // get word and character counts
       counts = new int[size];
       word = scan.nextLine();
       for (int i = 0; i < word.length(); i++)
       {
         counts[(int)word.charAt(i) - low]++;
       }
       
       // calculate length!
       result = word.length();
       for (int i = word.length() - 1; i > 1; i--)
         result = result * i;

       // divide by repeats
       for (int c = 0; c < size; c++)
         for (int i = counts[c]; i > 1; i--)
           result = result / i;
       System.out.println(result);          
     }
   }
}
Test Class:

import java.util.Scanner;

import java.io.*;

import java.util.StringTokenizer;

public class Test

{

   public static void main (String[] args) throws IOException

   {

// Scanner scan = new Scanner (System.in);

// Scanner scan = new Scanner (new File("test.txt"));

   Reader scan=new Reader();

  

   System.out.println();

}

  

static class Reader

{

BufferedReader br;

StringTokenizer st;

public Reader()

{

br = new BufferedReader(new

   InputStreamReader(System.in));

}

String next()

{

while (st == null || !st.hasMoreElements())

{

try

{

st = new StringTokenizer(br.readLine());

}

catch (IOException e)

{

e.printStackTrace();

}

}

return st.nextToken();

}

int nextInt()

{

return Integer.parseInt(next());

}

long nextLong()

{

return Long.parseLong(next());

}

double nextDouble()

{

return Double.parseDouble(next());

}

String nextLine()

{

String str = "";

try

{

str = br.readLine();

}

catch (IOException e)

{

e.printStackTrace();

}

return str;

}

}

}

Sample test:

Sample Input 1

Sample Output 1

at

ordeals

abcdefghijklmnopqrstuvwxyz

abcdefghijklmabcdefghijklm

abcdABCDabcd

2

5040

403291461126605635584000000

49229914688306352000000

29937600

The sample input is the test file and the out put is suppose to be the results of the test.

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution