3

Here's ultimately what I need to do:

I have an ArrayList called originalList that looks like [pans, pots, sit, it's, naps]

and another ArrayList called modifiedList that takes originalList and removes punctuation and uppercase and then sorts the list. So it would look like [anps, anps, ist, ist, opst]

modifiedList's purpose is to tell me what words are anagrams of each other. An anagram is a word that consist of the same letters. The problem is I need to sort originalList to match modifiedList so that I can output what words are anagrams of each other. originalList would need to become [pans, naps, sit, it's, pots]. Suggestions?

4
  • 1
    Are you bound to the idea of using two ArrayLists (by a homework assignment), or are you open to other ideas? Commented Mar 9, 2013 at 23:06
  • You could arrange originalList when you are preparing modifiedList itself Commented Mar 9, 2013 at 23:08
  • I have to use ArrayList. Trust me, I wouldn't if I didn't have to. Commented Mar 9, 2013 at 23:10
  • 1
    One solution is to create a class to hold the original string along with the String without the punctuation. Commented Mar 9, 2013 at 23:12

2 Answers 2

5

Don't use an additional list. Sort your original list using a comparator that "normalizes" the two words to compare, and then compares their normalized values (by normalizing, I mean transforming naps into anps).

You'll then have your anagrams next to each other in the list.

Sign up to request clarification or add additional context in comments.

2 Comments

But he will lose the original values then , how can he output which words are anagrams of each other ?
No, he won't lose anything. The original words will be sorted by their normalized values. Anagrams will thus be next to each other, in their original form. To print the anagrams, he just needs a second pass on the sorted array, and compare the normalized values of the word at index i to the normalized value of the word at index i + 1. If the goal is to print all the anagrams, I wouldn't use a List, though, but a Map<String, List<String>> where the key is the normalized value and the value if the list of anagrams having this normalized value.
1
public static void main(String args[]){
        ArrayList<String> alOriginal = new ArrayList<String>();
        alOriginal.add("pans");
        alOriginal.add("pots");
        alOriginal.add("sit");
        alOriginal.add("it's");
        alOriginal.add("naps");
        ArrayList<String> alAnagram = getSortedAnagramStrings(alOriginal);
        System.out.println(alOriginal);
        System.out.println(alAnagram);
    }
    public static java.util.ArrayList<String> getSortedAnagramStrings(ArrayList<String> original){
        ArrayList<String> alAnagramStrings = new ArrayList<String>();
        for (String currentString : original) {
            //  Remove punctuation
            char[] anagramChars = currentString.replace("'", "").toCharArray();
            //  Sort characters
            Arrays.sort(anagramChars);
            //  Prepare string
            String anagramString = new String(anagramChars);
            //  Add to array list
            alAnagramStrings.add(anagramString);
        }
        //  Simple sort logic
        for (int index = 0; index < alAnagramStrings.size(); index ++){
            for (int index1 = index + 1; index1 < alAnagramStrings.size(); index1 ++){
                //  If both anagram strings are same
                if(alAnagramStrings.get(index).compareTo(alAnagramStrings.get(index1)) == 0){
                    //  Compare original strings
                    if (original.get(index).compareTo(original.get(index1)) > 0){
                        String temp =original.get(index);
                        original.set(index, original.get(index1));
                        original.set(index1, temp);
                    }else{
                        String temp =original.get(index);
                        original.set(index1, original.get(index));
                        original.set(index, temp);
                    }
                }else if(alAnagramStrings.get(index).compareTo(alAnagramStrings.get(index1)) > 0){
                    String temp =alAnagramStrings.get(index);
                    alAnagramStrings.set(index, alAnagramStrings.get(index1));
                    alAnagramStrings.set(index1, temp);
                    String temp1 =original.get(index);
                    original.set(index, original.get(index1));
                    original.set(index1, temp1);
                }
            }
        }
        return alAnagramStrings;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.