From 3b55b40e7474ee3bf8607b0a919f4498efc36dd8 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月20日 12:10:14 +0530 Subject: [PATCH 01/10] update: total words in a Trie --- .../Trees/Trie/total-words-in-trie/index.js | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/_DataStructures_/Trees/Trie/total-words-in-trie/index.js diff --git a/src/_DataStructures_/Trees/Trie/total-words-in-trie/index.js b/src/_DataStructures_/Trees/Trie/total-words-in-trie/index.js new file mode 100644 index 00000000..10abf918 --- /dev/null +++ b/src/_DataStructures_/Trees/Trie/total-words-in-trie/index.js @@ -0,0 +1,23 @@ +// eslint-disable-next-line no-unused-vars +const Trie = require('../index'); + +function totalWords(root) { + let result = 0; + if (root.isEndOfWord) { + result += 1; + } + for (let i = 0; i < 26; i += 1) { + if (root.children[i] !== null) { + result += totalWords(root.children[i]); + } + } + return result; +} + +// const words = ['bed', 'ball', 'apple', 'java', 'javascript']; +// const trie = new Trie(); + +// words.forEach(word => trie.insert(word)); +// console.log(totalWords(trie.root)); + +module.exports = totalWords; From 42e4372eb6eea4c5a45be8a2f4fb37783a4b2588 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月20日 12:11:42 +0530 Subject: [PATCH 02/10] update: entry in Readme --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6d258a35..57d7dce1 100644 --- a/README.md +++ b/README.md @@ -31,16 +31,16 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [Implement 2 Stacks using Single Array](src/_DataStructures_/Stack/2-stacks-using1-array) - [Sort a Stack](src/_DataStructures_/Stack/sort-a-stack) -* [Queue](src/_DataStructures_/Queue) +- [Queue](src/_DataStructures_/Queue) - [Weave](src/_DataStructures_/Queue/weave) - [Reverse First K Elements of a Queue](src/_DataStructures_/Queue/reverse-first-k) - [Generate all Binary Numbers from 1 to N](src/_DataStructures_/Queue/generate-binary-number) - [Queue using Stack](src/_DataStructures_/Queue/queue-using-stack) -* [Doubly Linked List](src/_DataStructures_/DoublyLinkedList) +- [Doubly Linked List](src/_DataStructures_/DoublyLinkedList) -* [Trees](src/_DataStructures_/Trees) +- [Trees](src/_DataStructures_/Trees) - [Binary Tree (creation using level order)](src/_DataStructures_/Trees/BinaryTree) - [Binary Search Tree](src/_DataStructures_/Trees/BinarySearchTree) - [Find kth maximum in a BinarySearchTree](src/_DataStructures_/Trees/BinarySearchTree/find-kth-max) @@ -50,6 +50,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [Find k Nodes from Root of BST](src/_DataStructures_/Trees/BinarySearchTree/find-k-nodes-from-root) - [Suffix Tree](src/_DataStructures_/SuffixTree) - [Trie](src/_DataStructures_/Trees/Trie) + - [Total Words in a Trie](src/_DataStructures_/Trees/Trie/total-words-in-trie) ### Logical Problems From f77ad9d7c3c426e30152448d69e7efe4ce847216 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月20日 20:48:06 +0530 Subject: [PATCH 03/10] update: find all words in a trie --- .../Trees/Trie/all-words-in-trie/index.js | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/_DataStructures_/Trees/Trie/all-words-in-trie/index.js diff --git a/src/_DataStructures_/Trees/Trie/all-words-in-trie/index.js b/src/_DataStructures_/Trees/Trie/all-words-in-trie/index.js new file mode 100644 index 00000000..351b5263 --- /dev/null +++ b/src/_DataStructures_/Trees/Trie/all-words-in-trie/index.js @@ -0,0 +1,41 @@ +// eslint-disable-next-line no-unused-vars +const Trie = require('../index'); + +function getAllWords(root, level, word) { + let result = []; + + if (!root) return result; + + if (root.isEndOfWord) { + let temp = ''; + for (let i = 0; i < level; i += 1) { + temp += String(word[i]); + } + result = [...result, temp]; + } + + for (let i = 0; i < 26; i += 1) { + if (root.children[i] !== null) { + // eslint-disable-next-line no-param-reassign + word[level] = String.fromCharCode(i + 'a'.charCodeAt(0)); + result = [...result, ...getAllWords(root.children[i], level + 1, word)]; + } + } + return result; +} + +function allWordsFromTrie(root) { + const word = []; // char arr to store a word + for (let i = 0; i < 26; i += 1) { + word[i] = null; + } + return getAllWords(root, 0, word); +} + +// const words = ['bed', 'ball', 'apple', 'java', 'javascript']; +// const trie = new Trie(); + +// words.forEach(word => trie.insert(word)); +// console.log(allWordsFromTrie(trie.root)); + +module.exports = allWordsFromTrie; From 4f7d0c7a3270e1f4db3e0973653f10c3221b8643 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月20日 20:50:59 +0530 Subject: [PATCH 04/10] update: entry in readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 57d7dce1..fa59314d 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [Suffix Tree](src/_DataStructures_/SuffixTree) - [Trie](src/_DataStructures_/Trees/Trie) - [Total Words in a Trie](src/_DataStructures_/Trees/Trie/total-words-in-trie) + - [All the words from a Trie](src/_DataStructures_/Trees/Trie/all-words-in-trie) ### Logical Problems From 6469d5fe22be8a172e6af66e3873906cac2c0e15 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月20日 20:53:33 +0530 Subject: [PATCH 05/10] -fix: problem name fix --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fa59314d..52256da4 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [Find k Nodes from Root of BST](src/_DataStructures_/Trees/BinarySearchTree/find-k-nodes-from-root) - [Suffix Tree](src/_DataStructures_/SuffixTree) - [Trie](src/_DataStructures_/Trees/Trie) - - [Total Words in a Trie](src/_DataStructures_/Trees/Trie/total-words-in-trie) + - [Total count of words in a Trie](src/_DataStructures_/Trees/Trie/total-words-in-trie) - [All the words from a Trie](src/_DataStructures_/Trees/Trie/all-words-in-trie) ### Logical Problems From a6b3ce763b604a2fad3f03387134735a3673275d Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月22日 13:55:28 +0530 Subject: [PATCH 06/10] update: counting word ocurences --- src/_DataStructures_/Trees/Trie/Node.js | 5 +++++ src/_DataStructures_/Trees/Trie/index.js | 1 + 2 files changed, 6 insertions(+) diff --git a/src/_DataStructures_/Trees/Trie/Node.js b/src/_DataStructures_/Trees/Trie/Node.js index 05f6e673..ab6cfc27 100644 --- a/src/_DataStructures_/Trees/Trie/Node.js +++ b/src/_DataStructures_/Trees/Trie/Node.js @@ -3,6 +3,7 @@ class TrieNode { this.char = char; this.children = []; this.isEndOfWord = false; + this.wordCount = 0; // mark all the alphabets as null for (let i = 0; i < 26; i += 1) this.children[i] = null; @@ -15,6 +16,10 @@ class TrieNode { unmarkAsLeaf() { this.isEndOfWord = false; } + + increaseCount() { + this.wordCount += 1; + } } module.exports = TrieNode; diff --git a/src/_DataStructures_/Trees/Trie/index.js b/src/_DataStructures_/Trees/Trie/index.js index 58222c96..042354e6 100644 --- a/src/_DataStructures_/Trees/Trie/index.js +++ b/src/_DataStructures_/Trees/Trie/index.js @@ -32,6 +32,7 @@ class Trie { // when we are done with inserting all the character of the word, // mark the node as end leaf currentNode.markAsLeaf(); + currentNode.increaseCount(); return true; } From f0ca6ed91412c4fd9bf11102551b174ffb425045 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月22日 14:03:46 +0530 Subject: [PATCH 07/10] update: unique count --- .../Trees/Trie/total-words-in-trie/index.js | 4 ++-- .../Trees/Trie/unique-word-count/index.js | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 src/_DataStructures_/Trees/Trie/unique-word-count/index.js diff --git a/src/_DataStructures_/Trees/Trie/total-words-in-trie/index.js b/src/_DataStructures_/Trees/Trie/total-words-in-trie/index.js index 10abf918..679f9cda 100644 --- a/src/_DataStructures_/Trees/Trie/total-words-in-trie/index.js +++ b/src/_DataStructures_/Trees/Trie/total-words-in-trie/index.js @@ -4,7 +4,7 @@ const Trie = require('../index'); function totalWords(root) { let result = 0; if (root.isEndOfWord) { - result += 1; + result += root.wordCount; } for (let i = 0; i < 26; i += 1) { if (root.children[i] !== null) { @@ -14,7 +14,7 @@ function totalWords(root) { return result; } -// const words = ['bed', 'ball', 'apple', 'java', 'javascript']; +// const words = ['bed', 'ball', 'apple', 'java', 'javascript', 'bed']; // const trie = new Trie(); // words.forEach(word => trie.insert(word)); diff --git a/src/_DataStructures_/Trees/Trie/unique-word-count/index.js b/src/_DataStructures_/Trees/Trie/unique-word-count/index.js new file mode 100644 index 00000000..c71ffca2 --- /dev/null +++ b/src/_DataStructures_/Trees/Trie/unique-word-count/index.js @@ -0,0 +1,23 @@ +/* eslint-disable no-unused-vars */ +const Trie = require('../index'); + +function uniqueWordCount(root) { + let result = 0; + if (root.isEndOfWord) { + result += 1; + } + for (let i = 0; i < 26; i += 1) { + if (root.children[i]) { + result += uniqueWordCount(root.children[i]); + } + } + return result; +} + +// const words = ['bed', 'ball', 'apple', 'java', 'javascript', 'bed']; +// const trie = new Trie(); + +// words.forEach(word => trie.insert(word)); +// console.log(uniqueWordCount(trie.root)); + +module.exports = uniqueWordCount; From 2fbc809055f6539c8587e8876f9084b48ef59274 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月22日 14:07:03 +0530 Subject: [PATCH 08/10] fix: return words based on wordCount --- .../Trees/Trie/all-words-in-trie/index.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/_DataStructures_/Trees/Trie/all-words-in-trie/index.js b/src/_DataStructures_/Trees/Trie/all-words-in-trie/index.js index 351b5263..bc814859 100644 --- a/src/_DataStructures_/Trees/Trie/all-words-in-trie/index.js +++ b/src/_DataStructures_/Trees/Trie/all-words-in-trie/index.js @@ -11,7 +11,12 @@ function getAllWords(root, level, word) { for (let i = 0; i < level; i += 1) { temp += String(word[i]); } - result = [...result, temp]; + // get the count and push all the occurences + const res = []; + for (let i = 0; i < root.wordCount; i += 1) { + res.push(temp); + } + result = [...result, ...res]; } for (let i = 0; i < 26; i += 1) { @@ -32,7 +37,7 @@ function allWordsFromTrie(root) { return getAllWords(root, 0, word); } -// const words = ['bed', 'ball', 'apple', 'java', 'javascript']; +// const words = ['bed', 'ball', 'apple', 'java', 'javascript', 'bed']; // const trie = new Trie(); // words.forEach(word => trie.insert(word)); From 3c3d2c4e0cbabb095fec7624c0a1eda5a627ef26 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月22日 14:19:19 +0530 Subject: [PATCH 09/10] update: find all unique words --- .../Trees/Trie/get-unique-words/index.js | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/_DataStructures_/Trees/Trie/get-unique-words/index.js diff --git a/src/_DataStructures_/Trees/Trie/get-unique-words/index.js b/src/_DataStructures_/Trees/Trie/get-unique-words/index.js new file mode 100644 index 00000000..34bbf221 --- /dev/null +++ b/src/_DataStructures_/Trees/Trie/get-unique-words/index.js @@ -0,0 +1,40 @@ +const Trie = require('../index'); + +function getAllUniqueWords(root, level, word) { + let result = []; + + if (!root) return result; + + if (root.isEndOfWord) { + let temp = ''; + for (let i = 0; i < level; i += 1) { + temp += String(word[i]); + } + result = [...result, temp]; + } + + for (let i = 0; i < 26; i += 1) { + if (root.children[i]) { + // eslint-disable-next-line no-param-reassign + word[level] = String.fromCharCode(i + 'a'.charCodeAt(0)); + result = [...result, ...getAllUniqueWords(root.children[i], level + 1, word)]; + } + } + return result; +} + +function allUniqueWordsFromTrie(root) { + const word = []; // char arr to store a word + for (let i = 0; i < 26; i += 1) { + word[i] = null; + } + return getAllUniqueWords(root, 0, word); +} + +// const words = ['bed', 'ball', 'apple', 'java', 'javascript', 'bed']; +// const trie = new Trie(); + +// words.forEach(word => trie.insert(word)); +// console.log(allUniqueWordsFromTrie(trie.root)); + +module.exports = allUniqueWordsFromTrie; From c61dfa1d7c12fcd47afa3e19fa9958d260e1a4d8 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: 2019年10月22日 15:09:25 +0530 Subject: [PATCH 10/10] update: README entries added --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 52256da4..44345d43 100644 --- a/README.md +++ b/README.md @@ -50,8 +50,10 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct - [Find k Nodes from Root of BST](src/_DataStructures_/Trees/BinarySearchTree/find-k-nodes-from-root) - [Suffix Tree](src/_DataStructures_/SuffixTree) - [Trie](src/_DataStructures_/Trees/Trie) - - [Total count of words in a Trie](src/_DataStructures_/Trees/Trie/total-words-in-trie) + - [Total words count count in a Trie](src/_DataStructures_/Trees/Trie/total-words-in-trie) + - [Unique words count in a Trie](src/_DataStructures_/Trees/Trie/unique-word-count) - [All the words from a Trie](src/_DataStructures_/Trees/Trie/all-words-in-trie) + - [Unique words in a Trie](src/_DataStructures_/Trees/Trie/get-unique-words) ### Logical Problems

AltStyle によって変換されたページ (->オリジナル) /