Skip to content

Commit

Permalink
[DFS] Rectify time complexity and add a solution to Word Pattern II
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Jul 4, 2018
1 parent 86355ec commit dd511ba
Show file tree
Hide file tree
Showing 16 changed files with 78 additions and 30 deletions.
2 changes: 1 addition & 1 deletion DFS/CombinationSumII.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Question Link: https://leetcode.com/problems/combination-sum-ii/
* Primary idea: Classic Depth-first Search
*
* Time Complexity: O(n!), Space Complexity: O(2^n - 2)
* Time Complexity: O(n^n), Space Complexity: O(2^n - 2)
*
*/

Expand Down
2 changes: 1 addition & 1 deletion DFS/CombinationSumIII.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Question Link: https://leetcode.com/problems/combination-sum-iii/
* Primary idea: Classic Depth-first Search
*
* Time Complexity: O(n!), Space Complexity: O(nCk)
* Time Complexity: O(n^n), Space Complexity: O(nCk)
*
*/

Expand Down
2 changes: 1 addition & 1 deletion DFS/Combinations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Question Link: https://leetcode.com/problems/combinations/
* Primary idea: Classic Depth-first Search, another version of Subsets
*
* Time Complexity: O(n!), Space Complexity: O(n)
* Time Complexity: O(n^n), Space Complexity: O(n)
*
*/

Expand Down
2 changes: 1 addition & 1 deletion DFS/ExpressionAddOperators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* 1. String cast to Integer will make character loss, e.g. "05" -> 5
* 2. Multiplication's priority is higher than addiction
*
* Time Complexity: O(n!), Space Complexity: O(n)
* Time Complexity: O(n^n), Space Complexity: O(n)
*
*/

Expand Down
2 changes: 1 addition & 1 deletion DFS/GeneralizedAbbreviation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Question Link: https://leetcode.com/problems/generalized-abbreviation/
* Primary idea: Classic Depth-first Search
*
* Time Complexity: O(n!), Space Complexity: O(2^n)
* Time Complexity: O(n^n), Space Complexity: O(2^n)
*
*/

Expand Down
2 changes: 1 addition & 1 deletion DFS/NQueens.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Primary idea: Classic Depth-first Search, fill out row by row, and check column and
* diagnol for each time
*
* Time Complexity: O(n!), Space Complexity: O(n^2)
* Time Complexity: O(n^n), Space Complexity: O(n^2)
*
*/

Expand Down
2 changes: 1 addition & 1 deletion DFS/NQueensII.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Primary idea: Classic Depth-first Search, fill out row by row, and check column and
* diagnol for each time, only need to care which column is used
*
* Time Complexity: O(n!), Space Complexity: O(n)
* Time Complexity: O(n^n), Space Complexity: O(n)
*
*/

Expand Down
2 changes: 1 addition & 1 deletion DFS/PalindromePartitioning.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Primary idea: Classic Depth-first Search, use index to track substring,
* and move forward to deeper level only if the substring is a palindrome
*
* Time Complexity: O(n!), Space Complexity: O(n)
* Time Complexity: O(n^n), Space Complexity: O(n)
*
*/

Expand Down
2 changes: 1 addition & 1 deletion DFS/Permutations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Question Link: https://leetcode.com/problems/permutations/
* Primary idea: Classic Depth-first Search, remember backtracking
*
* Time Complexity: O(n!), Space Complexity: O(n)
* Time Complexity: O(n^n), Space Complexity: O(n)
*
*/

Expand Down
2 changes: 1 addition & 1 deletion DFS/PermutationsII.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Question Link: https://leetcode.com/problems/permutations-ii/
* Primary idea: Classic Depth-first Search, adopt last occurrence to avoid dupliates
*
* Time Complexity: O(n!), Space Complexity: O(n)
* Time Complexity: O(n^n), Space Complexity: O(n)
*
*/

Expand Down
2 changes: 1 addition & 1 deletion DFS/RemoveInvalidParentheses.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Primary idea: Remove ) when the string is invalid, add to result when string is valid,
* and do the same thing for the reversed one
*
* Time Complexity: O(n!), Space Complexity: O(n)
* Time Complexity: O(n^n), Space Complexity: O(n)
*
*/

Expand Down
2 changes: 1 addition & 1 deletion DFS/Subsets.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Question Link: https://leetcode.com/problems/subsets/
* Primary idea: Classic Depth-first Search
*
* Time Complexity: O(n!), Space Complexity: O(n)
* Time Complexity: O(n^n), Space Complexity: O(n)
*
*/

