프로그래밍/CodeSignal

[Intro] Are Similar?

CHIqueen 2020. 3. 17. 16:05

Two arrays are called similar if one can be obtained from another by swapping at most one pair of elements in one of the arrays.

Given two arrays a and b, check whether they are similar.

def areSimilar(a, b):
    return sorted(a)==sorted(b) and sum([c!=d for c,d in zip(a,b)])<=2

한번만 바꿀수 있으니 두 리스트를 비교했을때 다른 요소를 가지고 있는건 2개 이하여야 한다.