Wednesday, 1 Dec 2021
Category: substring

Given a string of lowercase letters S a character c. The task is to find minimum K such that every substring of length K contains the given character c. If there is no such K possible, return -1.Examples:Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self […]

Given a string S consisting of N characters, the task is to check if this string has an reversible equal substring from the start and the end. If yes, print True and then the longest substring present following the given conditions, otherwise print False.Example:Input: S = “abca”Output: TrueaExplanation:The substring “a”  is only the longest substring that […]

#include using namespace std;  void merge(vector& v, int left,           int mid, int right, int& inversions){    vector temp(right – left + 1);      int i = left;    int j = mid + 1;    int k = 0;    int cnt = 0;      while (i

Minimize hamming distance in Binary String by setting only one K size substring bits Given two binary strings S and T of length N and a positive integer K. Initially, all characters of T are ‘0’. The task is to find the minimum Hamming distance after choosing a substring of size K and making all […]

Check if a numeric string can be split into substrings having difference between consecutive numbers equal to KGiven a numeric string S consisting of N digits and a positive integer K, the task is to check if the given string can be split into more than one substrings with difference between consecutive substrings equalt to […]

import java.io.*;import java.util.*;  class GFG {                  static int count(String S, int N)    {                  char ch[] = { ‘u’, ‘o’, ‘i’, ‘e’, ‘a’ };                  int j = 0;                  int res = 0;                          int count = 0;                  HashSet mp = new HashSet();                  for (int i = 0; i < N; ++i) {                          if (S.charAt(i) == ch[j]) {                                  ++count;                                  mp.add(S.charAt(i));                                  if (mp.size() == 5) {                    res = Math.max(res, count);                }            }                                      else if (j + […]

#include using namespace std;void update(int ind, vector& segTree,            int n){        ind += n;        segTree[ind]++;    for (; ind > 1; ind >>= 1) {                segTree[ind >> 1] = segTree[ind]                            + segTree[ind ^ 1];    }}int query(int low, int high,          vector& segTree, int n){            low += n;    high += n;    int ans = 0;    while (low < high) {                        if (low % 2) {            ans += segTree[low];            low++;        }        if (high % 2) {            high--;            ans += […]

Count occurrences of substring X before every occurrence of substring Y in a given stringGiven three strings S, X, and Y consisting of N, A, and B characters respectively, the task is to find the number of occurrences of the substring X before every occurrence of the substring Y in the given string S.Examples:Input S […]

Number of substrings having an equal number of lowercase and uppercase lettersGiven string S consisting of lowercase and uppercase letters, the task is to find the number of substrings having the equal number of lowercase and uppercase letters.Examples:Input: S = “gEEk”Output: 3Explanation:Following are the substrings having the equal number of lowercase and uppercase letters:“gE”“gEEk”“Ek”Therefore, the […]

Minimize length of a string by removing pairs of consecutive increasing or decreasing digitsGiven a numeric string S consisting of N digits, the task is to find the minimum length of the string that can be formed by repeatedly removing pairs of adjacent consecutive characters arranged in either increasing or decreasing order.Examples:Input: S = “12213”Output: […]