Category: palindrome

Check if the string has a reversible equal substring at the ends

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

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

Check if a string can be made palindromic by swapping pairs of characters from indices having unequal characters in a Binary String

  import java.io.*;import java.util.HashMap;import java.util.HashSet;import java.util.Map;  class GFG {              public static boolean    canBecomePalindromeUtil(String S,                            String B)    {                        HashSet set = new HashSet();                  for (int i = 0; i < S.length(); i++) {                          set.add(S.charAt(i));        }                          HashMap map            = new HashMap();                  for (int i = 0; i < S.length(); i++) {            Integer k = map.get(S.charAt(i));            map.put(S.charAt(i),                    (k == null) ? 1 : k + 1);        }          boolean flag = false;                  if (S.length() % 2 == 1) […]

Continue Reading

Lexicographically smallest Palindromic Path in a Binary Tree

   #include using namespace std;    struct Node {     char data;     Node *left, *right; };    Node* newNode(char data) {     Node* temp = new Node();     temp->data = data;     temp->left = temp->right = NULL;     return temp; }    bool checkPalindrome(string s) {     int low = 0, high = (int)s.size() – 1;        while (low < high) […]

Continue Reading