Forming String with maximum occurrences
Last Updated :
08 Oct, 2023
Given the maximum occurrences of a, b, and c in a string, the task is to make the string containing only a, b, and c such that no three consecutive characters are the same. If the resultant string equals a+b+c, return the length (a +b +c) otherwise -1.
Examples:
Input: a = 3 , b = 4 , c = 5
Output: 12
Explanation: one of the valid strings: “cabcabcabcbc”.
Input: a = 2, b = 1, c = 10
Output: -1
Explanation: This is an unsatisfying case, here we cannot arrange balls in such a way that no three white balls are consecutive. Make sets of balls( in pair of 2 ) . 5 sets will give 4 spaces. ” cc_cc_cc_cc_cc “. It means we should have at least 4 balls of different colors. but we have only 3 here. Therefore, in this condition result will be -1.
Naive approach: To solve the problem follow the below idea:
Try to form a string using recursion and backtrack. You can do it by making a string containing character a ,b and c. Taking one char at a time in the given ranges(no. of ‘a’ in string should be less than given value of a. ) . Keep placing characters and if conditions are not satisfiable Backtrack and place again . For every position in string except the first one you can have 2 choices. For the first one you have 3 choices. Time complexity for the brute force solution will be possible length of string raised to power of 2.
Time Complexity: O((a + b + c)2).
Auxiliary Space: O(a+b+c)
Efficient approach: To solve the problem follow the below idea:
As this problem is of combination you can actually do it in O(1) time complexity the problem do not want us to tell the string. It only want us to check whether the string is possible or not. If it is possible then return the length (a + b + c), else return -1. So just check weather maximum of {a, b, c} is less than or equal to rest two elements if true then return (a+b+c) else we cannot make a valid string so return -1;
Follow the steps to solve the problem:
- If say ‘a’ is greater than ‘b’ and ‘c’ then:
- ‘a’ should be less than of equal to ( b+c+1 )*2 for creating a valid string { explained in above example }.
- in this case, return a + b + c .
- Else
- it’s not possible to make such a string and return -1 .the
Below is the code of the above idea:
C++
#include <bits/stdc++.h>
using namespace std;
int solve( int a, int b, int c)
{
return (max({ a, b, c })
> (a + b + c - (max({ a, b, c })) + 1)
* 2
? -1
: a + b + c);
}
int main()
{
int a = 10;
int b = 3;
int c = 1;
int ans = solve(a, b, c);
cout << "length of the string is : " << ans;
return 0;
}
|
Java
import java.util.Arrays;
public class GFG {
static int solve( int a, int b, int c) {
int max = Math.max(Math.max(a, b), c);
int sumOfSmaller = a + b + c - max;
return (max > (sumOfSmaller + 1 ) * 2 ? - 1 : a + b + c);
}
public static void main(String[] args) {
int a = 10 ;
int b = 3 ;
int c = 1 ;
int ans = solve(a, b, c);
System.out.println( "length of the string is : " + ans);
}
}
|
Python3
def solve(a, b, c):
max_val = max (a, b, c)
sum_of_smaller = a + b + c - max_val
return - 1 if max_val > (sum_of_smaller + 1 ) * 2 else a + b + c
def main():
a = 10
b = 3
c = 1
ans = solve(a, b, c)
print ( "length of the string is:" , ans)
if __name__ = = "__main__" :
main()
|
C#
using System;
class GFG
{
static int Solve( int a, int b, int c)
{
int max = Math.Max(Math.Max(a, b), c);
int sumOfSmaller = a + b + c - max;
return (max > (sumOfSmaller + 1) * 2 ? -1 : a + b + c);
}
static void Main( string [] args)
{
int a = 10;
int b = 3;
int c = 1;
int ans = Solve(a, b, c);
Console.WriteLine( "length of the string is : " + ans);
}
}
|
Javascript
function solve(a, b, c) {
let max_val = Math.max(a, b, c);
let sum_of_smaller = a + b + c - max_val;
return max_val > (sum_of_smaller + 1) * 2 ? -1 : a + b + c;
}
let a = 10;
let b = 3;
let c = 1;
let ans = solve(a, b, c);
console.log( "length of the string is:" , ans);
|
Output
length of the string is : 14
Time complexity: O(1), only if else conditions are used.
Auxiliary Space: O(1), no extra space is used.
Share your thoughts in the comments
Please Login to comment...