RoomItems = "ab ba abab"
DirtyRoom = "ababaabb"
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)
Um9vbUl0ZW1zID0gImFiIGJhIGFiYWIiCkRpcnR5Um9vbSA9ICJhYmFiYWFiYiIKClJvb21JdGVtcyA9IHNvcnRlZChSb29tSXRlbXMuc3BsaXQoIiAiKSwga2V5PWxlbilbOjotMV0KCmRlZiBZYXJyKFJvb21JdGVtcywgRGlydHlSb29tKToKCWZvciBJdGVtIGluIFJvb21JdGVtczoKCQlpZiBhbGwoY2hhciBpbiBEaXJ0eVJvb21bMDpsZW4oSXRlbSldIGZvciBjaGFyIGluIEl0ZW0pOiAKCQkJcHJpbnQgSXRlbSwgRGlydHlSb29tWzA6bGVuKEl0ZW0pXQoJCQlSb29tSXRlbXMucmVtb3ZlKEl0ZW0pCgkJCURpcnR5Um9vbSA9IERpcnR5Um9vbS5yZXBsYWNlKERpcnR5Um9vbVswOmxlbihJdGVtKV0sIiIpK0RpcnR5Um9vbVswOmxlbihJdGVtKV0KCQkJWWFycihSb29tSXRlbXMsIERpcnR5Um9vbSkKCllhcnIoUm9vbUl0ZW1zLCBEaXJ0eVJvb20p