Maximize count of distinct strings generated by replacing similar adjacent digits having sum K with K
Last Updated :
25 May, 2021
Given a numeric string S of length N and a digit K, the task is to find the maximum number of distinct strings having a maximum occurrence of K in it formed by replacing any two adjacent digits of S with K if their sum is K.
Examples:
Input: S = “313”, K = 4
Output: 2
Explanation: Possible strings that can be generated are:
- Replacing S[0] and S[1] with K modifies S to “43”.
- Replacing S[1] and S[2] with K modifies S to “34”.
Input: S = “12352”, K = 7
Output: 1
Explanation: Only string possible is by replacing S[3] and S[4] with K i.e., S = “1237”.
Approach: The given problem can be solved using the observation that for some substring of S, there are two types of possibility to contribute to the result i.e., it can be a sequence of “xy” having an equal number of x and y i.e., “xyxy…xyxy” or it can be a sequence of xy having one extra x i.e., “xyxy…xyxyx” where x + y = K.
- Case 1: String of the form “xyxy…xyxy” can be converted to the string “KK…KK” where K occurs (length of substring)/2 times. So, the number of distinct strings that will be formed is 1.
- Case 2: String of the form “xyxy…xyxyx” has one extra ‘x’ and can be converted into the string “KK…KKx” where K occurs (length of substring-1)/2 times. Let this value is M. Upon observation, it can be seen that there will be (M + 1) possible positions for x in the converted string.
Follow the below steps to solve the problem:
- Initialize the variables ans with 1, flag with 0, and start_index with -1 where ans will store the answer up to index i and start_index will be used to store the starting index of each substring from where the desired sequence started.
- Traverse the string S and do the following:
- If the sum of any adjacent digits S[i] and S[i + 1] is K and flag is set to false, set flag to true and start_index to i.
- If flag is true and S[i] and S[i + 1] is not K, set flag to false and also, if (start_index – i + 1) is odd, multiply ans by (start_index – i + 1 – 1)/2 + 1 as stated in Case 2.
- After traversing the string S, if flag is true and (N – start_index) is odd, multiply ans by (N – start_index – 1)/2 + 1.
- After completing the above steps, print ans as the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void countStrings(string s, int k)
{
int ans = 1;
int len = s.size();
int flag = 0;
int start_ind;
for ( int i = 0; i < len - 1; i++) {
if (s[i] - '0' + s[i + 1] - '0'
== k
&& flag == 0) {
flag = 1;
start_ind = i;
}
if (flag == 1
&& s[i] - '0'
+ s[i + 1] - '0'
!= k) {
flag = 0;
if ((i - start_ind + 1) % 2 != 0)
ans *= (i - start_ind + 1 - 1)
/ 2
+ 1;
}
}
if (flag == 1
&& (len - start_ind) % 2 != 0)
ans *= (len - start_ind) / 2 + 1;
cout << ans;
}
int main()
{
string S = "313" ;
int K = 4;
countStrings(S, K);
}
|
Java
import java.util.*;
class GFG{
static void countStrings(String s, int k)
{
int ans = 1 ;
int len = s.length();
int flag = 0 ;
int start_ind = 0 ;
for ( int i = 0 ; i < len - 1 ; i++)
{
if (s.charAt(i) - '0' +
s.charAt(i + 1 ) - '0' == k && flag == 0 )
{
flag = 1 ;
start_ind = i;
}
if (flag == 1 && s.charAt(i) - '0' +
s.charAt(i + 1 ) - '0' != k)
{
flag = 0 ;
if ((i - start_ind + 1 ) % 2 != 0 )
ans *= (i - start_ind + 1 - 1 ) / 2 + 1 ;
}
}
if (flag == 1 && (len - start_ind) % 2 != 0 )
ans *= (len - start_ind) / 2 + 1 ;
System.out.println(ans);
}
public static void main(String[] args)
{
String S = "313" ;
int K = 4 ;
countStrings(S, K);
}
}
|
Python3
def countStrings(s, k) :
ans = 1
lenn = len (s)
flag = 0
for i in range (lenn - 1 ):
if ( ord (s[i]) - ord ( '0' ) + ord (s[i + 1 ]) - ord ( '0' )
= = k
and flag = = 0 ) :
flag = 1
start_ind = i
if (flag = = 1
and ord (s[i]) - ord ( '0' )
+ ord (s[i + 1 ]) - ord ( '0' )
! = k) :
flag = 0
if ((i - start_ind + 1 ) % 2 ! = 0 ) :
ans * = (i - start_ind + 1 - 1 ) / / 2 + 1
if (flag = = 1
and (lenn - start_ind) % 2 ! = 0 ):
ans * = (lenn - start_ind) / / 2 + 1
print (ans)
S = "313"
K = 4
countStrings(S, K)
|
C#
using System;
class GFG{
static void countStrings(String s, int k)
{
int ans = 1;
int len = s.Length;
int flag = 0;
int start_ind = 0;
for ( int i = 0; i < len - 1; i++)
{
if (s[i] - '0' +
s[i + 1] - '0' == k && flag == 0)
{
flag = 1;
start_ind = i;
}
if (flag == 1 && s[i] - '0' +
s[i + 1] - '0' != k)
{
flag = 0;
if ((i - start_ind + 1) % 2 != 0)
ans *= (i - start_ind + 1 - 1) / 2 + 1;
}
}
if (flag == 1 && (len - start_ind) % 2 != 0)
ans *= (len - start_ind) / 2 + 1;
Console.WriteLine(ans);
}
public static void Main(String[] args)
{
String S = "313" ;
int K = 4;
countStrings(S, K);
}
}
|
Javascript
<script>
function countStrings(s, k)
{
var ans = 1;
var len = s.length;
var flag = 0;
var start_ind;
for ( var i = 0; i < len - 1; i++) {
if ((s[i].charCodeAt(0) - '0' .charCodeAt(0) +
s[i + 1].charCodeAt(0) - '0' .charCodeAt(0))
== k
&& flag == 0) {
flag = 1;
start_ind = i;
}
if (flag == 1
&& (s[i].charCodeAt(0) - '0' .charCodeAt(0)
+ s[i + 1].charCodeAt(0) -
'0' .charCodeAt(0))
!= k) {
flag = 0;
if ((i - start_ind + 1) % 2 != 0)
ans *= (i - start_ind + 1 - 1)
/ 2
+ 1;
}
}
if (flag == 1
&& (len - start_ind) % 2 != 0)
ans *= parseInt((len - start_ind) / 2) + 1;
document.write( ans);
}
var S = "313" ;
var K = 4;
countStrings(S, K);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...