Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
We will be given comma separated strings by multiple people. Now those people may not remember all the strings. But whatever order they provide will be correct

For ex :
Person1 : W7, W8, W1, W9
Person2 : W8, W9, W2
Person3 : W2, W10

**Now our role is to detect whether all the above mentioned orders possible**

For ex :
Person1 : W7, W8
Person2 : W8, W9, W10
Person3 : W10, W7

Now the above ordering is not possible because according to person1 - W7 comes before W8. Acc to Person2 : W8 comes before W10. That means W7 comes before W10. But this contradicts with Person3 who says W7 comes after W10


What I want ?

I don't want the code but some help as to how to approach this question

What I have tried:

I thought about it a lot. But frankly was unable to think of a clear solution!

I thought we could maintain a list for each word of the words that come after it and before it. But maintaining and updating such a list is too heavy !
Posted
Updated 8-Aug-16 23:04pm
v2

1 solution

Consider a sequence, e.g.
W7,W8,W1,W9
Any item has preceding items as well as following items.
For instance W8 has
  • preceding items {W7}
  • following items {W1,W9}

Please note either preceding items or following items could be the empty set.

Now you can compare two sequences (say sequence A and B): for every item of the sequence A you may have two cases:
  1. You don't find the same item in sequence B
  2. You find the same item in sequence B


In case 1 there is no mismatch, and you can go on with next item of sequence A.

In case 2 you have to make sure that any item in the preceding items of sequence A cannot be found in the following items of sequence B (and viceversa: any item in the following items of sequence A cannot be found in the preceding items of sequence B). If this is not the case then ther is a mismatch and the two sequences are incompatible.

If two sequences are compatible then you can merge them in order to obtain a merged sequence that preserves items order. The merged sequence could in turn be compared with another input sequence, and so on.

I think it isn't a smart algorithm, however it could work.
 
Share this answer
 
Comments
Maciej Los 9-Aug-16 17:23pm    
Great explanation!
A5!
CPallini 10-Aug-16 2:30am    
I hope it works.
Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900