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++ code of the above approach: #include <bits/stdc++.h> using namespace std;
// Defining the solve function void solve(string str)
{ // Initializing a map to keep track
// of the number of substrings found
map< int , int > m;
// Reading the input string
// Initializing a variable to keep
// track of the number of times each
// digit appears in the current
// substring
int bb = 0;
// Inserting an initial key-value
// pair of (0, 1) into the map
m[0]++;
// Initializing a variable to keep
// track of the count of contiguous
// substrings found so far
int ret = 0;
// Iterating through the input string
for ( int i = 0; i < str.size(); i++) {
// Converting the current character
// to an integer
int v = str[i] - '0' ;
// Toggling the v-th bit in bb
// using the bitwise XOR operation
bb ^= (1 << v);
// If the current substring is a
// repetition of some string
// twice, increment ret by the
// number of occurrences of the
// substring in the map
if (m.count(bb))
ret += m[bb];
// Incrementing the count of the
// current substring in the map
m[bb]++;
}
// Printing the count of required
// substrings found
cout << ret << endl;
} // Driver code int main()
{ string str = "15512212" ;
// Function call
solve(str);
return 0;
} |
/*package whatever //do not write package name here */ import java.io.*;
import java.util.*;
class GFG {
// Defining the solve function
public static void solve(String str)
{
// Initializing a map to keep track
// of the number of substrings found
Map<Integer, Integer> m
= new HashMap<Integer, Integer>();
// Initializing a variable to keep
// track of the number of times each
// digit appears in the current
// substring
int bb = 0 ;
// Inserting an initial key-value
// pair of (0, 1) into the map
m.put( 0 , 1 );
// Initializing a variable to keep
// track of the count of contiguous
// substrings found so far
int ret = 0 ;
// Iterating through the input string
for ( int i = 0 ; i < str.length(); i++) {
// Converting the current character
// to an integer
int v = str.charAt(i) - '0' ;
// Toggling the v-th bit in bb
// using the bitwise XOR operation
bb ^= ( 1 << v);
// If the current substring is a
// repetition of some string
// twice, increment ret by the
// number of occurrences of the
// substring in the map
if (m.containsKey(bb))
ret += m.get(bb);
// Incrementing the count of the
// current substring in the map
m.put(bb, m.getOrDefault(bb, 0 ) + 1 );
}
// Printing the count of required
// substrings found
System.out.println(ret);
}
// Driver code
public static void main(String[] args)
{
String str = "15512212" ;
// Function call
solve(str);
}
// This Code is Contributed by Vikas Bishnoi
} |
# Defining the solve function def solve(s: str ) - > None :
# Initializing a dictionary to keep track
# of the number of substrings found
m = {}
# Initializing a variable to keep
# track of the number of times each
# digit appears in the current
# substring
bb = 0
# Inserting an initial key-value
# pair of (0, 1) into the dictionary
m[ 0 ] = 1
# Initializing a variable to keep
# track of the count of contiguous
# substrings found so far
ret = 0
# Iterating through the input string
for i in range ( len (s)):
# Converting the current character
# to an integer
v = int (s[i])
# Toggling the v-th bit in bb
# using the bitwise XOR operation
bb ^ = ( 1 << v)
# If the current substring is a
# repetition of some string
# twice, increment ret by the
# number of occurrences of the
# substring in the dictionary
if bb in m:
ret + = m[bb]
# Incrementing the count of the
# current substring in the dictionary
if bb not in m:
m[bb] = 0
m[bb] + = 1
# Printing the count of required
# substrings found
print (ret)
# Driver code if __name__ = = '__main__' :
s = "15512212"
# Function call
solve(s)
# akashish__ |
using System;
using System.Collections.Generic;
class GFG
{ // Defining the solve function
static void Solve( string str)
{
// Initializing a dictionary to keep track
// of the number of substrings found
Dictionary< int , int > m = new Dictionary< int , int >();
// Initializing a variable to keep
// track of the number of times each
// digit appears in the current
// substring
int bb = 0;
// Inserting an initial key-value
// pair of (0, 1) into the dictionary
m[0] = 1;
// Initializing a variable to keep
// track of the count of contiguous
// substrings found so far
int ret = 0;
// Iterating through the input string
for ( int i = 0; i < str.Length; i++)
{
// Converting the current character
// to an integer
int v = str[i] - '0' ;
// Toggling the v-th bit in bb
// using the bitwise XOR operation
bb ^= (1 << v);
// If the current substring is a
// repetition of some string
// twice, increment ret by the
// number of occurrences of the
// substring in the dictionary
if (m.ContainsKey(bb))
ret += m[bb];
// Incrementing the count of the
// current substring in the dictionary
if (m.ContainsKey(bb))
m[bb]++;
else
m.Add(bb, 1);
}
// Printing the count of required
// substrings found
Console.WriteLine(ret);
}
// Driver code
static void Main( string [] args)
{
string str = "15512212" ;
// Function call
Solve(str);
}
} //This code is contributed by Aditi Tyagi |
function solve(str) {
// Initializing a map to keep track
// of the number of substrings found
const m = new Map();
// Initializing a variable to keep
// track of the number of times each
// digit appears in the current
// substring
let bb = 0;
// Inserting an initial key-value
// pair of (0, 1) into the map
m.set(0, 1);
// Initializing a variable to keep
// track of the count of contiguous
// substrings found so far
let ret = 0;
// Iterating through the input string
for (let i = 0; i < str.length; i++) {
// Converting the current character
// to an integer
const v = parseInt(str[i]);
// Toggling the v-th bit in bb
// using the bitwise XOR operation
bb ^= (1 << v);
// If the current substring is a
// repetition of some string
// twice, increment ret by the
// number of occurrences of the
// substring in the map
if (m.has(bb)) {
ret += m.get(bb);
}
// Incrementing the count of the
// current substring in the map
m.set(bb, (m.get(bb) || 0) + 1);
}
// Printing the count of required
// substrings found
console.log(ret);
} // Driver code const str = "15512212" ;
// Function call solve(str); //This code is contributed by Tushar Rokade |
6
Time Complexity: O(n*log(m))
Auxilairy Space: O(m)
Related articles: