Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[anniemon78] Week6 #915

Merged
merged 2 commits into from
Jan 18, 2025
Merged
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
66 changes: 66 additions & 0 deletions design-add-and-search-words-data-structure/anniemon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
var WordDictionary = function() {
this.children = {};
this.isEnd = false;
};
/**
* 시간 복잡도: word의 길이만큼 순회하므로, O(n)
* 공간 복잡도: 최대 word의 길이만큼 추가 노드를 생성하므로, O(n)
*/
/**
* @param {string} word
* @return {void}
*/
WordDictionary.prototype.addWord = function(word) {
let children = this.children;
for(let w of word) {
if(!children[w]) {
children[w] = {};
children[w].isEnd = false;
}
children = children[w];
}
children.isEnd = true;
};
/**
* 시간 복잡도:
* 최악의 경우 각 노드의 자식 노드는 모든 알파벳의 갯수인 26이 되고,
* 재귀 호출 스택의 깊이는 word.length에 비례하므로,
* word의 길이가 n이면, 시간 복잡도는 O(26^n)
* 공간 복잡도:
* 호출 스택 깊이는 n이므로, 공간 복잡도는 O(n)
*/
/**
* @param {string} word
* @return {boolean}
*/
WordDictionary.prototype.search = function(word) {
let children = this.children;
return this.dfs(0, word, children)
};

WordDictionary.prototype.dfs = function(i, word, children) {
if(i === word.length) {
return children.isEnd;
}
if(word[i] === '.') {
for(const c in children) {
if(c === 'isEnd') continue;
if(this.dfs(i+1, word, children[c])) {
return true;
}
}
return false;
} else {
if(!children[word[i]]) {
return false;
}
return this.dfs(i+1, word, children[word[i]]);
}
}

/**
* Your WordDictionary object will be instantiated and called as such:
* var obj = new WordDictionary()
* obj.addWord(word)
* var param_2 = obj.search(word)
*/
27 changes: 27 additions & 0 deletions valid-parentheses/anniemon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 시간 복잡도: s의 길이만큼 순회하므로, O(n)
* 공간 복잡도: 스택은 최대 s의 길이만큼 문자를 저장하므로, O(n)
*/
/**
* @param {string} s
* @return {boolean}
*/
var isValid = function(s) {
const stack = [];
const brackets = {
'(': ')',
'{': '}',
'[': ']'
}
for(const b of s) {
if(b in brackets) {
stack.push(b);
} else {
const cur = stack.pop();
sungjinwi marked this conversation as resolved.
Show resolved Hide resolved
if(brackets[cur] !== b) {
return false;
}
}
}
return stack.length === 0;
};
Loading