Minimum partitions of String such that each part is at most K
Last Updated :
26 Sep, 2022
Given a string S of size N consisting of numerical digits 1-9 and a positive integer K, the task is to minimize the partitions of the string such that each part is at most K. If it’s impossible to partition the string print -1.
Examples:
Input: S = “3456”, K = 45
Output: 2
Explanation: One possible way is to partition as 3, 45, 6.
Another possibility is 3, 4, 5, 6, which uses 3 partition.
No configuration needs less than 2 partitions. Hence, the answer is 2.
Input: S = “7891634”, K = 21
Output: 5
Explanation: The minimum number of partitions is
7, 8, 9, 16, 3, 4, which uses 5 partitions.
Input: S = “67142849”, K = 39
Output: 5
Approach: The approach to this problem is based on the below idea:
Make each partition have a value as large as possible with an upper limit of K.
An edge case is if it’s impossible to partition the string. It will happen if the maximum digit in the string is larger than K..
Follow the below steps to solve this problem:
- Iterate over the characters of the string from i = 0 to N-1:
- If the number formed till now is at most K then keep on continuing with this partition. Otherwise, increase the number of partitions.
- Otherwise, If the current digit is larger than K, return -1.
- The last number may not have been accounted for after iterating the string, so check if the last partition is greater than 0. If yes, then increment number of partitions (say ans) by 1.
- Finally, return ans – 1, as we need to find the minimum number of partitions, which is one less than the numbers formed.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumCommas(string& s, int k)
{
int n = s.size();
long long cur = 0;
int ans = 0;
for ( int i = 0; i < n; ++i) {
if (cur * 10 + (s[i] - '0' ) <= k) {
cur = cur * 10 + (s[i] - '0' );
}
else {
if (cur == 0 or cur > k) {
return -1;
}
else {
ans++;
cur = s[i] - '0' ;
}
}
}
if (cur > 0 and cur <= k) {
ans++;
}
return ans - 1;
}
int main()
{
string S = "7891634" ;
int K = 21;
cout << minimumCommas(S, K);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static int minimumCommas(String s, int k)
{
int n = s.length();
long cur = 0 ;
int ans = 0 ;
for ( int i = 0 ; i < n; ++i) {
if (cur * 10
+ Character.getNumericValue(s.charAt(i))
<= k) {
cur = cur * 10
+ Character.getNumericValue(
s.charAt(i));
}
else {
if (cur == 0 || cur > k) {
return - 1 ;
}
else {
ans++;
cur = Character.getNumericValue(
s.charAt(i));
}
}
}
if (cur > 0 && cur <= k) {
ans++;
}
return ans - 1 ;
}
public static void main(String[] args)
{
String S = "7891634" ;
int K = 21 ;
System.out.print(minimumCommas(S, K));
}
}
|
Python3
def minimumCommas(s, k) :
n = len (s)
cur = 0
ans = 0
for i in range (n) :
if (cur * 10 + ( ord (s[i]) - ord ( '0' )) < = k) :
cur = cur * 10 + ( ord (s[i]) - ord ( '0' ))
else :
if (cur = = 0 or cur > k) :
return - 1
else :
ans + = 1
cur = ( ord (s[i]) - ord ( '0' ))
if (cur > 0 and cur < = k) :
ans + = 1
return ans - 1
if __name__ = = "__main__" :
S = "7891634"
K = 21
print (minimumCommas(S, K))
|
C#
using System;
class GFG {
static int minimumCommas( string s, int k)
{
int n = s.Length;
long cur = 0;
int ans = 0;
for ( int i = 0; i < n; ++i) {
if (cur * 10 + (s[i] - '0' ) <= k) {
cur = cur * 10 + (s[i] - '0' );
}
else {
if (cur == 0 || cur > k) {
return -1;
}
else {
ans++;
cur = s[i] - '0' ;
}
}
}
if (cur > 0 && cur <= k) {
ans++;
}
return ans - 1;
}
public static void Main()
{
string S = "7891634" ;
int K = 21;
Console.WriteLine(minimumCommas(S, K));
}
}
|
Javascript
<script>
function minimumCommas(s, k)
{
let n = s.length;
let cur = 0;
let ans = 0;
for (let i = 0; i < n; ++i) {
if (cur * 10 + (s.charCodeAt(i) - '0' .charCodeAt(0)) <= k) {
cur = cur * 10 + (s.charCodeAt(i) - '0' .charCodeAt(0));
}
else {
if (cur == 0 || cur > k) {
return -1;
}
else {
ans++;
cur = s.charCodeAt(i) - '0 '.charCodeAt(0);
}
}
}
// If cur > 0, means the last number is cur
if (cur > 0 && cur <= k) {
// Increment the ' ans'
ans++;
}
return ans - 1;
}
let S = "7891634" ;
let K = 21;
document.write(minimumCommas(S, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...