Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.Map;
- public class Anagrams {
- public interface Function {
- public int call() throws Exception;
- }
- public static void main(String[] args) throws Exception {
- if (args.length < 1) {
- System.out.println("Usage: java Anagrams [file]");
- System.exit(1);
- }
- final File file = new File(args[0]);
- int expectedNonAnagram = 65763;
- time("testWithHashMap", 10, new Function() {
- @Override
- public int call() throws IOException {
- return testWithHashMap(file);
- }
- }, expectedNonAnagram);
- time("testWithSorting", 10, new Function() {
- @Override
- public int call() throws IOException {
- return testWithSorting(file);
- }
- }, expectedNonAnagram);
- time("testWithArray", 10, new Function() {
- @Override
- public int call() throws IOException {
- return testWithArray(file);
- }
- }, expectedNonAnagram);
- }
- public static void time(String name, int repetitions, Function function,
- int expectedResult) throws Exception {
- long total = 0;
- for (int i = 0; i < repetitions; i++) {
- System.gc();
- long start = System.currentTimeMillis();
- int result = function.call();
- long end = System.currentTimeMillis();
- if (result != expectedResult) {
- System.out.println("Oops, " + name + " is broken");
- return;
- }
- total += end - start;
- }
- System.out.println("Execution of " + name + " took "
- + (total / repetitions) + " ms on average");
- }
- public static int testWithArray(File file) throws IOException {
- BufferedReader reader = new BufferedReader(new FileReader(file));
- try {
- String line = reader.readLine();
- int lineNumber = 2;
- int[] firstLineChars = getCharNumberArray(line);
- while ((line = reader.readLine()) != null) {
- int[] currentLineChars = getCharNumberArray(line);
- if (!Arrays.equals(firstLineChars, currentLineChars)) {
- return lineNumber;
- }
- lineNumber += 1;
- }
- } finally {
- reader.close();
- }
- return -1;
- }
- private static int[] getCharNumberArray(String line) {
- int[] charNumbers = new int[26];
- Arrays.fill(charNumbers, 0);
- for (char c : line.toLowerCase().toCharArray()) {
- if (Character.isAlphabetic(c)) {
- charNumbers[c - 'a'] += 1;
- }
- }
- return charNumbers;
- }
- public static int testWithSorting(File file) throws IOException {
- BufferedReader reader = new BufferedReader(new FileReader(file));
- try {
- String line = reader.readLine();
- int lineNumber = 2;
- char[] firstLineChars = getSortedChars(line);
- while ((line = reader.readLine()) != null) {
- char[] currentLineChars = getSortedChars(line);
- if (!Arrays.equals(firstLineChars, currentLineChars)) {
- return lineNumber;
- }
- lineNumber += 1;
- }
- } finally {
- reader.close();
- }
- return -1;
- }
- private static char[] getSortedChars(String line) {
- StringBuilder builder = new StringBuilder();
- for (char c : line.toLowerCase().toCharArray()) {
- if (Character.isAlphabetic(c)) {
- builder.append(c);
- }
- }
- char[] results = builder.toString().toCharArray();
- Arrays.sort(results);
- return results;
- }
- public static int testWithHashMap(File file) throws IOException {
- BufferedReader reader = new BufferedReader(new FileReader(file));
- try {
- String line = reader.readLine();
- int lineNumber = 2;
- Map<Character, Integer> firstLineCounts = countLetters(line);
- while ((line = reader.readLine()) != null) {
- Map<Character, Integer> currentLineCounts = countLetters(line);
- if (!firstLineCounts.equals(currentLineCounts)) {
- return lineNumber;
- }
- lineNumber += 1;
- }
- } finally {
- reader.close();
- }
- return -1;
- }
- private static Map<Character, Integer> countLetters(String line) {
- Map<Character, Integer> letterCounts = new HashMap<Character, Integer>();
- for (char c : line.toLowerCase().toCharArray()) {
- if (Character.isAlphabetic(c)) {
- Integer count = letterCounts.get(c);
- if (count == null) {
- count = 0;
- }
- count += 1;
- letterCounts.put(c, count);
- }
- }
- return letterCounts;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement