Topcoder MagicWords problem