Code Signal_练习题_stringsRearrangement

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
膜拜大佬

 

转载于:https://www.cnblogs.com/BlameKidd/p/9477429.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值