Pregunta de entrevista de Booking.com

Call a function F in a loop. F returns a string of 140 chars. Write to the output 1 when F returns a string with the same letters (basically a permutation) of a string previously returned. (the question was not this well formed)

Respuestas de entrevistas

Anónimo

30 oct 2015

Build an hashmap where for every letter you count how many times it appears in the string, and put it in a set. For every string check if the set contains the built hash. OR sort the string, and put it in a set

2

Anónimo

7 dic 2015

I think sorting the word would work much better and is easier to understand

Anónimo

8 ene 2016

This is called Anagram

Anónimo

14 jun 2016

"and put it in a set." to put it in a set you need to implement a way to compare hashs What you would need is 1 - build your hash for given word(O(n)) 2 - check if the word is present in every other hash with same quantity, since you will need to compare with all previous words everytime the complexity is O(nk) where k is the number of distinct words so overal complexity O(nk)

Anónimo

1 jul 2018

You can either create a custom hash function which is very complex or compress the phrase like: a4b6f.... ordered alphabetically. If the 2 compressed phrases mach, they are anagrams. The complexity would be O(140) to compress each phrase * n phrases which leads to O(n) complexity.

Anónimo

14 jun 2019

Short python answer: def findPermutationsSort(): res = f([]) perm_dict = {} for s in res: key = ''.join(sorted(s)) if key in perm_dict: print 1 else: perm_dict[key] = 1