Skip to content

Commit dd511ba

Browse files
committed
[DFS] Rectify time complexity and add a solution to Word Pattern II
1 parent 86355ec commit dd511ba

16 files changed

+78
-30
lines changed

DFS/CombinationSumII.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Question Link: https://leetcode.com/problems/combination-sum-ii/
33
* Primary idea: Classic Depth-first Search
44
*
5-
* Time Complexity: O(n!), Space Complexity: O(2^n - 2)
5+
* Time Complexity: O(n^n), Space Complexity: O(2^n - 2)
66
*
77
*/
88

DFS/CombinationSumIII.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Question Link: https://leetcode.com/problems/combination-sum-iii/
33
* Primary idea: Classic Depth-first Search
44
*
5-
* Time Complexity: O(n!), Space Complexity: O(nCk)
5+
* Time Complexity: O(n^n), Space Complexity: O(nCk)
66
*
77
*/
88

DFS/Combinations.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Question Link: https://leetcode.com/problems/combinations/
33
* Primary idea: Classic Depth-first Search, another version of Subsets
44
*
5-
* Time Complexity: O(n!), Space Complexity: O(n)
5+
* Time Complexity: O(n^n), Space Complexity: O(n)
66
*
77
*/
88

DFS/ExpressionAddOperators.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* 1. String cast to Integer will make character loss, e.g. "05" -> 5
88
* 2. Multiplication's priority is higher than addiction
99
*
10-
* Time Complexity: O(n!), Space Complexity: O(n)
10+
* Time Complexity: O(n^n), Space Complexity: O(n)
1111
*
1212
*/
1313

DFS/GeneralizedAbbreviation.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Question Link: https://leetcode.com/problems/generalized-abbreviation/
33
* Primary idea: Classic Depth-first Search
44
*
5-
* Time Complexity: O(n!), Space Complexity: O(2^n)
5+
* Time Complexity: O(n^n), Space Complexity: O(2^n)
66
*
77
*/
88

DFS/NQueens.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Primary idea: Classic Depth-first Search, fill out row by row, and check column and
44
* diagnol for each time
55
*
6-
* Time Complexity: O(n!), Space Complexity: O(n^2)
6+
* Time Complexity: O(n^n), Space Complexity: O(n^2)
77
*
88
*/
99

DFS/NQueensII.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Primary idea: Classic Depth-first Search, fill out row by row, and check column and
44
* diagnol for each time, only need to care which column is used
55
*
6-
* Time Complexity: O(n!), Space Complexity: O(n)
6+
* Time Complexity: O(n^n), Space Complexity: O(n)
77
*
88
*/
99

DFS/PalindromePartitioning.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Primary idea: Classic Depth-first Search, use index to track substring,
44
* and move forward to deeper level only if the substring is a palindrome
55
*
6-
* Time Complexity: O(n!), Space Complexity: O(n)
6+
* Time Complexity: O(n^n), Space Complexity: O(n)
77
*
88
*/
99

DFS/Permutations.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Question Link: https://leetcode.com/problems/permutations/
33
* Primary idea: Classic Depth-first Search, remember backtracking
44
*
5-
* Time Complexity: O(n!), Space Complexity: O(n)
5+
* Time Complexity: O(n^n), Space Complexity: O(n)
66
*
77
*/
88

DFS/PermutationsII.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Question Link: https://leetcode.com/problems/permutations-ii/
33
* Primary idea: Classic Depth-first Search, adopt last occurrence to avoid dupliates
44
*
5-
* Time Complexity: O(n!), Space Complexity: O(n)
5+
* Time Complexity: O(n^n), Space Complexity: O(n)
66
*
77
*/
88

DFS/RemoveInvalidParentheses.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Primary idea: Remove ) when the string is invalid, add to result when string is valid,
44
* and do the same thing for the reversed one
55
*
6-
* Time Complexity: O(n!), Space Complexity: O(n)
6+
* Time Complexity: O(n^n), Space Complexity: O(n)
77
*
88
*/
99

DFS/Subsets.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Question Link: https://leetcode.com/problems/subsets/
33
* Primary idea: Classic Depth-first Search
44
*
5-
* Time Complexity: O(n!), Space Complexity: O(n)
5+
* Time Complexity: O(n^n), Space Complexity: O(n)
66
*
77
*/
88

DFS/SubsetsII.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Question Link: https://leetcode.com/problems/subsets-ii/
33
* Primary idea: Classic Depth-first Search, avoid duplicates by adopting the first occurrence
44
*
5-
* Time Complexity: O(n!), Space Complexity: O(n)
5+
* Time Complexity: O(n^n), Space Complexity: O(n)
66
*
77
*/
88

