Open In App

Count of given Strings in 2D character Array using Trie

Last Updated : 30 Nov, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Counting the number of occurrences of a given string in a 2D character array using a Trie.

Examples:

Input: vector<string> grid = {"abcde", "fghij", "xyabc", "klmno",}; string word = "abc";
Output: 2
Explanation: abc occurs twice in "abcde", "xyabc"

Input: vector<string> grid = {"abcde", "fghij", "xyabc", "klmno",}; string word = "klm";
Output: 1
Explanation: klm occurs once in "klmno"

Approach:

Counting the number of occurrences of a given string in a 2D character array using a Trie approach involves building a Trie data structure from the 2D array and then searching for the given string in the Trie.

  • We first build a Trie from the given word.
  • Then, we iterate through the 2D character array and construct substrings from each starting position.
  • If a substring exists in the Trie, we increment the count.
  • Finally, we return the count, which represents the number of occurrences of the given string in the 2D array.

Implementation:

C++
#include <iostream>
#include <vector>
using namespace std;

const int ALPHABET_SIZE = 26;

// Trie Node
struct TrieNode {
    TrieNode* children[ALPHABET_SIZE];
    bool isEndOfWord;
    TrieNode()
    {
        isEndOfWord = false;
        for (int i = 0; i < ALPHABET_SIZE; i++) {
            children[i] = nullptr;
        }
    }
};

// Trie class
class Trie {
public:
    Trie() { root = new TrieNode(); }

    // Insert a word into the Trie
    void insert(const string& word)
    {
        TrieNode* current = root;
        for (char c : word) {
            int index = c - 'a';
            if (!current->children[index]) {
                current->children[index] = new TrieNode();
            }
            current = current->children[index];
        }
        current->isEndOfWord = true;
    }

    // Search for a word in the Trie
    bool search(const string& word)
    {
        TrieNode* current = root;
        for (char c : word) {
            int index = c - 'a';
            if (!current->children[index]) {
                return false;
            }
            current = current->children[index];
        }
        return current->isEndOfWord;
    }

private:
    TrieNode* root;
};

// Count occurrences of a word in a 2D array using Trie
int countWordsIn2DArray(vector<string>& grid,
                        const string& word)
{
    int rows = grid.size();
    int cols = grid[0].size();
    int count = 0;

    Trie trie;
    trie.insert(word);

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            string current = "";
            for (int k = j; k < cols; k++) {
                current += grid[i][k];
                if (trie.search(current)) {
                    count++;
                }
            }
        }
    }

    return count;
}

int main()
{
    vector<string> grid = {
        "abcde",
        "fghij",
        "xyabc",
        "klmno",
    };
    string word = "abc";

    int result = countWordsIn2DArray(grid, word);
    cout << result << endl;

    return 0;
}
Java Python3 C# JavaScript

Output
2

Time Complexity: O(K + R * C * N), where K is the length of the input word, R is the number of rows, C is the number of columns, and N is the maximum length of the substrings generated from the 2D array.

Auxiliary Space complexity: O(K), where K is the length of the input word


Next Article

Similar Reads