class Solution:
def sortSentence(self, s: str) -> str:
dic = collections.defaultdict(str)
answer = ""
def sep(s):
dic[s[-1]] = s[:-1]
return s[-1]
s = list(s.split())
nums = sorted(map(sep, s))
for num in nums:
answer += dic[num] + " "
return answer[:-1]
pythonic하게 써보자면.
class Solution:
def sortSentence(self, s: str) -> str:
s = " ".join(sorted(s.split(), key = lambda x : x[-1]))
s = re.sub("[1-9]", "", s)
return s