Trends

Count all palindromic Substrings for each character in a given String

Trending 1 year ago
beritaja.com

Improve Article

Save Article

Improve Article

Save Article

Given a drawstring S of length n, for each character S[i], nan task is to find nan number of palindromic substrings of magnitude K specified that nary substring should incorporate S[i], the task is to return an array A of magnitude n, wherever A[i] is nan count of palindromic substrings of magnitude K which does not see nan characteristic S[i].

Examples:

Input: S = “aababba”, K = 2
Output : A[] = [1 1 2 2 1 1 2]
Explanation: A[0] = 1 => removing char s[0] = ‘a’ only 1 palindromic substring of magnitude 2 is imaginable i.e. “bb”
A[1] = 1 => removing char s[1] = ‘a’ only 1 palindromic substring of magnitude 2 is imaginable i.e. “bb” 
A[2] = 2 => removing char s[2] = ‘b’ 2 palindromic substring of magnitude 2 are imaginable i.e. “aa” and “bb”
A[3] = 2 => removing char s[3] = ‘a’ 2 palindromic substring of magnitude 2 are imaginable i.e. “aa” and “bb” 
A[4] = 1 => removing char s[4] = ‘b’ only 1 palindromic substring of magnitude 2 is imaginable i.e. “aa” 
A[5] = 1 => removing char s[5] = ‘b’ only 1 palindromic substring of magnitude 2 is imaginable i.e. “aa” 
A[6] = 2 => removing char s[6] = ‘a’ 2 palindromic substring of magnitude 2 are imaginable i.e. “aa” and “bb” 

Input: S = “abbab”, K = 2
Output: A[] = [1 0 0 1 1]
Explanation: A[0] = 1 => removing char s[0] = ‘a’ only 1 palindromic substring of magnitude 2 is imaginable i.e. “bb”
A[1] = 1 => removing char s[1] = ‘b’ nary palindromic substring of magnitude 2 is possible
A[2] = 2 => removing char s[2] = ‘b’ nary palindromic substring of magnitude 2 is possible
A[3] = 2 => removing char s[3] = ‘a’ only 1 palindromic substring of magnitude 2 is imaginable i.e. “bb”
A[4] = 1 => removing char s[4] = ‘b’ only 1 palindromic substring of magnitude 2 is imaginable i.e. “bb”

Approach: To lick nan problem travel nan beneath steps:

  • For characteristic S[i] of nan string, we request to find palindromic substrings of magnitude K specified that nan substrings don’t see S[i]. This attack requires 1 loop to iterate nan drawstring and 2 abstracted soul for loops because we don’t person to see nan characteristic S[i] truthful we request to skip nan full scope for which S[i] is included successful immoderate substring.
  • Run a loop to iterate nan drawstring from one = 0 to one < magnitude of string. 
  • The first soul loop will tally from j = 0 to j ≤ one – K, past nan past substring earlier S[i] will commencement from i – K and extremity at i – 1 which won’t see S[i].
  • The 2nd soul loop starts from j = one + 1 to n – 1.
  • For each substring, cheque if it is simply a palindrome, past increment nan antagonistic for that characteristic S[i].

Below are nan steps for nan supra approach:

  • Create an array A[] to shop nan count of nan number of palindromic substrings for S[i].
  • Run a loop to iterate nan drawstring from i = 0 to i < S.length.
  • Initialize a counter-variable count = 0.
  • The first soul loop runs from j = 0 to j = one – K and generates a substring from j of magnitude K to cheque if it is simply a palindrome, incrementing nan counter.
  • The 2nd soul loop runs from j = i+1 to j = l – 1 and generates a substring from j of magnitude k to cheque if it is simply a palindrome, and increment nan counter.
  • When some soul loop ends, update A[i] = count
  • Return nan array A[].

Below is nan implementation for nan supra approach:

C++

// C++ programme for nan supra approach #include <bits/stdc++.h> using namespace std; // usability to cheque if a drawstring is palindrome bool isPalin(string s, int l) { for (int one = 0; one < l; i++) { if (s[i] != s[l - one - 1]) return false; } return true; } // usability to count nan number of paldinromic // substrings for each characteristic of a string void findCount(string s, int l, int k, int A[]) { for (int one = 0; one < s.length(); i++) { int count = 0; // loop 1 for (int j = 0; j <= one - k; j++) { drawstring sub = s.substr(j, k); if (isPalin(sub, k)) count++; } // loop 2 for (int j = one + 1; j < l; j++) { drawstring sub = s.substr(j, k); if (isPalin(sub, k)) count++; } A[i] = count; } } // Driver fucntion int main() { drawstring s = "aababba"; // fixed string int l = 7; // magnitude of string int k = 2; // magnitude of substring // array to shop nan count int A[s.length()]; // usability call findCount(s, l, k, A); cout << "Count of palindromic substrings for " << "each characteristic of drawstring s is: [ "; for (int one = 0; one < s.length(); i++) cout << A[i] << " "; cout << "]" << '\n'; }

Output

Count of palindromic substrings for each characteristic of drawstring s is: [ 1 1 2 2 1 1 2 ]

Time Complexity: O(l2) wherever l is nan magnitude of nan string
Auxiliary Space: O(l) wherever l is nan magnitude of nan Resultant Array

Editor: Naga



Read other contents from Beritaja.com at
More Source
close