Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pairs of strings in reverse order in a list of more than million strings?

Tags:

string

reverse

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?

like image 776
amit mathapati Avatar asked Sep 29 '11 23:09

amit mathapati


People also ask

How do you reverse a list in Jupyter notebook?

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 does it mean to reverse a string?

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.


2 Answers

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.

like image 98
Karoly Horvath Avatar answered Sep 27 '22 22:09

Karoly Horvath


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

like image 26
Serdalis Avatar answered Sep 27 '22 21:09

Serdalis