Find the Substring with maximum product
Last Updated :
18 Nov, 2021
Given 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:
C++
#include <bits/stdc++.h>
using namespace std;
int value( char x)
{
return ( int )(x - 'a' );
}
string maximumProduct(string str, int n)
{
string answer = "" , curr = "" ;
long long maxProduct = 0, product = 1;
for ( int i = 0; i < n; i++) {
product *= 1LL * value(str[i]);
curr += str[i];
if (product >= maxProduct) {
maxProduct = product;
answer = curr;
}
if (product == 0) {
product = 1;
curr = "" ;
}
}
return answer;
}
int main()
{
string str = "sdtfakdhdahdzz" ;
int n = str.size();
cout << maximumProduct(str, n) << endl;
return 0;
}
|
Java
class GFG{
static int value( char x)
{
return ( int )(x - 'a' );
}
static String maximumProduct(String str, int n)
{
String answer = "" , curr = "" ;
long maxProduct = 0 , product = 1 ;
for ( int i = 0 ; i < n; i++) {
product *= 1L * value(str.charAt(i));
curr += str.charAt(i);
if (product >= maxProduct) {
maxProduct = product;
answer = curr;
}
if (product == 0 ) {
product = 1 ;
curr = "" ;
}
}
return answer;
}
public static void main(String[] args)
{
String str = "sdtfakdhdahdzz" ;
int n = str.length();
System.out.print(maximumProduct(str, n) + "\n" );
}
}
|
Python3
def value(x):
return ( ord (x) - ord ( 'a' ))
def maximumProduct(strr, n):
answer = ""
curr = ""
maxProduct = 0
product = 1
for i in range (n):
product * = value(strr[i])
curr + = strr[i]
if (product > = maxProduct):
maxProduct = product
answer = curr
if (product = = 0 ):
product = 1
curr = ""
return answer
if __name__ = = '__main__' :
strr = "sdtfakdhdahdzz"
n = len (strr)
print (maximumProduct(strr, n))
|
C#
using System;
public class GFG{
static int value( char x)
{
return ( int )(x - 'a' );
}
static String maximumProduct(String str, int n)
{
String answer = "" , curr = "" ;
long maxProduct = 0, product = 1;
for ( int i = 0; i < n; i++) {
product *= 1L * value(str[i]);
curr += str[i];
if (product >= maxProduct) {
maxProduct = product;
answer = curr;
}
if (product == 0) {
product = 1;
curr = "" ;
}
}
return answer;
}
public static void Main(String[] args)
{
String str = "sdtfakdhdahdzz" ;
int n = str.Length;
Console.Write(maximumProduct(str, n) + "\n" );
}
}
|
Javascript
<script>
function value(x)
{
return (x.charCodeAt() - 'a' .charCodeAt());
}
function maximumProduct(str, n)
{
let answer = "" , curr = "" ;
let maxProduct = 0, product = 1;
for (let i = 0; i < n; i++) {
product *= (1 * value(str[i]));
curr += str[i];
if (product >= maxProduct) {
maxProduct = product;
answer = curr;
}
if (product == 0) {
product = 1;
curr = "" ;
}
}
return answer;
}
let str = "sdtfakdhdahdzz" ;
let n = str.length;
document.write(maximumProduct(str, n) + "<br/>" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...