Minimum Number of Substrings of a String such that all are Power of 5


Introduction

In this tutorial, we implement 2 examples using C++ to find the minimum number of substrings in a given string. Those substrings are all powers of 5 that means, the substrings are the factors of the number 5. To implement the example, take an input binary string and generate the minimum possible substrings that are factors of 5. If you want to know whether a substring is a power of 5 or not, check its decimal values.

The binary string is a combination of 1 and 0 and we cannot find a particular binary string that is multiple of 5 or not. Like 0101's decimal value is 5.

Demonstration 1

String = “101101”

Output

2

Using the above input string, the possible number of substrings that are a power of 5 is 2. The possible substrings are “101” and “101” (the substrings can be anyone). These substrings are powers of 5. Decimal value of 101 is 5 which is divisible by 5.

Demonstration 2

String = “1111111

Output

 2

In the above example, using the above input string, there are 2 possible numbers of substrings that are powers of 5 and those are 1111, 11111111. The decimal value of 1111 equals 15 and 11111111 equals 225, both the decimal values are factors of 5 or power of 5.

Algorithm

  • Take an input string in binary form.

  • Find the length of the string using length() function.

  • Generate possible substrings using the input string.

  • Check the generated substrings are factors of 5 or not.

  • If substrings are powers of 5 save the value in the counter variable.

  • Print the values of the counter variable.

Syntax

In the below examples, following C++ library functions are used:

length() : This is a built-in string library function. It returns the input string length in byte form. The length defines the number of characters in the string.

string_name.length();

memset() : It assigns contiguous memory blocks to a particular value. It has 3 parameters: char, length, and an integer value.

memset(char, len, int);

unordered_set() : It is an unordered data structure that allows insertion and deletion in an unordered pattern.

unordered_set(data_type) set_name;

unordered_set.insert():It is a library function of the unordered_set header file. It inserts elements into the set unordered.

unordered_set_name.insert(value);

unordered_set.find() : It is used to find stored values of the unordered_set. It is defined in the unordered_set header file.

unordered_set_name.find(value);

substr() : it is a string class library function used to generate substrings using the input string. It takes two parameters that define its starting point and the length of the substring.

string_name.substr(pos, len);

stoi() : This standard string class library function converts string to integer. It is frequently used in C++ programming. stoi() stands for string to integer values.

stoi(string str, position, base);

vector : It is a dynamic array. Moreover, it provides a contiguous memory location for its elements.

vector<data_type> vector_name;

Example 1

We implemented an example by defining a long long type header file. Take an input string and generate substrings and check whether the substrings are factors of 5 powers or not. Print the minimum number of substrings that can be formed using the input string.

#include <bits/stdc++.h>
using namespace std;
#define ll unsigned long long
 
//function to check substring is power of 5
bool ispowerOfFive(ll len)
{
    if (len < 125)
        return (len == 1 || len == 5 || len == 25);
    if (len % 125 != 0)
        return false;
    else
        return ispowerOfFive(len / 125);
}
 
//function to return decimal value 
ll decimalValue(string str, int x, int y)
{
    ll result = 0;
    for (int i = x; i < y; i++) 
    {
        result = result * 2 + (str[i] - '0');
    }
    return result;
}
 
int minCutValue(string str, int len)
{
    int dpg[len + 1];
 
    // memory allocation using memset
    memset(dpg, len + 1, sizeof(dpg));
    dpg[0] = 0;
 
    for (int x = 1; x <= len; x++) 
    {

        if (str[x - 1] == '0')
            continue;
        for (int y = 0; y < x; y++) 
        {
 
            if (str[y] == '0')
                continue;

            ll value = decimalValue(str, y, x);
 // calling function
            if (!ispowerOfFive(value))
                continue;

            dpg[x] = min(dpg[x], dpg[y] + 1);
        }
    }
 
    return ((dpg[len] < len + 1) ? dpg[len] : -1);
}

 int main()
{
    string str = "1011011011";
    int len = str.length();
    cout << minCutValue(str, len);
 
    return 0;
}

Output

4

Example 2

We implemented an example by using some user-defined functions. Used vector (similar to an array with no predefined size) and dynamic programming approach to generate the substrings using the input string. After generating substrings using the substr() function check if they are powers of 5 or not.

#include <vector>
#include <iostream>
#include <string>
#include <cmath>
#include <unordered_set>
using namespace std;

bool isPower(int n) 
{
    while (n % 5 == 0)
    {
        n /= 5;
    }
    return n == 1;
}

int countSubstr(const string& s) 
{
    int l = s.length();
    unordered_set<int> powersOfFive;
    
    // Pre-compute powers of 5 up to the maximum length of substring possible
    for (int x = 0; x < l; x++) 
    {
        int pwr = 1;
        for (int y = 0; y <= x; y++)
        {
            pwr *= 5;
        }
        powersOfFive.insert(pwr);
    }
    
    // Dynamic programming approach to count minimum substrings
    vector<int> dpg(l + 1, l + 1);
    dpg[0] = 0;
    
    for (int x = 1; x <= l; x++) 
    {
        for (int y = 0; y < x; y++) 
        {
            string sb = s.substr(y, x - y);
            int sbNum = stoi(sb, nullptr, 2);  // Parse binary string as integer
            
            if (powersOfFive.find(sbNum) != powersOfFive.end()) 
            {
                dpg[x] = min(dpg[x], dpg[y] + 1);
            }
        }
    }
    
    return dpg[l] == l + 1 ? -1 : dpg[l];
}

int main() {
    string s = "1111101";
    
    int output = countSubstr(s);
    if (output == -1) 
    {
        cout << "No valid substrings found." << endl;
    }
    else 
    {
        cout << "Minimum number of substrings: " << output << endl;
    }
    
    return 0;
}

Output

Minimum number of substrings: 1

Conclusion

In this tutorial, we implemented two examples in C++ using different logic to determine the number of substrings from the input string. Those substrings are powers of 5. Using length(), we determine if a substring is a factor of 5 or not. The length() function helps determine the length of a string.

To make the task requirements clear, we used 3 demonstrations in this tutorial and also various C++ library functions for implementing examples.

Updated on: 18-Aug-2023

68 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements