Type an input and write what you think is the output . Each problem is converted to Web Assembly, so any possible input will show the corresponding output. Only correct predictions count.
Locked until you have predicted 3 outputs correctly.
| 1 | struct Trie { |
| 2 | Trie* kids[26] = {}; |
| 3 | int word = -1; |
| 4 | }; |
| 5 | |
| 6 | void add(Trie* node, const string& text, int id) { |
| 7 | |
| 8 | for (char c : text) { |
| 9 | |
| 10 | int i = c - 'a'; |
| 11 | |
| 12 | if (!node->kids[i]) { |
| 13 | node->kids[i] = new Trie(); |
| 14 | } |
| 15 | |
| 16 | node = node->kids[i]; |
| 17 | } |
| 18 | |
| 19 | node->word = id; |
| 20 | } |
| 21 | |
| 22 | void hunt(vector<string>& grid, int r, int c, Trie* node, |
| 23 | vector<int>& found) { |
| 24 | |
| 25 | int rows = grid.size(); |
| 26 | int cols = grid[0].size(); |
| 27 | |
| 28 | if (r < 0 || r >= rows || c < 0 || c >= cols) { |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | char ch = grid[r][c]; |
| 33 | |
| 34 | if (ch == '*') { |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | Trie* next = node->kids[ch - 'a']; |
| 39 | |
| 40 | if (!next) { |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | if (next->word >= 0) { |
| 45 | found.push_back(next->word); |
| 46 | next->word = -1; |
| 47 | } |
| 48 | |
| 49 | grid[r][c] = '*'; |
| 50 | |
| 51 | hunt(grid, r + 1, c, next, found); |
| 52 | hunt(grid, r - 1, c, next, found); |
| 53 | hunt(grid, r, c + 1, next, found); |
| 54 | hunt(grid, r, c - 1, next, found); |
| 55 | |
| 56 | grid[r][c] = ch; |
| 57 | } |
| 58 | |
| 59 | vector<string> solution(vector<string> grid, |
| 60 | const vector<string>& words) { |
| 61 | |
| 62 | if (grid.empty() || grid[0].empty()) { |
| 63 | return {}; |
| 64 | } |
| 65 | |
| 66 | Trie root; |
| 67 | |
| 68 | for (int i = 0; i < (int)words.size(); i++) { |
| 69 | add(&root, words[i], i); |
| 70 | } |
| 71 | |
| 72 | int rows = grid.size(); |
| 73 | int cols = grid[0].size(); |
| 74 | |
| 75 | vector<int> found; |
| 76 | |
| 77 | for (int r = 0; r < rows; r++) { |
| 78 | for (int c = 0; c < cols; c++) { |
| 79 | hunt(grid, r, c, &root, found); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | sort(found.begin(), found.end()); |
| 84 | |
| 85 | vector<string> ans; |
| 86 | |
| 87 | for (int id : found) { |
| 88 | ans.push_back(words[id]); |
| 89 | } |
| 90 | |
| 91 | return ans; |
| 92 | } |
Names have been stripped. The signature is the only clue you get for free. Compiled as C++20 with the standard headers and using namespace std; already in scope.