RoomItems = "ab ab ab abab abab abab"
DirtyRoom = "abaabbabaabbabaabb"
RoomItems = sorted(RoomItems.split(" "), key=len)[::-1]
def Yarr(RoomItems, DirtyRoom):
for Item in RoomItems:
if all(char in DirtyRoom[0:len(Item)] for char in Item):
print Item, DirtyRoom[0:len(Item)]
RoomItems.remove(Item)
DirtyRoom = DirtyRoom.replace(DirtyRoom[0:len(Item)],"")+DirtyRoom[0:len(Item)]
Yarr(RoomItems, DirtyRoom)
Yarr(RoomItems, DirtyRoom)
Um9vbUl0ZW1zID0gImFiIGFiIGFiIGFiYWIgYWJhYiBhYmFiIgpEaXJ0eVJvb20gPSAiYWJhYWJiYWJhYWJiYWJhYWJiIgoKUm9vbUl0ZW1zID0gc29ydGVkKFJvb21JdGVtcy5zcGxpdCgiICIpLCBrZXk9bGVuKVs6Oi0xXQoKZGVmIFlhcnIoUm9vbUl0ZW1zLCBEaXJ0eVJvb20pOgoJZm9yIEl0ZW0gaW4gUm9vbUl0ZW1zOgoJCWlmIGFsbChjaGFyIGluIERpcnR5Um9vbVswOmxlbihJdGVtKV0gZm9yIGNoYXIgaW4gSXRlbSk6IAoJCQlwcmludCBJdGVtLCBEaXJ0eVJvb21bMDpsZW4oSXRlbSldCgkJCVJvb21JdGVtcy5yZW1vdmUoSXRlbSkKCQkJRGlydHlSb29tID0gRGlydHlSb29tLnJlcGxhY2UoRGlydHlSb29tWzA6bGVuKEl0ZW0pXSwiIikrRGlydHlSb29tWzA6bGVuKEl0ZW0pXQoJCQlZYXJyKFJvb21JdGVtcywgRGlydHlSb29tKQoKWWFycihSb29tSXRlbXMsIERpcnR5Um9vbSk=