Given an array of equal-length strings, check if it is possible to rearrange the strings in such a way that after the rearrangement the strings at consecutive positions would differ by exactly one character.

Example

    • For inputArray = ["aba", "bbb", "bab"], the output should be
      stringsRearrangement(inputArray) = false.

      All rearrangements don't satisfy the description condition.

    • For inputArray = ["ab", "bb", "aa"], the output should be
      stringsRearrangement(inputArray) = true.

      Strings can be rearranged in the following way: "aa", "ab", "bb".

不会做....

import itertools
def stringsRearrangement(inputArray):
def f(x, y):
c = 0
for i in range(len(x)):
if x[i] != y[i]:
c += 1
if c == 1:
return True
return False
for k in itertools.permutations(inputArray, len(inputArray)):
r = True
for i in range(len(k) - 1):
if not f(k[i], k[i + 1]):
r = False
if r:
return True
return False

膜拜大佬

Code Signal_练习题_stringsRearrangement的更多相关文章

  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_练习题_absoluteValuesSumMinimization

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

  9. Code Signal_练习题_depositProfit

    You have deposited a specific amount of money into your bank account. Each year your balance increas ...

随机推荐

  1. tcpdump命令抓包参数

    在 Linux 命令行中使用 tcpdump 抓包 通过实例学习tcpdump命令 聊聊 tcpdump 与 Wireshark 抓包分析 tcpdump常用参数 -n 显示IP地址和端口号 -v 显 ...

  2. python 跨平台获取网卡信息和本机ip地址

    笔者在项目中遇到过获取本机网卡ip的例子,利用python库psutil解决了此问题. def get_netcard(): """获取网卡名称和ip地址 "& ...

  3. POJ 2491

    #include<iostream>#include<stdio.h>#include<string>#define MAXN 400using namespace ...

  4. python3处理json数据

    获取actuator的值 [root@mongo_rs1 tmp]# cat test.py import requests import json url = 'http://wxtest.mayo ...

  5. 剑指offer二十之包含min函数的栈

    一.题目 定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数. 二.思路 用一个栈dataStack保存数据,用另外一个栈minStack保存依次入栈最小的数.每次元素存入minSt ...

  6. ssh-key的复制

    执行ssh-keygen 生产钥 在b主机root目录创建.ssh文件夹 在a主机输入ssh-copy-id root@*.*.*.* 就把公钥复制过去了 命令:scp 不同的Linux之间copy文 ...

  7. Python:使用异常处理来判断运行的平台

    try: import termios, TERMIOS 1 except ImportError: try: import msvcrt 2 except ImportError: try: fro ...

  8. 兼容IE9以下和非IE浏览器的原生js事件绑定函数

    事件绑定函数的demo如下: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "htt ...

  9. windows cmd窗口提示“telnet”命令不能内部或外部命令,也不是可运行的程序

    windows cmd窗口提示“telnet”命令不能内部或外部命令,也不是可运行的程序 原因:C:\Windows\System32目录下没有telnet.exe,path系统变量的值包含了C:\W ...

  10. 关于配置 TeamCity 清理历史 artifacts 问题

    使用 CI 一段时间后,artifacts 占用的磁盘会很大,可以配置保留多少天的 artifacts,具体如下: Administration Click the Edit link for any ...