DFS/WordPatternII.swift

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/word-pattern-ii/
3+
* Primary idea: Depth first search, split the string to find possible word to pattern character until find the end
4+
*
5+
* Time Complexity: O(n^n), Space Complexity: O(n)
6+
*
7+
*/
8+
9+
class WordPatternII {
10+
func wordPatternMatch(_ pattern: String, _ str: String) -> Bool {
11+
return helper(pattern, str, [Character: String]())
12+
}
13+
14+
fileprivate func helper(_ pattern: String, _ str: String, _ patternToWord: [Character: String]) -> Bool {
15+
guard let firstChar = pattern.first, str.count > 0 else {
16+
return pattern.isEmpty && str.isEmpty
17+
}
18+
19+
let newPattern = String(pattern.suffix(pattern.count - 1))
20+
21+
if let existingWord = patternToWord[firstChar] {
22+
if str.hasPrefix(existingWord) {
23+
return helper(newPattern, String(str.suffix(str.count - existingWord.count)), patternToWord)
24+
} else {
25+
return false
26+
}
27+
}
28+
29+
for i in 0..<str.count {
30+
31+
let word = String(str.prefix(i + 1))
32+
33+
// word is already used for another pattern
34+
if patternToWord.values.contains(word) {
35+
continue
36+
}
37+
38+
var patternToWord = patternToWord
39+
patternToWord[firstChar] = word
40+
if helper(newPattern, String(str.suffix(str.count - word.count)), patternToWord) {
41+
return true
42+
}
43+
}
44+
45+
return false
46+
}
47+
}

DFS/WordSquares.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Question Link: https://leetcode.com/problems/word-squares/
33
* Primary idea: Classic Depth-first Search, fill out row by row, choose correct word with fixed prefix, only need to care which column is used
44
*
5-
* Time Complexity: O(n!), Space Complexity: O(n)
5+
* Time Complexity: O(n^n), Space Complexity: O(n)
66
*
77
*/
88

README.md

