Category: frequency-counting

Count of substrings with the frequency of at most one character as Odd

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 […]

Continue Reading

Maximum count of Strings to be selected such that there is a majority character

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 […]

Continue Reading

Form minimum number of Palindromic Strings from a given string

  #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 = […]

Continue Reading

Maximize the profit after selling the tickets | Set 2 (For elements in range [1, 10^6])

                                   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 && […]

Continue Reading

Convert given Strings into T by replacing characters in between strings any number of times

  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] […]

Continue Reading