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 aand b equal.

我的解答:

 def areSimilar(a, b):
count = 0
if sorted(a) == sorted(b):
for i in zip(a,b):
if i[0] != i[1]:
count +=1
if count > 2:
return False
else:
return True
else:
return False

膜拜大佬:

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

Code Signal_练习题_Are Similar?的更多相关文章

  1. Code Signal_练习题_digitDegree

    Let's define digit degree of some positive integer as the number of times we need to replace this nu ...

  2. Code Signal_练习题_Knapsack Light

    You found two items in a treasure chest! The first item weighs weight1 and is worth value1, and the ...

  3. Code Signal_练习题_growingPlant

    Each day a plant is growing by upSpeed meters. Each night that plant's height decreases by downSpeed ...

  4. Code Signal_练习题_arrayMaxConsecutiveSum

    Given array of integers, find the maximal possible sum of some of its k consecutive elements. Exampl ...

  5. Code Signal_练习题_differentSymbolsNaive

    Given a string, find the number of different characters in it. Example For s = "cabca", th ...

  6. Code Signal_练习题_firstDigit

    Find the leftmost digit that occurs in a given string. Example For inputString = "var_1__Int&qu ...

  7. Code Signal_练习题_extractEachKth

    Given array of integers, remove each kth element from it. Example For inputArray = [1, 2, 3, 4, 5, 6 ...

  8. Code Signal_练习题_stringsRearrangement

    Given an array of equal-length strings, check if it is possible to rearrange the strings in such a w ...

  9. Code Signal_练习题_absoluteValuesSumMinimization

    Given a sorted array of integers a, find an integer x from a such that the value of abs(a[0] - x) + ...

随机推荐

  1. php防止网站被刷新

    在实际应用中,总会遇到某些页面被恶意用户刷新.当你的系统在某些模块没有使用缓存的时候,频繁的刷新会导致数据库吃紧.下面附上一段代码,防止频繁的刷新造成的死机情况. 主要是从 session方面进行限制 ...

  2. [Objective-C语言教程]动态绑定(32)

    动态绑定确定在运行时而不是在编译时调用的方法. 动态绑定也称为后期绑定. 在Objective-C中,所有方法都在运行时动态解析.执行的确切代码由方法名称(选择器)和接收对象确定. 动态绑定可实现多态 ...

  3. Leetcode 102 二叉树的层次遍历 Python

    二叉树的层次遍历 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7],   3   / \ 9 20 ...

  4. XMPPFramework核心类介绍

    XMPPFramework结构 在进入下一步之前,先给大家讲讲XMPPFramework的目录结构,以便新手们更容易读懂文章.我们来看看下图: 虽然这里有很多个目录,但是我们在开发中基本只关心Core ...

  5. jenkins发送测试报告邮件

     1.安装插件 Email Extension Plugin 2.设置Extended E-mail Notification a."系统管理"--“系统设置”.配置Extende ...

  6. WCF系列教程之WCF中的会话

    本文参考自http://www.cnblogs.com/wangweimutou/p/4516224.html,纯属读书笔记,加深记忆 一.WCF会话简介 1.在WCF应用程序中,回话将一组消息相互关 ...

  7. 【Kafka】Kafka数据可靠性深度解读

    转帖:http://www.infoq.com/cn/articles/depth-interpretation-of-kafka-data-reliability Kafka起初是由LinkedIn ...

  8. ActionController::UnfilteredParameters: unable to convert unpermitted parameters to hash

    rails 开发中 5.1版本使用binding.pry会报 ActionController::UnfilteredParameters: unable to convert unpermitted ...

  9. MySQL之mysql命令使用详解

    MySQL Name mysql - the MySQL command-line tool Synopsis mysql [options] db_name Description mysql is ...

  10. C#(winform)设置窗体的启动位置

    只需要设置窗体的StartPosition属性: registerForm.StartPosition = FormStartPosition.CenterScreen; FormStartPosit ...