Count pairs of non overlapping Substrings of size K which form a palindromic String – GeeksforGeeks

Given a string S of length l, the task is to count the number of pairs of two non-overlapping substrings of length K of the string S, such that on joining the two non-overlapping substrings the resultant string is a palindrome.

Examples:

Input: S = “abcdbadc”, K = 2
Output:
Explanation: Possible substrings of length 2: ab, bc, cd, db, ba, ad, dc

  • (ab, ba) => abba
  • (cd, dc) => cddc

Input: S = “efjadcajfe”, K = 3
Output: 2
Explanation: Possible substrings of length 3: efj, fja, jad, adc, dca, caj, ajf, jfe

  • (efj, jfe) => efjjfe
  • (fja, ajf) => fjaajf

Approach: To solve the problem follow the below intuition:

A palindrome string is a string that if reversed will form the same original string. Also if you observe a palindromic string will always be even in length, hence it can be divided into two parts which will actually be mirror images of each other. 

  • This same idea of mirror images we apply to our approach. Since we have to find all substrings two nested loops will always be required.
  • The outer loop will generate a substring of length K then the inner loop will again generate a substring of length K which will not overlap with the substring generated by the outer loop. 
  • Before starting the second loop we will reverse the outer string. Then we generate more non-overlapping substrings and check if any substring is equal to the outer reversed substring. 
  • If there is a match then definitely if we concatenate the inner string and original outer string then the newly formed string will be a palindromic string and the count for such strings will be incremented, else we continue our search.

Follow the below steps to solve the problem:

  • Declare a counter variable to store the count.
    • The outer loop starts from i = 0 and goes till the length of string l – K and in each iteration loop variable. 
  • A substring of length K is generated before the second loop begins and the reverse of the substring is stored in s1.
    • The inner loop runs from i + K to l – K( the loop starts from i + k to avoid overlapping of substrings).
  • A second substring of length K, s2 is generated which does not overlap with the first substring. 
  • Now check if the first string is equal to the second string.
  • If yes then increment the counter else continue the loop
    • exit outer loop  
    • exit inner loop 
  • Return the count

Below is the implementation of the above approach : 

C++

#include <bits/stdc++.h>

using namespace std;

  

string reverse(string s)

{

    string ans = "";

    int l = s.length();

    for (int i = l - 1; i >= 0; i--) {

        ans.push_back(s[i]);

    }

    return ans;

}

  

int myfunction(string s, int k)

{

  

    

    int count = 0;

    for (int i = 0; i <= s.length() - k; i++) {

        string s1 = s.substr(i, k);

        s1 = reverse(s1);

        string s2 = "";

  

        for (int j = i + k; j <= s.length() - k; j++) {

            string s2 = s.substr(j, k);

  

            

            

            

            if (s1 == s2) {

                count++;

            }

        }

    }

    return count;

}

  

int main()

{

    string s = "abcdbadc";

    int ans = myfunction(s, 2);

  

    

    cout << "Number of such pairs is : " << ans;

    return 0;

}

Output
Number of such pairs is : 2

Time Complexity: O ( (l – k) * (k + k) * (l – k) *k ) where l is the length of the string and k is the length of the substring.
Auxiliary Space: O ( 2*k ) where k is the length of the substring

Alternate Approach: To solve the problem follow the below idea:

This approach is more straightforward and simpler than the previous one. In this approach, we simply generate the outer string and the inner string but we won’t reverse the outer string. Instead, we concatenate the two strings and check if the newly formed string is a palindrome or not.

Follow the below steps to solve the problem: 

  • Declare a counter variable to store the count.
    • The outer loop starts from i = 0 and goes till the length of string l – K and in each iteration loop variable.
  • A substring of length K is generated before the second loop begins and stored in s1.
    • The inner loop runs from i + K to l – K( the loop starts from i + K to avoid overlapping of substrings).
  • A second substring of length K, s2 is generated which does not overlap with the first substring. 
  • Now concatenate the two substrings and check if the new string is a palindrome or not 
  • If yes then increment the counter else continue the loop
    • exit outer loop
    • exit inner loop
  • Return the count

  Below is the implementation for the above approach : 

C++

#include <bits/stdc++.h>

using namespace std;

  

bool isPaldinrome(string s)

{

    int l = s.length();

    for (int i = 0; i < l; i++) {

        if (s[i] != s[l - 1 - i])

            return false;

    }

    return true;

}

  

int myfunction(string s, int k)

{

  

    

    int count = 0;

  

    for (int i = 0; i <= s.length() - k; i++) {

  

        string s1 = s.substr(i, k);

  

        for (int j = i + k; j <= s.length() - k; j++) {

            string s2 = s.substr(j, k);

  

            

            string s3 = s1 + s2;

  

            

            

            

            if (isPaldinrome(s3)) {

                count++;

            }

        }

    }

    return count;

}

  

int main()

{

    string s = "abcdbadc";

    int ans = myfunction(s, 2);

  

    

    cout << "Number of such pairs is : " << ans;

    return 0;

}

Output
Number of such pairs is : 2

Time Complexity: O((l – k) * k * (l – k) *k + 2k) where l is the length of the string and k is the length of the substring.
Auxiliary Space: O(2*k) where k is the length of the substring

 

Stay connected with us on social media platform for instant update click here to join our  Twitter, & Facebook We are now on Telegram. Click here to join our channel (@TechiUpdate) and stay updated with the latest Technology headlines. For all the latest Technology News Click Here 

Read original article here

Denial of responsibility! FineRadar is an automatic aggregator around the global media. All the content are available free on Internet. We have just arranged it in one platform for educational purpose only. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials on our website, please contact us by email – abuse@fineradar.com. The content will be deleted within 24 hours.
countfineradar updateformFree Fire Redeem Codesgadget updateGeeksforGeeksLatest tech newsoverlappingPairsPalindromicsizestringSubstringsTech Headlinestech newsTech News UpdatesTechnologyTechnology News
Comments (0)
Add Comment