Wednesday, 1 Dec 2021
Category: frequency-counting

Given a string S of N characters, the task is to calculate the total number of non-empty substrings such that at most one character occurs an odd number of times.Example: Input: S = “aba”Output: 4Explanation: The valid substrings are “a”, “b”, “a”, and “aba”. Therefore, the total number of required substrings are 4.Input: “aabb”Output: 9Explanation: The […]

Given an array A[]of N strings of lowercase characters, the task is to find maximum number of strings, such that one character has majority i.e. the occurrence of one character in all of the strings is greater than all the other characters combined.Examples:Input: A[] = {“aba”, “abcde”, “aba”}Output: 2Explanation: On choosing {“aba”, “aba”}, the occurrence […]

#include using namespace std;  int minimumPalindromicStrings(string S){        int N = S.length();                      vector freq(26);    for (int i = 0; i < N; i++) {        freq[S[i] - 'a']++;    }          vector oddFreqCharacters;                                for (int i = 0; i < 26; i++) {          if (freq[i] & 1) {              oddFreqCharacters.push_back(i);            freq[i]--;        }          freq[i] /= 2;    }          vector ans;              if (oddFreqCharacters.empty()) {                          string left = "";          for (int i = 0; i < 26; i++) {            for (int j = […]

function maxAmount(n, k, arr)        {                                             let A = new Array(1000001).fill(0);            for (let i = 0; i < n; i++)            {                A[arr[i]]++;            }            let j = 0;                                                 for (let i = 0; i < 1000001; i++) {                while (A[i] != 0) {                    arr[j++] = i;                    A[i]--;                }            }                        let ans = 0;            let mod = 1e9 + 7;            let i = n - 1;            j = n - 2;                                    while (k > 0 && […]

public class GFG {                  static String checkIfPossible(        int N, String[] arr, String T)    {                        int[] freqS = new int[256];                          int[] freqT = new int[256];                          for (char ch : T.toCharArray()) {            freqT[ch – ‘a’]++;        }                  for (int i = 0; i < N; i++) {                                      for (char ch : arr[i].toCharArray()) {                freqS[ch - 'a']++;            }        }          for (int i = 0; i < 256; i++) {                                      if (freqT[i] == 0                && freqS[i] […]