Skip to content

Instantly share code, notes, and snippets.

@anurag-7
Created May 28, 2024 09:44
Show Gist options
  • Save anurag-7/dfc1a75cce67717e7e38573f087d7fe2 to your computer and use it in GitHub Desktop.
Save anurag-7/dfc1a75cce67717e7e38573f087d7fe2 to your computer and use it in GitHub Desktop.
Finding the longest word which is still a word with a character removed
import json
# Dataset used https://github.com/dwyl/english-words/blob/master/words_dictionary.json
with open(r'path/to/words_dictionary.json') as f:
words = json.load(f)
longest_len = 0
longest_word = ""
for word in words:
length = len(word)
if all(((word[:idx] + word[idx + 1:]) in words) for idx in range(length)) and length > longest_len:
longest_len = length
longest_word = word
print(word)
print("Word: ", longest_word)
print("Length: ", longest_len)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment