Minimum number of sets with numbers less than Y
Given a string of consecutive digits and a number Y, the task is to find the number of minimum sets such that every set follows the below rule:
- Set should contain consecutive numbers
- No digit can be used more than once.
- The number in the set should not be more than Y.
Examples:
Input: s = "1234", Y = 30
Output: 3
Three sets of {12, 3, 4}
Input: s = "1234", Y = 4
Output: 4
Four sets of {1}, {2}, {3}, {4}
Approach: The following problem can be solved using a greedy approach whose steps are given below:
- Iterate in the string, and concatenate the number to a variable(let say num) using num*10 + (s[i]-‘0’)
- If the number is not greater than Y, then mark f = 1.
- If the number exceeds Y, then increase count if f = 1 and re-initialize f as 0 and also initialize num as s[i]-‘0’ or num as 0.
- After iterating in the string completely, then increase the count if f is 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumSets(string s, int y)
{
int cnt = 0;
int num = 0;
int l = s.length();
int f = 0;
for ( int i = 0; i < l; i++) {
num = num * 10 + (s[i] - '0' );
if (num <= y)
f = 1;
else
{
if (f)
cnt += 1;
num = s[i] - '0' ;
f = 0;
if (num <= y)
f = 1;
else
num = 0;
}
}
if (f)
cnt += 1;
return cnt;
}
int main()
{
string s = "1234" ;
int y = 30;
cout << minimumSets(s, y);
return 0;
}
|
Java
import java.util.*;
class solution
{
static int minimumSets(String s, int y)
{
int cnt = 0 ;
int num = 0 ;
int l = s.length();
boolean f = false ;
for ( int i = 0 ; i < l; i++) {
num = num * 10 + (s.charAt(i) - '0' );
if (num <= y)
f = true ;
else
{
if (f)
cnt += 1 ;
num = s.charAt(i) - '0' ;
f = false ;
if (num <= y)
f = true ;
else
num = 0 ;
}
}
if (f == true )
cnt += 1 ;
return cnt;
}
public static void main(String args[])
{
String s = "1234" ;
int y = 30 ;
System.out.println(minimumSets(s, y));
}
}
|
Python3
import math as mt
def minimumSets(s, y):
cnt = 0
num = 0
l = len (s)
f = 0
for i in range (l):
num = num * 10 + ( ord (s[i]) - ord ( '0' ))
if (num < = y):
f = 1
else :
if (f):
cnt + = 1
num = ord (s[i]) - ord ( '0' )
f = 0
if (num < = y):
f = 1
else :
num = 0
if (f):
cnt + = 1
return cnt
s = "1234"
y = 30
print (minimumSets(s, y))
|
C#
using System;
class solution
{
static int minimumSets( string s, int y)
{
int cnt = 0;
int num = 0;
int l = s.Length ;
bool f = false ;
for ( int i = 0; i < l; i++) {
num = num * 10 + (s[i] - '0' );
if (num <= y)
f = true ;
else
{
if (f)
cnt += 1;
num = s[i] - '0' ;
f = false ;
if (num <= y)
f = true ;
else
num = 0;
}
}
if (f == true )
cnt += 1;
return cnt;
}
public static void Main()
{
string s = "1234" ;
int y = 30;
Console.WriteLine(minimumSets(s, y));
}
}
|
PHP
<?php
function minimumSets( $s , $y )
{
$cnt = 0;
$num = 0;
$l = strlen ( $s );
$f = 0;
for ( $i = 0; $i < $l ; $i ++)
{
$num = $num * 10 + ( $s [ $i ] - '0' );
if ( $num <= $y )
$f = 1;
else
{
if ( $f )
$cnt += 1;
$num = $s [ $i ] - '0' ;
$f = 0;
if ( $num <= $y )
$f = 1;
else
$num = 0;
}
}
if ( $f )
$cnt += 1;
return $cnt ;
}
$s = "1234" ;
$y = 30;
echo (minimumSets( $s , $y ));
?>
|
Javascript
<script>
function minimumSets(s, y)
{
let cnt = 0;
let num = 0;
let l = s.length;
let f = false ;
for (let i = 0; i < l; i++) {
num = num * 10 + (s[i] - '0' );
if (num <= y)
f = true ;
else
{
if (f)
cnt += 1;
num = s[i] - '0' ;
f = false ;
if (num <= y)
f = true ;
else
num = 0;
}
}
if (f == true )
cnt += 1;
return cnt;
}
let s = "1234" ;
let y = 30;
document.write(minimumSets(s, y));
</script>
|
Time Complexity: O(N), as we are using a loop to traverse N times. Where N is the length of the string.
Auxiliary Space: O(1), as we are not using any extra space.
Last Updated :
16 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...