Category: Suffix

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

Check if count of substrings in S with string S1 as prefix and S2 as suffix is equal to that with S2 as prefix and S1 as suffix

#include using namespace std;  int countSubstrings(string S, string S1,                    string S2){        int N = S.length();    int N1 = S1.length();    int N2 = S2.length();              int count = 0, totalcount = 0;          for (int i = 0; i < N; i++) {                  string prefix = S.substr(i, N1);                  string suffix = S.substr(i, N2);                  if (S1 == prefix)            count++;                  if (S2 == suffix)            totalcount += count;    }          return totalcount;}  void checkSubstrings(string S, string S1,                     string S2){          int […]

Continue Reading

Calculate absolute difference between minimum and maximum sum of pairs in an array

#include using namespace std; int GetDiff(int A[], int N) {               int SuffMaxArr[N];          SuffMaxArr[N – 1] = A[N – 1];          for (int i = N – 2; i >= 0; –i) {                           SuffMaxArr[i] = max(SuffMaxArr[i + 1],                             A[i + 1]);     }          int MaximumSum = INT_MIN;          for (int i […]

Continue Reading

Queries to calculate GCD of an array after multiplying first or last K elements by X

#include using namespace std; void findGCDQueries(int arr[], int N,                     int Queries[][3],                     int Q) {               int prefix[N], suffix[N];     prefix[0] = arr[0];     suffix[N – 1] = arr[N – 1];          for (int i = 1; i < N; i++) {         prefix[i] = __gcd(prefix[i - 1],                           arr[i]);     }          for (int i = N - 2; […]

Continue Reading