├── L1_ImplementTrieCpp ├── L1_ImplementTrieJava ├── L2Cpp ├── L2_ImplementTrie2Java ├── L3_CompleteString_Cpp ├── L3_Complete_String_Java ├── L4_DistinctSubstrings_Cpp ├── L4_DistinctSubstrings_Java ├── L5MaxNumTrieJava ├── L5MaxPairCpp ├── L6MaxXorQueriesCpp └── L6MaxXorQueriesJava /L1_ImplementTrieCpp: -------------------------------------------------------------------------------- 1 | /* 2 | Your Trie object will be instantiated and called as such: 3 | Trie* obj = new Trie(); 4 | obj->insert(word); 5 | bool check2 = obj->search(word); 6 | bool check3 = obj->startsWith(prefix); 7 | */ 8 | 9 | struct Node { 10 | Node *links[26]; 11 | bool flag = false; 12 | 13 | bool containsKey(char ch) { 14 | return (links[ch - 'a'] != NULL); 15 | } 16 | Node* get(char ch) { 17 | return links[ch-'a']; 18 | } 19 | void put(char ch, Node* node) { 20 | links[ch-'a'] = node; 21 | } 22 | void setEnd() { 23 | flag = true; 24 | } 25 | bool isEnd() { 26 | return flag; 27 | } 28 | }; 29 | class Trie { 30 | private: Node *root; 31 | public: 32 | /** Initialize your data structure here. */ 33 | Trie() { 34 | root = new Node(); 35 | } 36 | 37 | /** Inserts a word into the trie. */ 38 | void insert(string word) { 39 | Node *node = root; 40 | for(int i = 0;icontainsKey(word[i])) { 42 | node->put(word[i], new Node()); 43 | } 44 | node = node->get(word[i]); 45 | } 46 | node->setEnd(); 47 | } 48 | 49 | /** Returns if the word is in the trie. */ 50 | bool search(string word) { 51 | Node *node = root; 52 | for(int i = 0;icontainsKey(word[i])) { 54 | return false; 55 | } 56 | node = node->get(word[i]); 57 | } 58 | if(node->isEnd()) { 59 | return true; 60 | } 61 | return false; 62 | } 63 | 64 | /** Returns if there is any word in the trie that starts with the given prefix. */ 65 | bool startsWith(string prefix) { 66 | Node *node = root; 67 | for(int i = 0;icontainsKey(prefix[i])) { 69 | return false; 70 | } 71 | node = node->get(prefix[i]); 72 | } 73 | return true; 74 | } 75 | }; 76 | -------------------------------------------------------------------------------- /L1_ImplementTrieJava: -------------------------------------------------------------------------------- 1 | class Node { 2 | Node links[] = new Node[26]; 3 | boolean flag = false; 4 | 5 | public Node() { 6 | 7 | } 8 | 9 | boolean containsKey(char ch) { 10 | return (links[ch - 'a'] != null); 11 | } 12 | Node get(char ch) { 13 | return links[ch-'a']; 14 | } 15 | void put(char ch, Node node) { 16 | links[ch-'a'] = node; 17 | } 18 | void setEnd() { 19 | flag = true; 20 | } 21 | boolean isEnd() { 22 | return flag; 23 | } 24 | }; 25 | public class Trie { 26 | private static Node root; 27 | 28 | //Initialize your data structure here 29 | 30 | Trie() { 31 | root = new Node(); 32 | } 33 | 34 | 35 | //Inserts a word into the trie 36 | 37 | public static void insert(String word) { 38 | Node node = root; 39 | for(int i = 0;icontainsKey(word[i])) { 47 | node->put(word[i], new Node()); 48 | } 49 | node = node->get(word[i]); 50 | node->increasePrefix(); 51 | } 52 | node->increaseEnd(); 53 | } 54 | 55 | int countWordsEqualTo(string &word){ 56 | Node* node = root; 57 | for(int i = 0;icontainsKey(word[i])) { 59 | node = node->get(word[i]); 60 | } 61 | else { 62 | return 0; 63 | } 64 | } 65 | return node->getEnd(); 66 | } 67 | 68 | int countWordsStartingWith(string &word){ 69 | Node* node = root; 70 | for(int i = 0;icontainsKey(word[i])) { 72 | node = node->get(word[i]); 73 | } 74 | else { 75 | return 0; 76 | } 77 | } 78 | return node->getPrefix(); 79 | } 80 | 81 | void erase(string &word){ 82 | Node* node = root; 83 | for(int i = 0;icontainsKey(word[i])) { 85 | node = node->get(word[i]); 86 | node->reducePrefix(); 87 | } 88 | else { 89 | return; 90 | } 91 | } 92 | node->deleteEnd(); 93 | } 94 | }; 95 | -------------------------------------------------------------------------------- /L2_ImplementTrie2Java: -------------------------------------------------------------------------------- 1 | class Node { 2 | Node links[] = new Node[26]; 3 | int cntEndWith = 0; 4 | int cntPrefix = 0; 5 | 6 | public Node() { 7 | } 8 | 9 | boolean containsKey(char ch) { 10 | return (links[ch - 'a'] != null); 11 | } 12 | Node get(char ch) { 13 | return links[ch-'a']; 14 | } 15 | void put(char ch, Node node) { 16 | links[ch-'a'] = node; 17 | 18 | } 19 | void increaseEnd() { 20 | cntEndWith++; 21 | } 22 | void increasePrefix() { 23 | cntPrefix++; 24 | } 25 | void deleteEnd() { 26 | cntEndWith--; 27 | } 28 | void reducePrefix() { 29 | cntPrefix--; 30 | } 31 | int getEnd() { 32 | return cntEndWith; 33 | } 34 | int getPrefix() { 35 | return cntPrefix; 36 | } 37 | }; 38 | public class Trie { 39 | private Node root; 40 | 41 | //Initialize your data structure here 42 | 43 | Trie() { 44 | root = new Node(); 45 | } 46 | 47 | 48 | //Inserts a word into the trie 49 | 50 | public void insert(String word) { 51 | Node node = root; 52 | for(int i = 0;icontainsKey(word[i])) { 34 | node->put(word[i], new Node()); 35 | } 36 | node = node->get(word[i]); 37 | } 38 | node->setEnd(); 39 | } 40 | bool checkIfAllPrefixExists(string word) { 41 | Node *node = root; 42 | bool flag = true; 43 | for(int i = 0;icontainsKey(word[i])) { 45 | node = node->get(word[i]); 46 | flag = flag & node->isEnd(); 47 | } 48 | else { 49 | return false; 50 | } 51 | } 52 | return flag; 53 | } 54 | }; 55 | string completeString(int n, vector &a){ 56 | Trie* obj = new Trie(); 57 | for(auto word : a) obj->insert(word); 58 | string longest = ""; 59 | for(auto word: a) { 60 | if(obj->checkIfAllPrefixExists(word)) { 61 | if(word.size() > longest.size()) { 62 | longest = word; 63 | } 64 | else if(word.size() == longest.size() && word < longest) { 65 | longest = word; 66 | } 67 | } 68 | } 69 | if(longest == "") return "None"; 70 | return longest; 71 | } 72 | -------------------------------------------------------------------------------- /L3_Complete_String_Java: -------------------------------------------------------------------------------- 1 | struct Node { 2 | Node *links[26]; 3 | bool flag = false; 4 | 5 | bool containsKey(char ch) { 6 | return (links[ch - 'a'] != NULL); 7 | } 8 | Node* get(char ch) { 9 | return links[ch-'a']; 10 | } 11 | void put(char ch, Node* node) { 12 | links[ch-'a'] = node; 13 | } 14 | void setEnd() { 15 | flag = true; 16 | } 17 | bool isEnd() { 18 | return flag; 19 | } 20 | }; 21 | class Trie { 22 | private: Node *root; 23 | public: 24 | /** Initialize your data structure here. */ 25 | Trie() { 26 | root = new Node(); 27 | } 28 | 29 | /** Inserts a word into the trie. */ 30 | void insert(string word) { 31 | Node *node = root; 32 | for(int i = 0;icontainsKey(word[i])) { 34 | node->put(word[i], new Node()); 35 | } 36 | node = node->get(word[i]); 37 | } 38 | node->setEnd(); 39 | } 40 | bool checkIfAllPrefixExists(string word) { 41 | Node *node = root; 42 | bool flag = true; 43 | for(int i = 0;icontainsKey(word[i])) { 45 | node = node->get(word[i]); 46 | flag = flag & node->isEnd(); 47 | } 48 | else { 49 | return false; 50 | } 51 | } 52 | return flag; 53 | } 54 | }; 55 | string completeString(int n, vector &a){ 56 | Trie* obj = new Trie(); 57 | for(auto word : a) obj->insert(word); 58 | string longest = ""; 59 | for(auto word: a) { 60 | if(obj->checkIfAllPrefixExists(word)) { 61 | if(word.size() > longest.size()) { 62 | longest = word; 63 | } 64 | else if(word.size() == longest.size() && word < longest) { 65 | longest = word; 66 | } 67 | } 68 | } 69 | if(longest == "") return "None"; 70 | return longest; 71 | } 72 | -------------------------------------------------------------------------------- /L4_DistinctSubstrings_Cpp: -------------------------------------------------------------------------------- 1 | struct Node { 2 | Node *links[26]; 3 | bool flag = false; 4 | 5 | bool containsKey(char ch) { 6 | return (links[ch - 'a'] != NULL); 7 | } 8 | Node* get(char ch) { 9 | return links[ch-'a']; 10 | } 11 | void put(char ch, Node* node) { 12 | links[ch-'a'] = node; 13 | } 14 | void setEnd() { 15 | flag = true; 16 | } 17 | bool isEnd() { 18 | return flag; 19 | } 20 | }; 21 | int countDistinctSubstrings(string &s) 22 | { 23 | Node* root = new Node(); 24 | int cnt = 0; 25 | int n = s.size(); 26 | for(int i = 0;icontainsKey(s[j])) { 31 | node->put(s[j], new Node()); 32 | cnt++; 33 | } 34 | node = node->get(s[j]); 35 | } 36 | } 37 | return cnt + 1; // Write your code here. 38 | } 39 | -------------------------------------------------------------------------------- /L4_DistinctSubstrings_Java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.ArrayList; 3 | class Node { 4 | Node links[] = new Node[26]; 5 | boolean flag = false; 6 | 7 | public Node() { 8 | 9 | } 10 | 11 | boolean containsKey(char ch) { 12 | return (links[ch - 'a'] != null); 13 | } 14 | Node get(char ch) { 15 | return links[ch-'a']; 16 | } 17 | void put(char ch, Node node) { 18 | links[ch-'a'] = node; 19 | } 20 | void setEnd() { 21 | flag = true; 22 | } 23 | boolean isEnd() { 24 | return flag; 25 | } 26 | }; 27 | 28 | public class Solution 29 | { 30 | public static int countDistinctSubstrings(String s) 31 | { 32 | Node root = new Node(); 33 | int n = s.length(); 34 | int cnt = 0; 35 | for(int i = 0; i < n;i++) { 36 | Node node = root; 37 | for(int j = i;j=0;i--) { 34 | int bit = (num >> i) & 1; 35 | if(!node.containsKey(bit)) { 36 | node.put(bit, new Node()); 37 | } 38 | node = node.get(bit); 39 | } 40 | } 41 | 42 | public int getMax(int num) { 43 | Node node = root; 44 | int maxNum = 0; 45 | for(int i = 31;i>=0;i--) { 46 | int bit = (num >> i) & 1; 47 | if(node.containsKey(1 - bit)) { 48 | maxNum = maxNum | (1< arr1, ArrayList arr2) 62 | { 63 | Trie trie = new Trie(); 64 | for(int i = 0;i=0;i--) { 26 | int bit = (num >> i) & 1; 27 | if(!node->containsKey(bit)) { 28 | node->put(bit, new Node()); 29 | } 30 | node = node->get(bit); 31 | } 32 | } 33 | public: 34 | int findMax(int num) { 35 | Node* node = root; 36 | int maxNum = 0; 37 | for(int i = 31;i>=0;i--) { 38 | int bit = (num >> i) & 1; 39 | if(node->containsKey(!bit)) { 40 | maxNum = maxNum | (1<get(!bit); 42 | } 43 | else { 44 | node = node->get(bit); 45 | } 46 | } 47 | return maxNum; 48 | } 49 | }; 50 | int maxXOR(int n, int m, vector &arr1, vector &arr2) 51 | { 52 | Trie trie; 53 | for(int i = 0;i 2 | struct Node { 3 | Node *links[2]; 4 | 5 | bool containsKey(int ind) { 6 | return (links[ind] != NULL); 7 | } 8 | Node* get(int ind) { 9 | return links[ind]; 10 | } 11 | void put(int ind, Node* node) { 12 | links[ind] = node; 13 | } 14 | }; 15 | class Trie { 16 | private: Node* root; 17 | public: 18 | Trie() { 19 | root = new Node(); 20 | } 21 | 22 | public: 23 | void insert(int num) { 24 | Node* node = root; 25 | // cout << num << endl; 26 | for(int i = 31;i>=0;i--) { 27 | int bit = (num >> i) & 1; 28 | if(!node->containsKey(bit)) { 29 | node->put(bit, new Node()); 30 | } 31 | node = node->get(bit); 32 | } 33 | } 34 | public: 35 | int findMax(int num) { 36 | Node* node = root; 37 | int maxNum = 0; 38 | for(int i = 31;i>=0;i--) { 39 | int bit = (num >> i) & 1; 40 | if(node->containsKey(!bit)) { 41 | maxNum = maxNum | (1<get(!bit); 43 | } 44 | else { 45 | node = node->get(bit); 46 | } 47 | } 48 | return maxNum; 49 | } 50 | }; 51 | vector maxXorQueries(vector &arr, vector> &queries){ 52 | vector ans(queries.size(), 0); 53 | vector>> offlineQueries; 54 | sort(arr.begin(), arr.end()); 55 | int index = 0; 56 | for(auto &it: queries) { 57 | offlineQueries.push_back({it[1],{it[0], index++}}); 58 | } 59 | sort(offlineQueries.begin(), offlineQueries.end()); 60 | int i = 0; 61 | int n = arr.size(); 62 | Trie trie; 63 | 64 | for(auto &it : offlineQueries) { 65 | while(i < n && arr[i] <= it.first) { 66 | trie.insert(arr[i]); 67 | i++; 68 | } 69 | if(i!=0) ans[it.second.second] = trie.findMax(it.second.first); 70 | else ans[it.second.second] = -1; 71 | } 72 | return ans; 73 | } 74 | -------------------------------------------------------------------------------- /L6MaxXorQueriesJava: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.*; 3 | class Node { 4 | Node links[] = new Node[2]; 5 | 6 | public Node() { 7 | } 8 | boolean containsKey(int ind) { 9 | return (links[ind] != null); 10 | } 11 | Node get(int ind) { 12 | return links[ind]; 13 | } 14 | void put(int ind, Node node) { 15 | links[ind] = node; 16 | } 17 | }; 18 | class Trie { 19 | private static Node root; 20 | 21 | //Initialize your data structure here 22 | 23 | Trie() { 24 | root = new Node(); 25 | } 26 | 27 | 28 | //Inserts a word into the trie 29 | 30 | public static void insert(int num) { 31 | Node node = root; 32 | for(int i = 31;i>=0;i--) { 33 | int bit = (num >> i) & 1; 34 | if(!node.containsKey(bit)) { 35 | node.put(bit, new Node()); 36 | } 37 | node = node.get(bit); 38 | } 39 | } 40 | 41 | public int getMax(int num) { 42 | Node node = root; 43 | int maxNum = 0; 44 | for(int i = 31;i>=0;i--) { 45 | int bit = (num >> i) & 1; 46 | if(node.containsKey(1 - bit)) { 47 | maxNum = maxNum | (1< maxXorQueries(ArrayList arr, ArrayList> queries) { 59 | Collections.sort(arr); 60 | ArrayList> offlineQueries = new ArrayList>(); 61 | int m = queries.size(); 62 | for(int i = 0;i temp = new ArrayList(); 64 | temp.add(queries.get(i).get(1)); 65 | temp.add(queries.get(i).get(0)); 66 | temp.add(i); 67 | offlineQueries.add(temp); 68 | } 69 | Collections.sort(offlineQueries, new Comparator> () { 70 | @Override 71 | public int compare(ArrayList a, ArrayList b) { 72 | return a.get(0).compareTo(b.get(0)); 73 | } 74 | }); 75 | int ind = 0; 76 | int n = arr.size(); 77 | Trie trie = new Trie(); 78 | ArrayList ans = new ArrayList(m); 79 | for(int i = 0;i