Click here to Skip to main content
15,940,550 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:

Please I have a sequence of words S, and a sequence of words T. How do I find the shortest continuous subsequence of words in S such that the words in T appear in the order (in c#)
An example is

S ==> One Ring to rule them all, One Ring to find them, One Ring to bring them all and in the darkness bind them
T ==> find, them, all
Result ==> find them, One Ring to bring them all

My current source code is:

public HashSet<string> details()
         String a = "One Ring to rule them all, One Ring to find them, One Ring to bring them all and in the darkness bind them";
         String b = "find, them, all"; // list of words to search
         string[] avalues = a.Split(',');
         string[] bvalues = b.Split(',');
         HashSet<string> set = new HashSet<string>();
         int index = -1;
         bool found = false;
         string text = "";

            for (int q = 0;q < bvalues.Length; q++)

                if (q == 0)
                    text = avalues[q];
                else if (q > 0)
                    text = avalues[q].Substring(0, avalues[q].LastIndexOf(bvalues[q])) + bvalues[q];


        char[] split = new char[] { ' ', '\t', '\r', '\n', '.', ';', ',', ':', };

        for (int r = 0; r <= set.Count-1; r++)
            string setelem = set.ElementAt(r - 1); ;
                string[] parts = set.ElementAt(r).Split(split, StringSplitOptions.RemoveEmptyEntries);
                foreach (string part in parts)

                    //construct the string to delete
                    if (set.ElementAt(r).Contains(part))
                        int indexa = set.ElementAt(r).IndexOf(part);
                       set.ElementAt(r - 1).Remove(indexa);


                 return set;

but its not giving me the desired output.I probably could not get the right approach. Can someone please help a brother in the house? Thanks...
Updated 22-Jun-15 15:52pm
PIEBALDconsult 22-Jun-15 21:50pm    
How is this different from your question two days ago?

1 solution

If I understand the task correctly, you want to find the shortest sub-string between 'find' and 'all' where 'them' occurs in the string.

One approach could be to use IndexOf.
1. Find the word 'find' and save the index as indexFind.
2. Find the word 'all', starting from indexFind and save this index as indexAll.
3. Check if you can find the word 'them' between the two indices.
4. If not, repeat from step 2 using indexAll as the start position.
5. Finally, copy the sub-string between indexFind and indexAll.

Note: You also need to check that, for example, the sequence does not contain duplicates like 'find them find them all'. In this case I presume you want only the last part of the string.

If you need a more generic solution, you can adapt the solution something like this:
1. Find the first word from set T in set S and save the index as indexFirst.
2. Find the last word from set T in set S, starting from indexFirst and save this index as indexLast.
3. Loop through set T and check if the words appear in sequence and only once (no duplicates).
4. If not, do another search round.

I hope this helps you further with your school work.
It is not a ready solution, just a pointer for the way forward.
Share this answer
PIEBALDconsult 22-Jun-15 22:14pm    
What if there are other than three items?
George Jonsson 22-Jun-15 23:20pm    
I thought I should keep it simple as it looks like school work, but you make a very good point.
I added a description for a more generic solution that might be useful.

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