Category: Strings

Print all Balanced Brackets Strings that can be formed by replacing wild card ‘?’

import java.io.*;import java.util.*;class Main {            static void print(char ch[])    {        for (Character c : ch) {            System.out.print(c);        }        System.out.println();    }            static boolean check(char ch[])    {                Stack S = new Stack();                        if (ch[0] == ‘)’) {            return false;        }                for (int i = 0; i < ch.length; i++) {                                    if (ch[i] == '(') {                S.add('(');            }                        else {                                                                if (S.size() == 0)                    return false;                                                else                    S.pop();            }        }                        if (S.size() == 0)            return true;                        else            return false;    }            static void count(char ch[], int index)    {                if (index == […]

Continue Reading

Minimum number of adjacent swaps to reverse a String

Given a string s. The task is to minimize the number of adjacent swaps required to reverse the string. Examples:Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to […]

Continue Reading

Find minimum value expression by inserting addition or multiplication operator between digits of given number

Given string str that contains only digits, the task is to return an expression by inserting the ‘+’ or ‘*’ operator between every two digits such that the arithmetic value of the expression is minimized.Example: Input: str = “322”Output: “3+2*2”Explanation: The value of above expression is 7 which is minimum possible over the other expressions “3+2+2”, […]

Continue Reading

Minimum adjacent swaps required to get Kth smallest number greater than given number

#include using namespace std;  void minSwapsKthLargest(string str, int k){        string res = str;              for (int i = 0; i < k; i++) {        next_permutation(            str.begin(), str.end());    }          int swap_count = 0;              for (int i = 0; i < res.length(); i++) {                  if (res[i] != str[i]) {            int start = i + 1;                          while (res[i] != str[start]) {                                  start++;            }            while (i != start) {                swap(str[start], str[start - 1]);                                                  start--;                swap_count++;            }        }    }          cout

Continue Reading

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

Karatsuba Algorithm for fast Multiplication of Large Decimal Numbers represented as Strings

  #include using namespace std;  string findSum(string str1, string str2){            if (str1.length() > str2.length())        swap(str1, str2);          string str = “”;          int n1 = str1.length();    int n2 = str2.length();          reverse(str1.begin(), str1.end());    reverse(str2.begin(), str2.end());      int carry = 0;    for (int i = 0; i < n1; i++) {                          int sum            = ((str1[i] - '0')               + (str2[i] - '0')               + carry);        str.push_back(sum % 10 + '0');                  carry = sum / 10;    }          for (int i = n1; […]

Continue Reading