Find the Substring with maximum product | C ++ program to find the maximum product substring ; Function to return the value of a character ; Function to find the maximum product substring ; To store substrings ; Check if current product is maximum possible or not ; If product is 0 ; Return the substring with maximum product ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE 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 ; }