Category: Backtracking

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

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

Find direction of path followed from root by a linked list in a Binary Tree

#include using namespace std;  struct ListNode {    int data;    ListNode* next;    ListNode(int data)    {        this->data = data;        this->next = NULL;    }};  struct TreeNode {    TreeNode* left;    TreeNode* right;    int val;      TreeNode(int x)        : left(NULL), right(NULL), val(x)    {    }};  ListNode* makeList(int arr[], int n){    ListNode* h = NULL;    ListNode* root;    for (int i = 0; i < n; i++) {        int data = arr[i];        ListNode* node = new ListNode(data);          if (h == NULL) {            h = node;            root = h;        }        else {            root->next […]

Continue Reading

Count of valid arrays of size P with elements in range [1, N] having duplicates at least M distance apart

#include using namespace std;  int calculate(int position, int used, int unused, int P,              int M, vector& dp){        if (position == P) {                        return unused == 0 ? 1 : 0;    }              if (dp[position][used][unused] != -1)        return dp[position][used][unused];          int result = 0;              if (unused > 0) {                        result += calculate(position + 1, used + 1,                            unused – 1, P, M, dp)                  * unused;    }              if (used > M) {                        result += […]

Continue Reading

Least count of words required to construct a target String

import java.io.*;import java.util.*;  class Sol {              int[][] countMap;      int cnt = Integer.MAX_VALUE;              public int minStickers(String[] stickers                           , String target)    {                if (target == null)            return -1;          if (target.length() == 0)            return 0;          if (stickers == null || stickers.length == 0)            return -1;          int m = stickers.length;        countMap = new int[m][26];                  for (int i = 0; i < stickers.length; i++) {            String s = stickers[i];            for (char c : s.toCharArray()) {                countMap[i]++;            }        }                          count(0, 0, new […]

Continue Reading

Print all Hamiltonian Cycles in an Undirected Graph

  import java.util.ArrayList;class GFG {                  boolean isSafe(int v, int graph[][],                   ArrayList path,                   int pos)    {                                if (graph[path.get(pos – 1)][v]            == 0)            return false;                          for (int i = 0; i < pos; i++)            if (path.get(i) == v)                return false;                          return true;    }              boolean hasCycle;              void hamCycle(int graph[][])    {                        hasCycle = false;                  ArrayList path            = new ArrayList();        path.add(0);                          boolean[] visited            = new boolean[graph.length];          for (int i = 0;             i < visited.length; i++)            visited[i] = false;          visited[0] = true;                          FindHamCycle(graph, 1, path,                     visited);          if (!hasCycle) {                                                  System.out.println(                "No Hamiltonian […]

Continue Reading

Convert a Mobile Numeric Keypad sequence to equivalent sentence

import java.io.*;import java.lang.*;import java.util.*;public class GFG {                static void printSentence(String S)    {                String nums[]            = { “”,    “”,    “ABC”,  “DEF”, “GHI”,                “JKL”, “MNO”, “PQRS”, “TUV”, “WXYZ” };        char str[] = S.toCharArray();                int i = 0;        while (i < str.length) {                                                if (str[i] == '.') {                i++;                continue;            }                                    int count = 0;                                    while (i + 1 < str.length                   && str[i] == str[i + 1]) {                                                if (count == 2                    && ((str[i] >= […]

Continue Reading