+17-16
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* [Microsoft](#microsoft)
2929

3030
## Progress
31-
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 261 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
31+
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 262 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
3232

3333

3434
## Array
@@ -223,31 +223,32 @@
223223
## Depth-first search
224224
| Title | Solution | Difficulty | Time | Space |
225225
| ----- | -------- | ---------- | ---- | ----- |
226-
[Permutations](https://leetcode.com/problems/permutations/)| [Swift](./DFS/Permutations.swift)| Medium| O(n!)| O(n)|
227-
[Permutations II](https://leetcode.com/problems/permutations-ii/)| [Swift](./DFS/PermutationsII.swift)| Medium| O(n!)| O(n)|
228-
[Subsets](https://leetcode.com/problems/subsets/)| [Swift](./DFS/Subsets.swift)| Medium| O(n!)| O(n)|
229-
[Subsets II](https://leetcode.com/problems/subsets-ii/)| [Swift](./DFS/SubsetsII.swift)| Medium| O(n!)| O(n)|
230-
[Combinations](https://leetcode.com/problems/combinations/)| [Swift](./DFS/Combinations.swift)| Medium| O(n!)| O(n)|
226+
[Permutations](https://leetcode.com/problems/permutations/)| [Swift](./DFS/Permutations.swift)| Medium| O(n^n)| O(n)|
227+
[Permutations II](https://leetcode.com/problems/permutations-ii/)| [Swift](./DFS/PermutationsII.swift)| Medium| O(n^n)| O(n)|
228+
[Subsets](https://leetcode.com/problems/subsets/)| [Swift](./DFS/Subsets.swift)| Medium| O(n^n)| O(n)|
229+
[Subsets II](https://leetcode.com/problems/subsets-ii/)| [Swift](./DFS/SubsetsII.swift)| Medium| O(n^n)| O(n)|
230+
[Combinations](https://leetcode.com/problems/combinations/)| [Swift](./DFS/Combinations.swift)| Medium| O(n^n)| O(n)|
231231
[Combination Sum](https://leetcode.com/problems/combination-sum/)| [Swift](./DFS/CombinationSum.swift)| Medium| O(n^n)| O(2^n - 1)|
232-
[Combination Sum II](https://leetcode.com/problems/combination-sum-ii/)| [Swift](./DFS/CombinationSumII.swift)| Medium| O(n!)| O(2^n - 2)|
233-
[Combination Sum III](https://leetcode.com/problems/combination-sum-iii/)| [Swift](./DFS/CombinationSumIII.swift)| Medium| O(n!)| O(nCk)|
232+
[Combination Sum II](https://leetcode.com/problems/combination-sum-ii/)| [Swift](./DFS/CombinationSumII.swift)| Medium| O(n^n)| O(2^n - 2)|
233+
[Combination Sum III](https://leetcode.com/problems/combination-sum-iii/)| [Swift](./DFS/CombinationSumIII.swift)| Medium| O(n^n)| O(nCk)|
234234
[Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/)| [Swift](./DFS/LetterCombinationsPhoneNumber.swift)| Medium| O(mn)| O(n)|
235235
[Factor Combinations](https://leetcode.com/problems/factor-combinations/)| [Swift](./DFS/FactorCombinations.swift)| Medium| O(n^n))| O(2^n - 1)|
236236
[Strobogrammatic Number II](https://leetcode.com/problems/strobogrammatic-number-ii/)| [Swift](./DFS/StrobogrammaticNumberII.swift)| Medium| O(m^n)| O(n)|
237-
[Generalized Abbreviation](https://leetcode.com/problems/generalized-abbreviation/)| [Swift](./DFS/GeneralizedAbbreviation.swift)| Medium| O(n!)| O(2^n)|
238-
[Palindrome Partitioning](https://leetcode.com/problems/palindrome-partitioning/)| [Swift](./DFS/PalindromePartitioning.swift)| Medium| O(n!)| O(n)|
237+
[Generalized Abbreviation](https://leetcode.com/problems/generalized-abbreviation/)| [Swift](./DFS/GeneralizedAbbreviation.swift)| Medium| O(n^n)| O(2^n)|
238+
[Palindrome Partitioning](https://leetcode.com/problems/palindrome-partitioning/)| [Swift](./DFS/PalindromePartitioning.swift)| Medium| O(n^n)| O(n)|
239239
[Is Graph Bipartite](https://leetcode.com/problems/is-graph-bipartite/)| [Swift](./DFS/IsGraphBipartite.swift)| Medium| O(n)| O(n)|
240240
[Number of Islands](https://leetcode.com/problems/number-of-islands/)| [Swift](./DFS/NumberofIslands.swift)| Medium| O((mn)^2)| O(1)|
241-
[Walls and Gates](https://leetcode.com/problems/walls-and-gates/)| [Swift](./DFS/WallsGates.swift)| Medium| O(n!)| O(2^n)|
241+
[Walls and Gates](https://leetcode.com/problems/walls-and-gates/)| [Swift](./DFS/WallsGates.swift)| Medium| O(n^n)| O(2^n)|
242242
[Word Search](https://leetcode.com/problems/word-search/)| [Swift](./DFS/WordSearch.swift)| Medium| O((mn * 4 ^ (k - 1))| O(mn)|
243243
[Word Search II](https://leetcode.com/problems/word-search-ii/)| [Swift](./DFS/WordSearchII.swift)| Hard| O(((mn)^2))| O(n^2)|
244244
[Add and Search Word - Data structure design](https://leetcode.com/problems/add-and-search-word-data-structure-design/)| [Swift](./DFS/WordDictionary.swift)| Medium| O(n)| O(n)|
245-
[N-Queens](https://leetcode.com/problems/n-queens/)| [Swift](./DFS/NQueens.swift)| Hard| O((n!))| O(n^2)|
246-
[N-Queens II](https://leetcode.com/problems/n-queens-ii/)| [Swift](./DFS/NQueensII.swift)| Hard| O((n!))| O(n)|
247-
[Word Squares](https://leetcode.com/problems/word-squares/)| [Swift](./DFS/WordSquares.swift)| Hard| O((n!))| O(n)|
245+
[N-Queens](https://leetcode.com/problems/n-queens/)| [Swift](./DFS/NQueens.swift)| Hard| O((n^n))| O(n^2)|
246+
[N-Queens II](https://leetcode.com/problems/n-queens-ii/)| [Swift](./DFS/NQueensII.swift)| Hard| O((n^n))| O(n)|
247+
[Word Squares](https://leetcode.com/problems/word-squares/)| [Swift](./DFS/WordSquares.swift)| Hard| O((n^n))| O(n)|
248+
[Word Pattern II](https://leetcode.com/problems/word-pattern-ii/)| [Swift](./DFS/WordPatternII.swift)| Hard| O(n^n)| O(n)|
248249
[Sudoku Solver](https://leetcode.com/problems/sudoku-solver/)| [Swift](./DFS/SudokuSolver.swift)| Hard| O(n^4)| O(1)|
249-
[Remove Invalid Parentheses](https://leetcode.com/problems/remove-invalid-parentheses/)| [Swift](./DFS/RemoveInvalidParentheses.swift)| Hard| O(n!)| O(n)|
250-
[Expression Add Operators](https://leetcode.com/problems/expression-add-operators/)| [Swift](./DFS/ExpressionAddOperators.swift)| Hard| O(n!)| O(n)|
250+
[Remove Invalid Parentheses](https://leetcode.com/problems/remove-invalid-parentheses/)| [Swift](./DFS/RemoveInvalidParentheses.swift)| Hard| O(n^n)| O(n)|
251+
[Expression Add Operators](https://leetcode.com/problems/expression-add-operators/)| [Swift](./DFS/ExpressionAddOperators.swift)| Hard| O(n^n)| O(n)|
251252

252253
## Breadth-first search
253254
| Title | Solution | Difficulty | Time | Space |

0 commit comments

Comments
 (0)