-4

I am trying to solve a problem where i have given a text file with words followed by newline charecter

I need to write a function which takes input as a string and should return output true if it is a meaningful word else false.

My attempt of doing it is to traverse through the text file and maintain a hash for the words . If my given input is a word which exists in the hash i would return true else false.but hash has a space complexity of O(n) how else can we achieve this.

Please help me with the solution.

Srisa
  • 269
  • 1
  • 3
  • 9
  • 2
    "Please help me with the solution and don't be harsh of not providing the code of what i tried." No. Show some effort. – ceejayoz Nov 30 '16 at 04:02
  • If you're trying to learn the vocabulary from a text file that's fine and you can't do better than O(|V|) space complexity where |V| is the size of your vocabulary. – gidim Nov 30 '16 at 04:03
  • Show code first or get no help –  Nov 30 '16 at 04:04
  • 1
    "but hash has a space complexity of O(n)" Yeah. Either that or read through the file each time you want (with time complexity of O(n)). Space or time, pick one. (Trie is somewhere in between, good pick if you have a good library, but not supported natively) – Amadan Nov 30 '16 at 04:05
  • Sorry for the last line... – Srisa Nov 30 '16 at 04:07
  • 1
    You could also look into directed acyclic word graphs (dawgs), which are even more memory efficient than tries, though harder to create and maintain: https://en.wikipedia.org/wiki/Deterministic_acyclic_finite_state_automaton. On the other hand, it takes less than a second to store the 260,000 words in the `yawl` wordlist into a standard Python set, and it makes only a smallish blip on my computers RAM usage. In most cases the obvious solution is perfectly adequate and only takes about 2 or 3 lines of code. – John Coleman Nov 30 '16 at 04:50
  • Please show your effort. – shawn Nov 30 '16 at 08:35

1 Answers1

1

You could try breaking up your text file into various text files. If your text file has words ranging from A-Z try breaking that up in a meaningful way so that you are only sorting through a subsection of those words instead of the whole dictionary. As others have pointed out we are not here to write the code for you so please post what you have tried so far so we can help!

Jay
  • 157
  • 2
  • 11
  • Oh thanks just to clarify is this a doable question in 30 minutes – Srisa Nov 30 '16 at 06:06
  • i was asked the same in a telephonic interview to implement – Srisa Nov 30 '16 at 06:06
  • Well this depends. If you were asked in an interview they probably don't want you messing with the actual dictionary meaning that your best bet is probably the trie tree explained above! Good luck in your interviews! I do believe that this is doable in 30 minutes. – Jay Nov 30 '16 at 06:35