Minimum number of sub-strings of a string such that all are power of 5
Given a binary string str. The task is to find the smallest positive integer C such that the binary string can be cut into C pieces (sub-strings) and each sub-string should be a power of 5 with no leading zeros.
Examples:
Input: str = “101101101”
Output: 3
The string “101101101” can be cut into three binary strings “101”, “101”, “101”
each of which is a power of 5.
Input: str = “1111101”
Output: 1
The string “1111101” can be cut into one binary string “1111101” which is
125 in decimal and a power of 5.
Input: str = “00000”
Output: -1
Strings of only zeroes is equivalent to 0 which is not a power of 5.
Approach: This problem is a simple variation of the longest increasing sub-sequence.
Iterate from i = 1 and for every str[j…i] where j = 0 & j < i, we check if the number formed from str[j..i] is a power of 5 then we update the dp[] array with the value of the lowest possible cut size value. After confirming that the number formed from str[j..i] in decimal is a power of 5 we calculate dp[i] = min(dp[i], dp[j] + 1).
This technique is pretty similar to finding the longest increasing sub-sequence.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define ll unsigned long long
bool ispower(ll n)
{
if (n < 125)
return (n == 1 || n == 5 || n == 25);
if (n % 125 != 0)
return false ;
else
return ispower(n / 125);
}
ll number(string s, int i, int j)
{
ll ans = 0;
for ( int x = i; x < j; x++) {
ans = ans * 2 + (s[x] - '0' );
}
return ans;
}
int minCuts(string s, int n)
{
int dp[n + 1];
memset (dp, n + 1, sizeof (dp));
dp[0] = 0;
for ( int i = 1; i <= n; i++) {
if (s[i - 1] == '0' )
continue ;
for ( int j = 0; j < i; j++) {
if (s[j] == '0' )
continue ;
ll num = number(s, j, i);
if (!ispower(num))
continue ;
dp[i] = min(dp[i], dp[j] + 1);
}
}
return ((dp[n] < n + 1) ? dp[n] : -1);
}
int main()
{
string s = "101101101" ;
int n = s.length();
cout << minCuts(s, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static boolean ispower( long n)
{
if (n < 125 )
{
return (n == 1 || n == 5 || n == 25 );
}
if (n % 125 != 0 )
{
return false ;
}
else
{
return ispower(n / 125 );
}
}
static long number(String s, int i, int j)
{
long ans = 0 ;
for ( int x = i; x < j; x++)
{
ans = ans * 2 + (s.charAt(x) - '0' );
}
return ans;
}
static int minCuts(String s, int n)
{
int [] dp = new int [n + 1 ];
Arrays.fill(dp, n+ 1 );
dp[ 0 ] = 0 ;
for ( int i = 1 ; i <= n; i++)
{
if (s.charAt(i - 1 ) == '0' )
{
continue ;
}
for ( int j = 0 ; j < i; j++)
{
if (s.charAt(j) == '0' )
{
continue ;
}
long num = number(s, j, i);
if (!ispower(num))
{
continue ;
}
dp[i] = Math.min(dp[i], dp[j] + 1 );
}
}
return ((dp[n] < n + 1 ) ? dp[n] : - 1 );
}
public static void main(String[] args)
{
String s = "101101101" ;
int n = s.length();
System.out.println(minCuts(s, n));
}
}
|
Python 3
def ispower( n):
if (n < 125 ):
return (n = = 1 or n = = 5 or n = = 25 )
if (n % 125 ! = 0 ):
return 0
else :
return ispower(n / / 125 )
def number(s, i, j):
ans = 0
for x in range ( i, j) :
ans = ans * 2 + ( ord (s[x]) - ord ( '0' ))
return ans
def minCuts(s, n):
dp = [n + 1 for i in range (n + 1 )]
dp[ 0 ] = 0 ;
for i in range ( 1 , n + 1 ) :
if (s[i - 1 ] = = '0' ):
continue
for j in range (i) :
if (s[j] = = '0' ):
continue
num = number(s, j, i)
if ( not ispower(num)):
continue
dp[i] = min (dp[i], dp[j] + 1 )
if dp[n] < n + 1 :
return dp[n]
else :
return - 1
if __name__ = = "__main__" :
s = "101101101"
n = len (s)
print (minCuts(s, n))
|
C#
using System;
class GFG
{
static Boolean ispower( long n)
{
if (n < 125)
{
return (n == 1 || n == 5 || n == 25);
}
if (n % 125 != 0)
{
return false ;
}
else
{
return ispower(n / 125);
}
}
static long number(String s, int i, int j)
{
long ans = 0;
for ( int x = i; x < j; x++)
{
ans = ans * 2 + (s[x] - '0' );
}
return ans;
}
static int minCuts(String s, int n)
{
int [] dp = new int [n + 1];
for ( int i = 0; i <= n; i++)
dp[i]=n+1;
dp[0] = 0;
for ( int i = 1; i <= n; i++)
{
if (s[i - 1] == '0' )
{
continue ;
}
for ( int j = 0; j < i; j++)
{
if (s[j] == '0' )
{
continue ;
}
long num = number(s, j, i);
if (!ispower(num))
{
continue ;
}
dp[i] = Math.Min(dp[i], dp[j] + 1);
}
}
return ((dp[n] < n + 1) ? dp[n] : -1);
}
public static void Main(String[] args)
{
String s = "101101101" ;
int n = s.Length;
Console.WriteLine(minCuts(s, n));
}
}
|
PHP
<?php
function ispower( $n )
{
if ( $n < 125)
return ( $n == 1 || $n == 5 || $n == 25);
if ( $n % 125 != 0)
return false;
else
return ispower( $n / 125);
}
function number( $s , $i , $j )
{
$ans = 0;
for ( $x = $i ; $x < $j ; $x ++)
{
$ans = $ans * 2 + (ord( $s [ $x ]) - ord( '0' ));
}
return $ans ;
}
function minCuts( $s , $n )
{
$dp = array_fill (0, $n + 1, $n + 1);
$dp [0] = 0;
for ( $i = 1; $i <= $n ; $i ++)
{
if ( $s [ $i - 1] == '0' )
continue ;
for ( $j = 0; $j < $i ; $j ++)
{
if ( $s [ $j ] == '0' )
continue ;
$num = number( $s , $j , $i );
if (!ispower( $num ))
continue ;
$dp [ $i ] = min( $dp [ $i ], $dp [ $j ] + 1);
}
}
return (( $dp [ $n ] < $n + 1) ? $dp [ $n ] : -1);
}
$s = "101101101" ;
$n = strlen ( $s );
echo minCuts( $s , $n );
?>
|
Javascript
<script>
function ispower(n)
{
if (n < 125)
return (n == 1 || n == 5 || n == 25);
if (n % 125 != 0)
return false ;
else
return ispower(parseInt(n / 125));
}
function number(s, i, j)
{
var ans = 0;
for ( var x = i; x < j; x++) {
ans = ans * 2 + (s[x] - '0' );
}
return ans;
}
function minCuts(s, n)
{
var dp = Array(n+1).fill(n+1);
dp[0] = 0;
for ( var i = 1; i <= n; i++) {
if (s[i - 1] == '0' )
continue ;
for ( var j = 0; j < i; j++) {
if (s[j] == '0' )
continue ;
var num = number(s, j, i);
if (!ispower(num))
continue ;
dp[i] = Math.min(dp[i], dp[j] + 1);
}
}
return ((dp[n] < n + 1) ? dp[n] : -1);
}
var s = "101101101" ;
var n = s.length;
document.write( minCuts(s, n));
</script>
|
Time complexity: O(n2)
Space complexity: O(n)
Last Updated :
28 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...