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 bestringsRearrangement(inputArray) = false.All rearrangements don't satisfy the description condition.
-
For
inputArray = ["ab", "bb", "aa"], the output should bestringsRearrangement(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
本文探讨了一道编程题:如何判断一组等长字符串是否可以通过重新排列,使得相邻字符串仅相差一个字符。文章提供了一个Python实现方案,并使用了排列组合的方法来解决这个问题。

被折叠的 条评论
为什么被折叠?



