Category: Trie

Lexicographically smallest string with given string as prefix

Given an array arr[] consisting of N strings and a string S if size M, the task is to find the lexicographically smallest string consisting of the string S as the prefix. If there doesn’t exist any string starting with prefix S then print “-1”.Examples:Input: arr[] = {“apple”, “appe”, “apl”, “aapl”, “appax”}, S = “app”Output: […]

Continue Reading

Search a string in the dictionary with a given prefix and suffix for Q queries

  #include using namespace std;  struct Trie {    Trie* arr[27] = { NULL };              int idx;};  Trie* root = new Trie();  void insert(string word, int i){            Trie* temp = root;              for (char ch : word) {                          if (temp->arr[ch – ‘a’] == NULL) {            Trie* t = new Trie();            temp->arr[ch – ‘a’] = t;        }          temp = temp->arr[ch – ‘a’];        temp->idx = i;    }}  int search(string word){    Trie* temp = root;              for (char ch […]

Continue Reading