Longest substring of only 4’s from the first N Characters of the Infinite String


Introduction

In this tutorial, we implement an approach to find the longest substring of only 4 using the first N characters of the infinite string. Infinite string using 4 looks like this: “44444444……” and for this string we define the length of characters to consider for solving the task. To solve the problem of this tutorial, consider an input numerical string, We solve this problem using two conditions and those conditions are as follows:

  • Consider an input string with random digits and generate the longest substring of 4’s from the string.

  • We consider an infinite string of combinations of 4, 5, 44, 45, etc. This string will contain digits of 4 and 5 combinations in increasing order. Using this string find the length of the longest substring containing 4’s only.

Demonstration 1

String = “3444445443”

Output

the longest substring containing 4’s is 44444

Explanation

In the above input string 444445443, there are 3, 4 and 5. The task is to generate a substring containing only 4’s. So the output of the task using the input string is 44444.

Demonstration 2

In this demonstration, we take an input string of 4 and 5 digit combinations in increasing order. We define the length of the input string and generate output in the form of the length of the longest substring containing only 4.

Total characters in the string = 6

Output

3

Explanation

We take 6 characters for the input string, and the resulting input string is 454445. Using this string, the length of the substring that contains only 4 characters using the first N characters is 3.

Demonstration 3

Length of the string = 8

Output

3

The length of the input string is 8 and for this the possible input string is 45444554. The length of the substring containing only 4’s using the initial characters is 3.

C++ Library Functions

Syntax

length() : It is a string class library function that returns the length of the string. The length of the string in byte format is the sum of the total characters in the string.

string_name.length();

substr(): It is defined in the string class header file. It generates substrings using the input string. It takes 2 parameters: begin, length.

string_name.substr(begin, length); 

Algorithm 1

  • Take the input string length.

  • The function maxlengthSubstr() returns the length of the longest substring containing only 4’s.

  • Initialize a prefix array to generate the possible number of input string combinations.

  • The array will iterate from array variables to the MAXN.

  • Find the length of the substring by calculating the result variable value.

Example 1

In this implementation, the user-defined function maxLengthSubstr() returns the length of the longest substring containing only 4’s. Initialize an pre_arr{} to calculate the length. Do not generate the input string in its entirety, instead specify its length. A constant variable MAXN is defined at the beginning of code.

Its C++ implementation is:

#include <bits/stdc++.h>
using namespace std;
#define MAXN 30
 
// Function returning the length of the longest substring containing only 4
int maxLengthSubstr(int Num)
{
    //variable containing the result
    int ans;
 
    // Initialize array to make groups
    int pre_arr[MAXN], q = 1;

    pre_arr[0] = 0;
    for (int x = 1; x < MAXN; x++)
    {
        q *= 2;
        pre_arr[x] = pre_arr[x - 1] + x * q;
    }

    int l;
 
 //Finding the length of the longest substring
    for (int x = 1; x < MAXN; x++) 
    {
        if (pre_arr[x] >= Num) 
        {
            l = x;
            break;
        }
    }
 
    int i = Num - pre_arr[l - 1];
    int j = 2 * l - 1;
 
    if (i >= j)
        ans = min(i, j);
    else
        ans = max(i, 2 * (l - 2) + 1);
 
    return ans;
}
 
// Controller
int main()
{
    int Num = 8;
    cout << "Maximum length of the longest substring of 8 string characters containing only 4s is : " << maxLengthSubstr(Num);
 
    return 0;
}

Output

Maximum length of the longest substring of 8 string characters containing only 4s is : 3

Example 2

In this implementation, the user-defined function longestSubstr() returns the longest substring containing only 4 using the first N characters of the input string. Each character of the input string is compared with 4 and when there is a match, increase the value of the counter variable.

Here is the C++ implementation of the above explanation.

#include <iostream>
#include <string>
using namespace std;

//function returning the longest substring containing only 4
string longestSubstring(const string& str) 
{
    int maxLen = 0;
    int start = 0;
    int currentLen = 0;
    int currentStartIndex = 0;

//iterating characters of the string
    for (int x = 0; x < str.length(); x++) 
    {
        if (str[x] == '4') 
        {
            if (currentLen == 0) 
            {
                currentStartIndex = x;
                currentLen = 1;
            }
            else 
            {
                currentLen++;
            }

            if (currentLen > maxLen) 
            {
                maxLen = currentLen;
                start = currentStartIndex;
            }
        } 
        else 
        {
            currentLen = 0;
        }
    }

    string longestSubstr = str.substr(start, maxLen);
    return longestSubstr;
}

//Controller
int main() 
{
    string str = "344444405474444";
    
    string longestSubstr = longestSubstring(str);
    cout << "Longest substring of only 4's: " << longestSubstr << endl;

    return 0;
}

Output

Longest substring of only 4's: 44444

Conclusion

We have reached the end of finding the longest substring with only 4’s from the first N characters of the input string. This was done using C++ logic. The first solution involves comparing each character in the input string to find the longest substring of only 4s. In the second solution, determine the length of the longest substring containing only 4's. This is done by using a string that contains the digits 4 and 5 in increasing order like 4, 5, 44, 45….

Both solutions helped resolve the problem by using some C++ library functions. Some demonstrations are used to elaborate on the purpose of the problem statement.

Updated on: 18-Aug-2023

56 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements