Recently asked during some interview that "How to find reverse of all strings if exists in a list of more than million strings?
For E.g. str[1] = "abc", I need to check for "cba" exactly, no anagrams.
Method 1. Store all the strings in a hashset, start traversing from the first string and check for the reversed form exists in Hashset. if yes, then pair else move to next element.
Can you suggest any method if memory is the constraint?
In Python, you can reverse the items of lists ( list ) with using reverse() , reversed() , and slicing. If you want to reverse strings ( str ) and tuples ( tuple ), use reversed() or slice.
What is string reversal? In programming, a string is a sequence of characters. It is one of the basic data types that we use to express text rather than numbers. Reversing a string means flipping the order of the characters completely. In other words, we are making the string read backwards.
If allowed, you could in-place sort the strings so when you look up the reverse of a string you can do a binary search.
You can use a Bloom Filter which will tell you if a string already exists within a hash table like structure, but each bucket is only 0 or 1 so very little space is used.
Exactly 1 000 000 bits == 125 KB
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With