Skip to content

Commit 872fbc7

Browse files
author
Albert Hu
authored
Merge pull request #17 from alberthu16/day23
Day23: stringRearrangments
2 parents 4c5c643 + af1a7b8 commit 872fbc7

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

day23/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
question of the day: https://codefights.com/challenge/RWQS5cCEodqSWx4bR
2+
3+
Given an array of equal-length strings, check if it is possible to
4+
rearrange the strings in such a way that after the rearrangement the
5+
strings at consecutive positions would differ by exactly one character.
6+
7+
Example
8+
9+
* For `inputArray = ["aba", "bbb", "bab"]`, the output should be
10+
`stringsRearrangement(inputArray) = false`.
11+
12+
All rearrangements don't satisfy the description condition.
13+
14+
* For `inputArray = ["ab", "bb", "aa"]`, the output should be
15+
`stringsRearrangement(inputArray) = true`.
16+
17+
Strings can be rearranged in the following way: `"aa", "ab", "bb"`.
18+
19+
## Ideas
20+
21+
There's a smart way to do this problem and a not-as-smart way. I'm going
22+
to use my knowledge of how to do permutations from [yesterday](../day22)
23+
to implement the brute-force check by iterating over all permutations of
24+
the input list and checking whether the current permutation is a possible
25+
solution. If it is, break there and return `true`. Otherwise, keep going
26+
until the last permutation has been checked, and then return `false`.
27+
28+
The permutations calculation would take `O(n!)` time just to generate
29+
all possible permutations of the input list. Then I also need to do an
30+
`O(n)` check each time to verify whether the permutation is a solution.
31+
Where `n` is the number of strings in the input list, the overall runtime
32+
is `O(n * n!)`.
33+
34+
## Code
35+
36+
[Python](./stringsRearrangement.py)
37+
38+
## Follow up
39+
40+
Any better way to approach this problem? I think so..
41+
42+
https://en.wikipedia.org/wiki/Hamiltonian_path_problem
43+

day23/stringsRearrangement.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from itertools import permutations
2+
3+
def stringsRearrangement(inputArray):
4+
for perm in permutations(inputArray):
5+
if neighborsDifferByOne(perm):
6+
return True
7+
return False
8+
9+
def neighborsDifferByOne(inputArray):
10+
if len(inputArray) <= 1:
11+
return True
12+
return differByOne(inputArray[0], inputArray[1]) and neighborsDifferByOne(inputArray[1:])
13+
14+
def differByOne(word, anotherWord):
15+
differencesCount = 0
16+
for i in xrange(len(word)):
17+
if word[i] != anotherWord[i]:
18+
differencesCount += 1
19+
if differencesCount > 1:
20+
return False
21+
22+
return differencesCount == 1
23+
24+
def testDifferByOne():
25+
assert not differByOne("", "")
26+
assert not differByOne("a", "a")
27+
assert not differByOne("aaa", "aaa")
28+
assert not differByOne("abcdeff", "abcedff")
29+
assert differByOne("a", "b")
30+
assert differByOne("abc", "abb")
31+
assert differByOne("abc", "bbc")
32+
assert differByOne("abcdefg", "abcdefz")
33+
34+
def testNeighborsDifferByOne():
35+
assert neighborsDifferByOne([])
36+
assert neighborsDifferByOne([""])
37+
assert neighborsDifferByOne(["a", "b"])
38+
assert neighborsDifferByOne(["a", "b", "c"])
39+
assert neighborsDifferByOne(["ab", "bb"])
40+
assert neighborsDifferByOne(["ab", "bb", "bc"])
41+
assert neighborsDifferByOne(["ab", "bb", "bc", "ba"])
42+
assert neighborsDifferByOne(["abc", "bbc", "bac", "bad"])
43+
assert not neighborsDifferByOne(["a", "a"])
44+
45+
def testStringsRearrangement():
46+
assert stringsRearrangement([])
47+
assert not stringsRearrangement(["aba", "bbb", "bab"])
48+
assert stringsRearrangement(["ab", "bb", "aac"])
49+
assert not stringsRearrangement(["qq", "qq", "qq"])
50+
assert stringsRearrangement(["aaa", "aba", "aaa", "aba", "aaa"])
51+
assert not stringsRearrangement(["ab", "ad", "ef", "eg"])
52+
assert stringsRearrangement(["abc", "abx", "axx", "abx", "abc"])
53+
assert stringsRearrangement(["f", "g", "a", "h"])
54+
55+
def main():
56+
testDifferByOne()
57+
testNeighborsDifferByOne()
58+
testStringsRearrangement()
59+
60+
if __name__ == "__main__":
61+
main()

0 commit comments

Comments
 (0)