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
72 changes: 72 additions & 0 deletions design-add-and-search-words-data-structure/sadie100.ts
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: Trie, Backtracking
  • 설명: Trie 구조를 활용하여 단어 저장과 검색을 효율화하며, '.' 와일드카드 검색 시 재귀적 탐색으로 Backtracking 패턴이 적용됩니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
words를 맵형태로 저장, 각 스펠링과 다음에 나오는 문자열을 key-value로 하는 맵으로 저장
.의 경우 모든 경우를 탐색한다

시간복잡도 :
addWord : O(L) (L은 words의 길이)
search : 일반적으로 O(L), .이 많으면 O(26^k) (k는 .의 개수)

*/
class TrieNode {
children: Map<string, TrieNode>
isEnd: boolean

constructor() {
this.children = new Map()
this.isEnd = false
}
}

class WordDictionary {
words = new TrieNode()
constructor() {}

addWord(word: string): void {
let curWords = this.words

for (let i = 0; i < word.length; i++) {
const char = word[i]

if (!curWords.children.has(char)) {
curWords.children.set(char, new TrieNode())
}

curWords = curWords.children.get(char)
}
curWords.isEnd = true
}

search(word: string): boolean {
return this.searchRecursively(word, this.words)
}

searchRecursively(word: string, area: TrieNode) {
for (let i = 0; i < word.length; i++) {
if (!area) return false
const char = word[i]

if (char === '.') {
const values = [...area.children.values()]

for (let newArea of values) {
const result = this.searchRecursively(word.slice(i + 1), newArea)
if (result) return true
}
return false
} else {
if (!area.children.has(char)) return false
area = area.children.get(char)
}
}

if (area.isEnd) return true
return false
}
}

/**
* Your WordDictionary object will be instantiated and called as such:
* var obj = new WordDictionary()
* obj.addWord(word)
* var param_2 = obj.search(word)
*/
34 changes: 34 additions & 0 deletions reverse-linked-list/sadie100.ts
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🏷️ 알고리즘 패턴 분석

  • 패턴: Reverse Linked List
  • 설명: 이 코드는 연결 리스트를 뒤집는 문제로, 포인터를 이용한 순회와 역방향 연결을 수행하는 패턴입니다. 일반적인 패턴 목록에 없지만, 연결 리스트 뒤집기 특화된 패턴입니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/

/*
직전 노드를 담는 변수 before과 다음 노드를 담는 변수 next, 순회중인 노드 head 변수와 함께 list를 순회
head의 next에 before을 넣고, head를 next로 변환하며 head가 null이 아닐 때까지 반복한다

시간복잡도 : O(N) - 1번 순회
공간복잡도 : O(1) - before, next 변수

*/
function reverseList(head: ListNode | null): ListNode | null {
if (!head) return head
let originNext = head.next
let before = null

while (head !== null) {
originNext = head.next
head.next = before
before = head
head = originNext
}

return before
}
Loading