I am looking for an efficient (more efficient than my combinatorial search) algorithm to tile segments of strings.
I am given two things:
- Length of the "area" to cover;
- Set of segments (a segment is defined by starting and ending index, as well as a label)
Example: The total length of area is 5. The given segments are:
- [(0,2), "A B"]
- [(1,3), "B C"]
- [(1,4), "B C D"]
- [(3,5), "D E"]
I am using Python notation to denote segment e.g. (0,2) and its label "A B" a string.
There are 3 ways how one could populate the "area" of size 5 with the given segments with no overlap:
1. ("A B"), ("D E")
2. ("B C"), ("D E")
3. ("B C D")
Other tilings are not possible as an additional segment will overlap with one of the existing tiles already sitting in the "area".
My current approach is to start with one segment and see how many combinations of non-overlapping segments I get. Then two segments and so on till 4. Each combination validity is checked if any of remaining segments cannot be added. If any of remaining segments can be inserted, the whole combination is rejected as invalid. This works pretty well when there are not many segments and there is little overlap/combinations.
Is there a simple yet efficient algorithm to find these "tilings"?