Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions week10/generate_parentheses.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Time complexity: O(2 ^ (2 * n))
// Space complexity: O(2 ^ (2 * n))
Comment on lines +1 to +2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both are wrong. The time and space complexity is O(2nCn / (n + 1)).


void generateBrackets(string currStr, int open, int close, vector<string> &bracket_list){
if(open == close && open == 0){
bracket_list.push_back(currStr);
return;
}
if(open > 0) generateBrackets(currStr + "(", open - 1, close, bracket_list);
if(open < close) generateBrackets(currStr + ")", open, close - 1, bracket_list);
}

vector<string> generateParentheses(int n){
vector<string> bracket_list;
generateBrackets("", n, n, bracket_list);
return bracket_list;
}
24 changes: 24 additions & 0 deletions week10/letter_combination_of_a_phone_number.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Time complexity: O(N * 4 ^ N), N = Length of digits.
// Space complexity: O(N * 4 ^ N), N = Length of digits.

void genCombination(string currStr, int pos, string &digits, vector<string> &digitLetterList, vector<string> &combinationList){
if(pos == digits.size()){
combinationList.push_back(currStr);
return;
}
int digit = digits[pos] - '0';
for(auto letter: digitLetterList){
genCombination(currStr + letter, pos + 1, digits, digitLetterList, combinationList);
}
}

vector<string> letterCombinations(string digits){
if(digits.size() == 0) return {};
vector<string> combinationList;
vector<string> digitLetterList = {
"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"
};
string currStr;
genCombination(currStr, 0, digits, digitLetterList, combinationList);
return combinationList;
}
19 changes: 19 additions & 0 deletions week10/subsets.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Time complexity: O(2^N) , N = Number of elements in the array.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

O(N * 2^N)

// Space complexity: O(2^N), N = Number of elements in the array.

void gen_subsets(int curr, vector<int> subset, vector<vector<int>> &subset_list, vector<int> &nums){
if(curr == nums.size()){
subset_list.push_back(subset);
return;
}
subset.push_back(nums[curr]);
gen_subsets(curr + 1, subset, subset_list, nums);
subset.pop_back();
gen_subsets(curr + 1, subset, subset_list, nums);
}

vector<vector<int>> subsets(vector<int> &nums){
vector<vector<int>> subset_list;
gen_subsets(0, {}, subset_list, nums);
return subset_list;
}
18 changes: 18 additions & 0 deletions week10/verify_preorder_sequence_in_binary_search_tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Time complexity: O(N), N = Number of elements in an array.
// Space complexity: O(N)
//

void verify_bst(vector<int> &preorder, int &curr_index, int left, int right){
if(curr_index >= preorder.size()) return;
int value = preorder[curr_index];
if(value < left || value > right) return;
curr_index++;
verify_bst(preorder, curr_index, left, value - 1);
verify_bst(preorder, curr_index, value + 1, right);
}

bool verify_preorder(vector<int> &preorder){
int curr_index = 0;
verify_bst(preorder, curr_index, INT_MIN, INT_MAX);
return curr_index == preorder.size();
}
31 changes: 31 additions & 0 deletions week10/word_search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Time complexity: O(M * N * 3 ^ L), M = Width of the board, N = Height of the board, L = Word length.
// Space complexity: O(L), L = Word length.

int dx[4] = {0, 0, 1, -1};
int dy[4] = {-1, 1, 0, 0};

bool traverseBoard(vector<vector<char>> &board, string &word, int &m, int &n, int pos, int x, int y){
if(x < 0 || x >= m || y < 0 || y >= n || word[pos] != board[x][y]) return false;
if(pos == word.size() - 1) return true;
bool status = false;
char ch = board[x][y];
board[x][y] = '#';
for(int i = 0; i < 4; i++){
status |= traverseBoard(board, word, m, n, pos+1, x + dx[i], y + dy[i]);
}
board[x][y] = ch;
return status;
}

bool exist(vector<vector<char>> &board, string word){
if(word.size() == 0) return true;
if(board.size() == 0 || board[0].size() == 0) return false;
int m = board.size();
int n = board[0].size();
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(traverseBoard(board, word, m, n, 0, i, j)) return true;
}
}
return false;
}