Expand Down
2 changes: 1 addition & 1 deletion DFS/SubsetsII.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Question Link: https://leetcode.com/problems/subsets-ii/
* Primary idea: Classic Depth-first Search, avoid duplicates by adopting the first occurrence
*
* Time Complexity: O(n!), Space Complexity: O(n)
* Time Complexity: O(n^n), Space Complexity: O(n)
*
*/

Expand Down
47 changes: 47 additions & 0 deletions DFS/WordPatternII.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Question Link: https://leetcode.com/problems/word-pattern-ii/
* Primary idea: Depth first search, split the string to find possible word to pattern character until find the end
*
* Time Complexity: O(n^n), Space Complexity: O(n)
*
*/

class WordPatternII {
func wordPatternMatch(_ pattern: String, _ str: String) -> Bool {
return helper(pattern, str, [Character: String]())
}

fileprivate func helper(_ pattern: String, _ str: String, _ patternToWord: [Character: String]) -> Bool {
guard let firstChar = pattern.first, str.count > 0 else {
return pattern.isEmpty && str.isEmpty
}

let newPattern = String(pattern.suffix(pattern.count - 1))

if let existingWord = patternToWord[firstChar] {
if str.hasPrefix(existingWord) {
return helper(newPattern, String(str.suffix(str.count - existingWord.count)), patternToWord)
} else {
return false
}
}

for i in 0..<str.count {

let word = String(str.prefix(i + 1))

// word is already used for another pattern
if patternToWord.values.contains(word) {
continue
}

var patternToWord = patternToWord
patternToWord[firstChar] = word
if helper(newPattern, String(str.suffix(str.count - word.count)), patternToWord) {
return true
}
}

return false
}
}
2 changes: 1 addition & 1 deletion DFS/WordSquares.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Question Link: https://leetcode.com/problems/word-squares/
* 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
*
* Time Complexity: O(n!), Space Complexity: O(n)
* Time Complexity: O(n^n), Space Complexity: O(n)
*
*/

