Jumbles

Home JumblesMap

Background

This assignment was primarily designed to test our knowledge of the ArrayList object. A leaderboard (which was ranked based on runtime) was created to encourage students to write fast/efficient code to accomplish the task.

We were provided with 2 files:

Each line of the jumbles.txt file contained sequences of characters, that when rearranged were supposed to form words (most often only 1 word) that can be found in dictionary.txt.

The goal was to match each jumbled “word” with its un-jumbled counterpart(s) from the dictionary. If there is no matching word in the dictionary, the jumble would be printed on its own.

However, the amount of dictionary words was the main challenge, as it would simply take too long to compare each jumble with each dictionary word. Some amount of filtration had to be done to optimize the process.

This is what the final output looks like (long)

addej jaded
ahicryrhe hierarchy
alvan naval
annaab banana
baltoc cobalt
braney nearby
celer creel
couph pouch
cukklen knuckle
dica acid cadi caid
dobeny beyond
dobol blood
dufil fluid
dupled puddle
eslyep sleepy
ettniner renitent
ettorp potter
genjal jangle
gluhc gulch
hartox thorax
hoybis boyish
hucnah haunch
iddec diced
irrpo prior
kutbec bucket
lappor poplar
lasia alias
laurib burial
lubly bully
meefal female
milit limit
mopsie impose
mycall calmly
nekel kneel
nokte token
noper prone
nwae anew wane wean
nyegtr gentry
perrim primer
preko poker
pudmy dumpy
pypin nippy
rebisc scribe
rodug gourd
rpeoims imposer promise semipro
shewo howes whose
wardty tawdry
warllc
yaldde deadly

Implementation

To accomplish this task, in the most efficient manner (that I could come up with at the time, while satisfying the assignment requirements), this is the solution that I came up with:

The complexity is O(N*M)

Solution (full class)

// COPYRIGHT AVI GLOZMAN, OCTOBER 2018
import java.util.*;
import java.io.*;

public class Jumbles {

  public static void main(String[] args) throws Exception {

      if (args.length < 2){
          System.out.println("\nUsage: C:\\> java Jumbles <dictionary file> <jumbles file>");
          System.exit(0);
      }
      BufferedReader dictionaryInput = new BufferedReader( new FileReader(args[0]));
      BufferedReader jumblesInput = new BufferedReader( new FileReader(args[1]));

      ArrayList<String> jumblesArray = new ArrayList<String>();
      ArrayList<String> dictionaryArray = new ArrayList<String>();

      while (jumblesInput.ready())
        jumblesArray.add(jumblesInput.readLine());
      jumblesInput.close();

      int longestJumble = 0;
      for (int i = 0; i < jumblesArray.size(); i++)
        if (jumblesArray.get(i).length() > longestJumble)
          longestJumble = jumblesArray.get(i).length();

      while (dictionaryInput.ready()){
        String currentWord = dictionaryInput.readLine();
        if (currentWord.length() <= longestJumble)
          dictionaryArray.add(currentWord);
      }
      dictionaryInput.close();

      Collections.sort(jumblesArray);

      for (String j : jumblesArray){
        ArrayList<String> wordsWithSameChars = new ArrayList<String>();
        for (String d : dictionaryArray)
          if (hasSameChars(j, d))
            if (canonicalize(j).equals(canonicalize(d)))
              wordsWithSameChars.add(d);

        String words = "";
        Collections.sort(wordsWithSameChars);
        for (String s : wordsWithSameChars)
          words+=(s + " ");
        System.out.println(j+ " " + words);
      }

    }

    static String canonicalize (String word){
      char [] wordArray = word.toCharArray();
      Arrays.sort(wordArray);
      return new String(wordArray);
    }

    static boolean hasSameChars (String jumble, String dictionaryTerm){
      if (jumble.length() == dictionaryTerm.length()){
          for (char c : jumble.toCharArray())
             if (dictionaryTerm.indexOf(c) < 0)
               return false;
        return true;
      }
      return false;
    }
  }

For a more efficient solution using Maps/Sets instead of just Lists, see JumblesMap