Skip to content

Instantly share code, notes, and snippets.

@Ryoliveira
Last active May 20, 2018 06:18
Show Gist options
  • Save Ryoliveira/471b9695fdaf9032f943b52f3a8ed028 to your computer and use it in GitHub Desktop.
Save Ryoliveira/471b9695fdaf9032f943b52f3a8ed028 to your computer and use it in GitHub Desktop.
Codefight.com Exercise #16
def areSimilar(a, b):
if a == b:
return True
for i in range(len(a)):
if a[i] in b:
if a[i] != b[i]:
j = b.index(a[i])
b[i], b[j] = b[j], b[i]
if a == b:
return True
else:
return False
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.
Example
For a = [1, 2, 3] and b = [1, 2, 3], the output should be
areSimilar(a, b) = true.
The arrays are equal, no need to swap any elements.
For a = [1, 2, 3] and b = [2, 1, 3], the output should be
areSimilar(a, b) = true.
We can obtain b from a by swapping 2 and 1 in b.
For a = [1, 2, 2] and b = [2, 1, 1], the output should be
areSimilar(a, b) = false.
Any swap of any two elements either in a or in b won't make a and b equal.
Input/Output
[execution time limit] 4 seconds (py3)
[input] array.integer a
Array of integers.
Guaranteed constraints:
3 ≤ a.length ≤ 105,
1 ≤ a[i] ≤ 1000.
[input] array.integer b
Array of integers of the same length as a.
Guaranteed constraints:
b.length = a.length,
1 ≤ b[i] ≤ 1000.
[output] boolean
true if a and b are similar, false otherwise.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment