Find the Substring with maximum product

Given a string str containing only lowercase English alphabets of size N, the task is to find the substring having the maximum product.

Each English alphabet has a value such that val(‘a’) = 0, val(‘b’) = 1, val(‘c’) = 2, ……, val(‘z’) = 25.

Examples:



Input: str = “sdtfakdhdahdzz”
Output: hdzz
Here, the maximum product is for the substring “hdzz”.
product = 7 * 3 * 25 * 25 = 13125

Input: str = “geeksforgeeks”
Output: geeksforgeeks

Approach:

  • First, traverse through the given string while maintaining a maximum product value.
  • Product will always keep increasing or will remain constant unless we encounter an ‘a’. Hence, start a new substring after each ‘a’ occurrence.
  • Also, along with the maximum product value we will also maintain the substring to which the maximum product corresponds.
  • Once the entire string has been traversed, print the substring corresponding to the maximum product.

Below is the implementation of the above approach:

CPP

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the maximum product substring
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the value of a character
int value(char x)
{
    return (int)(x - 'a');
}
  
// Function to find the maximum product substring
string maximumProduct(string str, int n)
{
    // To store substrings
    string answer = "", curr = "";
    long long maxProduct = 0, product = 1;
  
    for (int i = 0; i < n; i++) {
        product *= 1LL * value(str[i]);
  
        curr += str[i];
  
        // Check if current product is maximum
        // possible or not
        if (product >= maxProduct) {
            maxProduct = product;
            answer = curr;
        }
  
        // If product is 0
        if (product == 0) {
            product = 1;
            curr = "";
        }
    }
  
    // Return the substring with maximum product
    return answer;
}
  
// Driver code
int main()
{
    string str = "sdtfakdhdahdzz";
  
    int n = str.size();
  
    // Function call
    cout << maximumProduct(str, n) << endl;
  
    return 0;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find the maximum product sub
  
# Function to return the value of a character
def value(x):
    return (ord(x) - ord('a'))
  
# Function to find the maximum product sub
def maximumProduct(strr, n):
  
    # To store subs
    answer = ""
    curr = ""
    maxProduct = 0
    product = 1
  
    for i in range(n):
        product *=value(strr[i])
  
        curr += strr[i]
  
        # Check if current product is maximum
        # possible or not
        if (product >= maxProduct):
            maxProduct = product
            answer = curr
  
        # If product is 0
        if (product == 0):
            product = 1
            curr = ""
  
    # Return the sub with maximum product
    return answer
  
# Driver code
if __name__ == '__main__':
    strr = "sdtfakdhdahdzz"
  
    n = len(strr)
  
    # Function call
    print(maximumProduct(strr, n))
  
# This code is contributed by mohit kumar 29

chevron_right


Output:

hdzz



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : mohit kumar 29

Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.