File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change 1+ Today's challenge is a continuation of [ yesterday's] ( ../day23 ) !
2+
3+ ## Ideas
4+
5+ I wanted to take a different approach to this question; instead of doing a
6+ full brute-force exploration on all combinations of the inputArray,
7+ construct a graph and then do DFS from every node to find a possible
8+ Hamiltonian path. The existence of a path would verify whether there is
9+ a desired arrangement of the strings.
10+
11+ I can construct the graph in ` O(N<sup>2</sup>) ` time where ` N ` is the number of
12+ elements in the inputArray, and also the number of vertices in my graph. I check
13+ every possible pair of strings to see whether they differ by exactly one place,
14+ and add an edge in the graph between the two if so.
15+
16+ I can then run DFS from each node in ` N * O(N) = O(N<sup>2</sup>) ` time (this
17+ analysis doesn't seem right to me actually) to complete the algorithm.
18+
19+ Overall, it comes out to a runtime of ` O(n<sup>2</sup>) ` . The space complexity
20+ scales in proportion to the number of vertices and edges I need to keep track
21+ of in my graph. Vertices increase with every additional element in the inputArray.
22+ Edges increase when there are higher frequencies of words in the inputArray
23+ that are closer to each other in edit distance.
24+
25+ ## Code
26+
27+ [ Python] ( ./stringsRearrangementBacktracking.py ) (unfinished)
28+
29+ ## Follow up
30+
You can’t perform that action at this time.
0 commit comments