Category: TrueGeek

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

Introduction to Universal Hashing in Data Structure

Hashing is a great practical tool, with an interesting and subtle theory too. In addition to its use as a dictionary data structure, hashing also comes up in many different areas, including cryptography and complexity theory. This article discusses an important notion: Universal Hashing (also known as universal hash function families).Universal Hashing refers to selecting a […]

Continue Reading

Minimize count of steps to make all Matrix elements as 0 by replacing neighbours of K with 0

#include using namespace std;  void traverse(int M, int N,              vector& matrix,              int row, int col){          if (row >= 0 && row < M        && col >= 0 && col < N) {        if (matrix[row][col] == 0) {            return;        }                          matrix[row][col] = 0;                  traverse(M, N, matrix, row, col - 1);                  traverse(M, N, matrix, row, col + 1);                  traverse(M, N, matrix, row - 1, col);                  traverse(M, N, matrix, row + […]

Continue Reading

Maximum length of string formed by concatenation having even frequency of each character

  import java.io.*;import java.util.*;  public class index {    static int max = 0;    static String ans1 = “”;          static void calculate(String ans)    {          int dp[] = new int[26];        for (int i = 0; i < ans.length(); ++i) {                                      dp[ans.charAt(i) - 'A']++;        }                  for (int i = 0; i < dp.length; ++i) {            if (dp[i] % 2 == 1) {                return;            }        }        if (max < ans.length()) {                                      max = ans.length();            ans1 = ans;        }    }                  static […]

Continue Reading