Open In App

Forming String with maximum occurrences

Last Updated : 08 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ code for the above approach:
#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);
}
 
// Drivers code
int main()
{
    int a = 10;
    int b = 3;
    int c = 1;
 
    // Function Call
    int ans = solve(a, b, c);
    cout << "length of the string is : " << ans;
    return 0;
}


Java




// Java code for the above approach:
import java.util.Arrays;
 
public class GFG {
    // Function to calculate the length of the string
    // with given integer inputs a, b, and c
    static int solve(int a, int b, int c) {
        // Find the maximum among a, b, and c using Math.max() function
        int max = Math.max(Math.max(a, b), c);
 
        // Calculate the sum of the two smaller integers
        int sumOfSmaller = a + b + c - max;
 
        // Calculate the result using the given formula
        // If the maximum is greater than (sumOfSmaller + 1) * 2, return -1
        // Otherwise, return the sum of a, b, and c
        return (max > (sumOfSmaller + 1) * 2 ? -1 : a + b + c);
    }
 
    public static void main(String[] args) {
        int a = 10;
        int b = 3;
        int c = 1;
 
        // Function Call
        int ans = solve(a, b, c);
        System.out.println("length of the string is : " + ans);
    }
}


Python3




def solve(a, b, c):
    # Find the maximum among a, b, and c
    max_val = max(a, b, c)
     
    # Calculate the sum of the two smaller integers
    sum_of_smaller = a + b + c - max_val
     
    # Calculate the result using the given formula
    # If the maximum is greater than (sum_of_smaller + 1) * 2, return -1
    # Otherwise, return the sum of a, b, and c
    return -1 if max_val > (sum_of_smaller + 1) * 2 else a + b + c
 
# Main function
def main():
    a = 10
    b = 3
    c = 1
     
    # Function Call
    ans = solve(a, b, c)
    print("length of the string is:", ans)
 
if __name__ == "__main__":
    main()
     
# This code is contributed by Dwaipayan Bandyopadhyay


C#




using System;
 
class GFG
{
    // Function to calculate the length of the string
    // with given integer inputs a, b, and c
    static int Solve(int a, int b, int c)
    {
        // Find the maximum among a, b, and c
        int max = Math.Max(Math.Max(a, b), c);
 
        // Calculate the sum of the two smaller integers
        int sumOfSmaller = a + b + c - max;
 
        // Calculate the result using the given formula
        // If the maximum is greater than (sumOfSmaller + 1) * 2, return -1
        // Otherwise, return the sum of a, b, and c
        return (max > (sumOfSmaller + 1) * 2 ? -1 : a + b + c);
    }
 
    static void Main(string[] args)
    {
        int a = 10;
        int b = 3;
        int c = 1;
 
        // Function Call
        int ans = Solve(a, b, c);
        Console.WriteLine("length of the string is : " + ans);
    }
}


Javascript




// JavaScript Code Addition
function solve(a, b, c) {
  // Find the maximum among a, b, and c
  let max_val = Math.max(a, b, c);
 
  // Calculate the sum of the two smaller integers
  let sum_of_smaller = a + b + c - max_val;
 
  // Calculate the result using the given formula
  // If the maximum is greater than (sum_of_smaller + 1) * 2, return -1
  // Otherwise, return the sum of a, b, and c
  return max_val > (sum_of_smaller + 1) * 2 ? -1 : a + b + c;
}
 
// Driver code
let a = 10;
let b = 3;
let c = 1;
 
// Function Call
let ans = solve(a, b, c);
console.log("length of the string is:", ans);
 
// This code is contributed by Tapesh(tapeshdua420)


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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads