Techno Blender
Digitally Yours.

Find number of contiguous Substrings with repeated patterns

0 45


Improve Article

Save Article

Like Article

Improve Article

Save Article

Like Article

Given a string str consisting of digits, the task is to find the number of contiguous substrings such that the substring can be rearranged into a repetition of some string twice.

Examples:

Input:  str=”15512212″
Output: 6
Explanation: Possible 6 substrings are : 

  • “1551” can be rearranged to “1515
  • “155122” can be rearranged to “152152
  • “551221” can be rearranged to “512512
  • “1221” can be rearranged to “1212
  • “55” can be  rearranged to “55
  • “22” can be rearranged to “22

Input:  str=”590025995299005″
Output: 14

Approach: This can be solved with the following idea:

The approach used in the code is based on the observation that if a substring can be rearranged into a repetition of some string twice, then toggling any subset of its digits an even number of times will also result in a substring. This observation is used to efficiently compute the count of substrings using bitwise operations and maps.

Steps to implement the above approach:

  • Initialize a map m with key-value pair (0, 1).
  • Read the input string str.
  • Initialize a variable bb to 0.
  • Initialize a variable ret to 0.
  • For each character c in str, do the following:
    •  Convert the character c to integer v by subtracting the ASCII value of ‘0‘ from it.
    • Update bb by toggling the v-th bit in bb using the bitwise XOR operation.
    • If m[bb] exists, increment ret by m[bb].
    • Increment m[bb] by 1.
  • Print the value of ret, which is the count of required contiguous substrings.

Below is the implementation of the above approach:

C++

#include <bits/stdc++.h>

using namespace std;

  

void solve(string str)

{

  

    

    

    map<int, int> m;

  

    

  

    

    

    

    

    int bb = 0;

  

    

    

    m[0]++;

  

    

    

    

    int ret = 0;

  

    

    for (int i = 0; i < str.size(); i++) {

  

        

        

        int v = str[i] - '0';

  

        

        

        bb ^= (1 << v);

  

        

        

        

        

        

        if (m.count(bb))

            ret += m[bb];

  

        

        

        m[bb]++;

    }

  

    

    

    cout << ret << endl;

}

  

int main()

{

    string str = "15512212";

  

    

    solve(str);

    return 0;

}

Time Complexity: O(n*log(m))
Auxilairy Space: O(m)

Related articles:

Like Article

Save Article


Improve Article

Save Article

Like Article

Improve Article

Save Article

Like Article

Given a string str consisting of digits, the task is to find the number of contiguous substrings such that the substring can be rearranged into a repetition of some string twice.

Examples:

Input:  str=”15512212″
Output: 6
Explanation: Possible 6 substrings are : 

  • “1551” can be rearranged to “1515
  • “155122” can be rearranged to “152152
  • “551221” can be rearranged to “512512
  • “1221” can be rearranged to “1212
  • “55” can be  rearranged to “55
  • “22” can be rearranged to “22

Input:  str=”590025995299005″
Output: 14

Approach: This can be solved with the following idea:

The approach used in the code is based on the observation that if a substring can be rearranged into a repetition of some string twice, then toggling any subset of its digits an even number of times will also result in a substring. This observation is used to efficiently compute the count of substrings using bitwise operations and maps.

Steps to implement the above approach:

  • Initialize a map m with key-value pair (0, 1).
  • Read the input string str.
  • Initialize a variable bb to 0.
  • Initialize a variable ret to 0.
  • For each character c in str, do the following:
    •  Convert the character c to integer v by subtracting the ASCII value of ‘0‘ from it.
    • Update bb by toggling the v-th bit in bb using the bitwise XOR operation.
    • If m[bb] exists, increment ret by m[bb].
    • Increment m[bb] by 1.
  • Print the value of ret, which is the count of required contiguous substrings.

Below is the implementation of the above approach:

C++

#include <bits/stdc++.h>

using namespace std;

  

void solve(string str)

{

  

    

    

    map<int, int> m;

  

    

  

    

    

    

    

    int bb = 0;

  

    

    

    m[0]++;

  

    

    

    

    int ret = 0;

  

    

    for (int i = 0; i < str.size(); i++) {

  

        

        

        int v = str[i] - '0';

  

        

        

        bb ^= (1 << v);

  

        

        

        

        

        

        if (m.count(bb))

            ret += m[bb];

  

        

        

        m[bb]++;

    }

  

    

    

    cout << ret << endl;

}

  

int main()

{

    string str = "15512212";

  

    

    solve(str);

    return 0;

}

Time Complexity: O(n*log(m))
Auxilairy Space: O(m)

Related articles:

Like Article

Save Article

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. 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, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment