나는 이렇게 학습한다/Algorithm & SQL

1225. Switcheroo

daco2020 2022. 12. 25. 21:36
반응형

Given a string made up of letters a, b, and/or c, switch the position of letters a and b (change a to b and vice versa). Leave any incidence of c untouched.

Example:

'acb' --> 'bca'
'aabacbaa' --> 'bbabcabb'



Solution:

def switcheroo(s):
    return s.translate(str.maketrans("ab", "ba"))
def switcheroo(s):
    return s.translate({ord('a'): 'b', ord('b'): 'a'})


반응형