emna mokhtar
emna mokhtar

Reputation: 7

How to find if a word exists in a very large file in an optimized way ?

The game goes like this: The player inputs a number of vowels in [0..10]. We generate 10-input(vowels) consonants to finally display 10 letters unordered. The player then tries to form the longest word possible with the given letters.

The problem: we have a dictionary of important size to go through to find if the word is valid.

What is the best way to search through it? My best two ideas are:

  1. Seperate the word in different files indexed by the number of vowels inside the words stored in it.
  2. Use Streamer(). filter method using a function that returns the number of vowels in a word.

Both seem very expensive in term of time complexity (i don't know if im using the term correctly).

I hope i was clear enough.

Upvotes: 0

Views: 74

Answers (1)

Max08
Max08

Reputation: 1025

I am assuming you are using java. If that is true you can store all your words inside a HashSet<String>.

Sets store data in buckets. So when you search for a word, jvm would first find a bucket which might have this word and then look into that bucket to confirm if that word is present or not.

This approach is similar to the option 1 you have mentioned. All the complexity is hidden from you. you just need to call the contains method. jvm does all this for you behind the scene.

    HashSet<String> dictionary = new HashSet<String>();

    //add words to dictionary
    dictionary.add("apple");

    //Returns true if this set contains the specified element. 
    dictionary.contains("apple");

Upvotes: 1

Related Questions