码迷,mamicode.com
首页 > 其他好文 > 详细

Code Signal_练习题_stringsRearrangement

时间:2018-08-14 21:00:02      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:分享   img   tool   strong   col   rto   bbb   check   follow   

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

标签:分享   img   tool   strong   col   rto   bbb   check   follow   

原文地址:https://www.cnblogs.com/YD2018/p/9477429.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!