Expand Down
33 changes: 17 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* [Microsoft](#microsoft)

## Progress
[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).
[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).


## Array
Expand Down Expand Up @@ -223,31 +223,32 @@
## Depth-first search
| Title | Solution | Difficulty | Time | Space |
| ----- | -------- | ---------- | ---- | ----- |
[Permutations](https://leetcode.com/problems/permutations/)| [Swift](./DFS/Permutations.swift)| Medium| O(n!)| O(n)|
[Permutations II](https://leetcode.com/problems/permutations-ii/)| [Swift](./DFS/PermutationsII.swift)| Medium| O(n!)| O(n)|
[Subsets](https://leetcode.com/problems/subsets/)| [Swift](./DFS/Subsets.swift)| Medium| O(n!)| O(n)|
[Subsets II](https://leetcode.com/problems/subsets-ii/)| [Swift](./DFS/SubsetsII.swift)| Medium| O(n!)| O(n)|
[Combinations](https://leetcode.com/problems/combinations/)| [Swift](./DFS/Combinations.swift)| Medium| O(n!)| O(n)|
[Permutations](https://leetcode.com/problems/permutations/)| [Swift](./DFS/Permutations.swift)| Medium| O(n^n)| O(n)|
[Permutations II](https://leetcode.com/problems/permutations-ii/)| [Swift](./DFS/PermutationsII.swift)| Medium| O(n^n)| O(n)|
[Subsets](https://leetcode.com/problems/subsets/)| [Swift](./DFS/Subsets.swift)| Medium| O(n^n)| O(n)|
[Subsets II](https://leetcode.com/problems/subsets-ii/)| [Swift](./DFS/SubsetsII.swift)| Medium| O(n^n)| O(n)|
[Combinations](https://leetcode.com/problems/combinations/)| [Swift](./DFS/Combinations.swift)| Medium| O(n^n)| O(n)|
[Combination Sum](https://leetcode.com/problems/combination-sum/)| [Swift](./DFS/CombinationSum.swift)| Medium| O(n^n)| O(2^n - 1)|
[Combination Sum II](https://leetcode.com/problems/combination-sum-ii/)| [Swift](./DFS/CombinationSumII.swift)| Medium| O(n!)| O(2^n - 2)|
[Combination Sum III](https://leetcode.com/problems/combination-sum-iii/)| [Swift](./DFS/CombinationSumIII.swift)| Medium| O(n!)| O(nCk)|
[Combination Sum II](https://leetcode.com/problems/combination-sum-ii/)| [Swift](./DFS/CombinationSumII.swift)| Medium| O(n^n)| O(2^n - 2)|
[Combination Sum III](https://leetcode.com/problems/combination-sum-iii/)| [Swift](./DFS/CombinationSumIII.swift)| Medium| O(n^n)| O(nCk)|
[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)|
[Factor Combinations](https://leetcode.com/problems/factor-combinations/)| [Swift](./DFS/FactorCombinations.swift)| Medium| O(n^n))| O(2^n - 1)|
[Strobogrammatic Number II](https://leetcode.com/problems/strobogrammatic-number-ii/)| [Swift](./DFS/StrobogrammaticNumberII.swift)| Medium| O(m^n)| O(n)|
[Generalized Abbreviation](https://leetcode.com/problems/generalized-abbreviation/)| [Swift](./DFS/GeneralizedAbbreviation.swift)| Medium| O(n!)| O(2^n)|
[Palindrome Partitioning](https://leetcode.com/problems/palindrome-partitioning/)| [Swift](./DFS/PalindromePartitioning.swift)| Medium| O(n!)| O(n)|
[Generalized Abbreviation](https://leetcode.com/problems/generalized-abbreviation/)| [Swift](./DFS/GeneralizedAbbreviation.swift)| Medium| O(n^n)| O(2^n)|
[Palindrome Partitioning](https://leetcode.com/problems/palindrome-partitioning/)| [Swift](./DFS/PalindromePartitioning.swift)| Medium| O(n^n)| O(n)|
[Is Graph Bipartite](https://leetcode.com/problems/is-graph-bipartite/)| [Swift](./DFS/IsGraphBipartite.swift)| Medium| O(n)| O(n)|
[Number of Islands](https://leetcode.com/problems/number-of-islands/)| [Swift](./DFS/NumberofIslands.swift)| Medium| O((mn)^2)| O(1)|
[Walls and Gates](https://leetcode.com/problems/walls-and-gates/)| [Swift](./DFS/WallsGates.swift)| Medium| O(n!)| O(2^n)|
[Walls and Gates](https://leetcode.com/problems/walls-and-gates/)| [Swift](./DFS/WallsGates.swift)| Medium| O(n^n)| O(2^n)|
[Word Search](https://leetcode.com/problems/word-search/)| [Swift](./DFS/WordSearch.swift)| Medium| O((mn * 4 ^ (k - 1))| O(mn)|
[Word Search II](https://leetcode.com/problems/word-search-ii/)| [Swift](./DFS/WordSearchII.swift)| Hard| O(((mn)^2))| O(n^2)|
[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)|
[N-Queens](https://leetcode.com/problems/n-queens/)| [Swift](./DFS/NQueens.swift)| Hard| O((n!))| O(n^2)|
[N-Queens II](https://leetcode.com/problems/n-queens-ii/)| [Swift](./DFS/NQueensII.swift)| Hard| O((n!))| O(n)|
[Word Squares](https://leetcode.com/problems/word-squares/)| [Swift](./DFS/WordSquares.swift)| Hard| O((n!))| O(n)|
[N-Queens](https://leetcode.com/problems/n-queens/)| [Swift](./DFS/NQueens.swift)| Hard| O((n^n))| O(n^2)|
[N-Queens II](https://leetcode.com/problems/n-queens-ii/)| [Swift](./DFS/NQueensII.swift)| Hard| O((n^n))| O(n)|
[Word Squares](https://leetcode.com/problems/word-squares/)| [Swift](./DFS/WordSquares.swift)| Hard| O((n^n))| O(n)|
[Word Pattern II](https://leetcode.com/problems/word-pattern-ii/)| [Swift](./DFS/WordPatternII.swift)| Hard| O(n^n)| O(n)|
[Sudoku Solver](https://leetcode.com/problems/sudoku-solver/)| [Swift](./DFS/SudokuSolver.swift)| Hard| O(n^4)| O(1)|
[Remove Invalid Parentheses](https://leetcode.com/problems/remove-invalid-parentheses/)| [Swift](./DFS/RemoveInvalidParentheses.swift)| Hard| O(n!)| O(n)|
[Expression Add Operators](https://leetcode.com/problems/expression-add-operators/)| [Swift](./DFS/ExpressionAddOperators.swift)| Hard| O(n!)| O(n)|
[Remove Invalid Parentheses](https://leetcode.com/problems/remove-invalid-parentheses/)| [Swift](./DFS/RemoveInvalidParentheses.swift)| Hard| O(n^n)| O(n)|
[Expression Add Operators](https://leetcode.com/problems/expression-add-operators/)| [Swift](./DFS/ExpressionAddOperators.swift)| Hard| O(n^n)| O(n)|

## Breadth-first search
| Title | Solution | Difficulty | Time | Space |
Expand Down

0 comments on commit dd511ba

Please sign in to comment.