I have a line and another text file that contains a list of lines.
We call 2 lines "brotherhood lines" when they exactly match after sorting in alphabetical order.
For example, "abc" and "cba" will be sorted into "abc" and "abc", so the original two are brotherhood. But "abc" and "aaa" are not.
So, is there an efficient way to select all fraternity lines from a text file according to one line?
For example, we also have a "abc"text file that writes like this:
abc
cba
acb
lalala
then "abc", "cba", "acb"is the answer.
Of course, “sorting and comparing” is a good attempt, but “effective”, I mean, if there is a way, we can determine which candidate line is or not the original brotherhood after processing one pass.
This is the most effective way, I think. After all, you cannot say the answer without even reading the lines of the candidate. For sorting, in most cases we need to make more than 1 pass to the candidate line. So, a hash table may be a good solution, but I have no idea which hash function to choose.
source
share