Search code examples
pythonpython-3.xlistpermutationscramble

Is there any way in Python where we can find jumbled word entered by user exists given list without permutations code to make it faster?


suppose I have list of unique 300k+ items:

mylist = ["door", "mango", "rose", "orange", "car", "knowledge", "flower", ...., 300k+ items]

userinput = input()

Now, if the user inputs jumbled word for "knowledge". eg. "dngwekleo", the program should check the input word in mylist and print "knowledge" as output.

My code works fine till the length of the input word is 7, I have used permutations code for input and then match each word in permutation == mylist. But as soon as input length of input word goes beyond 8-10, it creates too many permutations and then python takes too much time (10 mins, 20 mins, 30 mins) in getting the output.

Please help me in solving this to get answer quicker like 10-15 secs, trying since 20 days.


Solution

  • Just to kick-start, you can approach by creating a lookup with key sorted by character's & retain value with original string.
    eg: {deegklnow : knowledge}

    my_list = ["door", "mango", "rose", "orange", "car", "knowledge", "flower"]
    
    lookup = {"".join(sorted(x)): x for x in my_list}
    
    print(lookup.get("".join(sorted("dngwekleo"))))
    print(lookup.get("".join(sorted("eosr"))))
    print(lookup.get("".join(sorted("rca"))))
    

    knowledge
    rose
    car