3

I wish to loop through a dictionary file and find words that contain only the given characters

Example dgo

Desired results: dog, god

NOT words that contain (within them) the given characters

I am working with the following code:

            while((dictionaryWord = br_.readLine()) != null) 
            {   

                    if(dictionaryWord.contains(getWord()))
                        System.out.println(dictionaryWord);

            }

But this gives me all words which contain the given characters -- NOT DESIRED

James P.
  • 19,313
  • 27
  • 97
  • 155
stackoverflow
  • 18,348
  • 50
  • 129
  • 196
  • You might want to take a look at [this](http://www.regular-expressions.info/tutorial.html) tutorial. Regular expressions are something that will come up time and time again, and it's probably a good idea to get to know them. – Jeffrey May 12 '12 at 20:58
  • @Jeffrey yes I'm familiar with them. I wanted to do this all java though – stackoverflow May 12 '12 at 20:59
  • You would still be doing this all in Java, it has a [regex library](http://docs.oracle.com/javase/7/docs/api/java/util/regex/package-summary.html) built in. (Oracle's [regex tutorial](http://docs.oracle.com/javase/tutorial/essential/regex/).) – Jeffrey May 12 '12 at 21:10
  • I'm not sure if it fits but I added an anagram tag. – James P. May 12 '12 at 21:27

2 Answers2

4

Without regular expressions:

public static boolean sameCharacters(String left, String right) {
    return sortCharacters(left).equals(sortCharacters(right));
}

private static String sortCharacters(String s) {
    final char[] chars = s.toCharArray();
    Arrays.sort(chars);
    return String.valueOf(chars);
}

UPDATE: better performing version (thanks to user384706):

public static boolean sameCharacters(String left, String right) {
    return Arrays.equals(sortCharacters(left), sortCharacters(right));
}

private static char[] sortCharacters(String s) {
    final char[] chars = s.toCharArray();
    Arrays.sort(chars);
    return chars;
}
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • Wouldn't it be better to have: `char[] sortCharacters(String s)` and compare the `char []` instead of creating new `String` and doing `equal`? – Cratylus May 12 '12 at 21:12
  • 1
    @JamesPoulson: `String.valueOf(chars)` makes an unnecessary internal copy of `chars` array to make sure `String` remains immutable. – Tomasz Nurkiewicz May 12 '12 at 21:30
  • Wouldn't it be possible to improve performance further by avoiding the sorting? I think it should be possible to write a method to do the comparison of the chars using a HashTable. This would have complexity O(N) compared to average case complexity O(NlogN) with sorting. – Martin Prakash May 12 '12 at 22:11
  • @MartinPrakash: in theory: yes. However for short strings (like single words) the cost of `HashSet` might be too big. Feel free to post your solution and do some benchmarks, it might be interesting. – Tomasz Nurkiewicz May 12 '12 at 22:14
2

You could check by doing

if (word.matches("[dgo]*")) {
    ...
}
aioobe
  • 413,195
  • 112
  • 811
  • 826