Divide and conquer with arrays. Note that strings are arrays too.
Given a string s and a set of valid words D, design an algorithm that determines if the input string can be segmented into a space-separated sequence of valid words.
For example, if D={am,are,I,important,is,you} and the input string is "Iamimportant", the string can be split into I am important
Assuming that checking containment in D takes constant time, your algorithm should run in O(n2).
Let G be a directed graph, whose edges are coloured either red or blue. We want to find the shortest path from some vertex s to some vertex t.
One additional constaint: our shortest path must consist of first red edges (though potentially 0 of them), and then exclusively blue edges (or 0).
Hints:
Consider a connected undirected graph where each edge has an integer weight as well as a color which is either red or blue. Give an efficient algorithm to find an MST of the graph with the smallest number of red edges. In other words, among all possible MSTs, the algorithm should output one that has the least number of red edges. Your algorithm should run in time O(|E| log |V |). Prove the correctness of your solution.
(Hint: reduce the problem to a standard MST problem on weighted graphs without edge colors)