RoomItems = "ab ab ab abab abab abab"
DirtyRoom = "abaabbabaabbabaabb"
RoomItems = sorted(RoomItems.split(" "), key=len)
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)],"",1)+DirtyRoom[0:len(Item)]
Yarr(RoomItems, DirtyRoom)
Yarr(RoomItems, DirtyRoom)
Um9vbUl0ZW1zID0gImFiIGFiIGFiIGFiYWIgYWJhYiBhYmFiIgpEaXJ0eVJvb20gPSAiYWJhYWJiYWJhYWJiYWJhYWJiIgoKUm9vbUl0ZW1zID0gc29ydGVkKFJvb21JdGVtcy5zcGxpdCgiICIpLCBrZXk9bGVuKQoKZGVmIFlhcnIoUm9vbUl0ZW1zLCBEaXJ0eVJvb20pOgoJZm9yIEl0ZW0gaW4gUm9vbUl0ZW1zOgoJCWlmIGFsbChjaGFyIGluIERpcnR5Um9vbVswOmxlbihJdGVtKV0gZm9yIGNoYXIgaW4gSXRlbSk6IAoJCQlwcmludCBJdGVtLCBEaXJ0eVJvb21bMDpsZW4oSXRlbSldCgkJCVJvb21JdGVtcy5yZW1vdmUoSXRlbSkKCQkJRGlydHlSb29tID0gRGlydHlSb29tLnJlcGxhY2UoRGlydHlSb29tWzA6bGVuKEl0ZW0pXSwiIiwxKStEaXJ0eVJvb21bMDpsZW4oSXRlbSldCgkJCVlhcnIoUm9vbUl0ZW1zLCBEaXJ0eVJvb20pCgpZYXJyKFJvb21JdGVtcywgRGlydHlSb29tKQ==