Category: prefix

Minimum characters to be replaced in Ternary string to remove all palindromic substrings for Q queries

  #include #define SIZE 100005using namespace std;  void preprocess(string& s, string& t,                int prefix[][SIZE],                int n, int i){        prefix[i][0] = (s[0] != t[0]);      for (int j = 1; j < n; j++) {                                  prefix[i][j]            = prefix[i][j - 1]              + (s[j] != t[j % 3]);    }    return;}  void minChangesNonPalindrome(    string str, int N, int Q,    vector queries){          int prefix[6][SIZE];                  vector sequences        = { "012", "021", "102",            "120", "201", "210" };      for (int i = 0; […]

Continue Reading

Count of non decreasing Arrays with ith element in range [A[i], B[i]]

#include using namespace std;  int totalValidArrays(int a[], int b[],                     int N){        int dp[N + 1][b[N – 1] + 1];          int pref[N + 1][b[N – 1] + 1];          memset(dp, 0, sizeof(dp)),        memset(pref, 0, sizeof(pref));          dp[0][0] = 1;          for (int i = 0; i

Continue Reading

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

Counting inversions in an subarrays

Given an array arr[], the goal is to count the number of inversions in all the sub-arrays. An inversion is a pair of indices i and j such that i > j and arr[i] < arr[j]. A sub-array from index x to y ( x= j.Naive Approach: A naive approach is to generate all possible […]

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

Queries to calculate Bitwise AND of an array with updates

  #include using namespace std;  int prefixCount[32][10000];  void findPrefixCount(vector arr,                     int size){        for (int i = 0; i < 32; i++) {                          prefixCount[i][0]            = ((arr[0] >> i) & 1);                          for (int j = 1; j < size; j++) {                          prefixCount[i][j]                = ((arr[j] >> i) & 1);            prefixCount[i][j]                += prefixCount[i][j – 1];        }    }}  void arrayBitwiseAND(int size){        int result = 0;          for (int i = 0; i < 32; i++) {                          int temp = […]

Continue Reading