RoomItems = "a aa b"
DirtyRoom = "aaba"
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)
Um9vbUl0ZW1zID0gImEgYWEgYiIKRGlydHlSb29tID0gImFhYmEiCgpSb29tSXRlbXMgPSBzb3J0ZWQoUm9vbUl0ZW1zLnNwbGl0KCIgIiksIGtleT1sZW4pCgpkZWYgWWFycihSb29tSXRlbXMsIERpcnR5Um9vbSk6Cglmb3IgSXRlbSBpbiBSb29tSXRlbXM6CgkJaWYgYWxsKGNoYXIgaW4gRGlydHlSb29tWzA6bGVuKEl0ZW0pXSBmb3IgY2hhciBpbiBJdGVtKTogCgkJCXByaW50IEl0ZW0sIERpcnR5Um9vbVswOmxlbihJdGVtKV0KCQkJUm9vbUl0ZW1zLnJlbW92ZShJdGVtKQoJCQlEaXJ0eVJvb20gPSBEaXJ0eVJvb20ucmVwbGFjZShEaXJ0eVJvb21bMDpsZW4oSXRlbSldLCIiLDEpK0RpcnR5Um9vbVswOmxlbihJdGVtKV0KCQkJWWFycihSb29tSXRlbXMsIERpcnR5Um9vbSkKCllhcnIoUm9vbUl0ZW1zLCBEaXJ0eVJvb20p