给定字符串str仅包含大小为N 的小写英文字母,任务是找到具有最大乘积的子字符串。
Each English alphabet has a value such that val(‘a’) = 0, val(‘b’) = 1, val(‘c’) = 2, ……, val(‘z’) = 25.
例子:
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
方法:
- 首先,遍历给定的字符串,同时保持最大的乘积值。
- 乘积将始终保持增加或保持不变,除非我们遇到‘a’ 。因此,在每次出现‘a’后开始一个新的子字符串。
- 此外,除了最大乘积值,我们还将维护最大乘积对应的子字符串。
- 遍历整个字符串,打印与最大乘积对应的子字符串。
下面是上述方法的实现:
C++
// C++ program to find the maximum product substring
#include
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;
}
Java
// Java program to find the maximum product subString
class GFG{
// Function to return the value of a character
static int value(char x)
{
return (int)(x - 'a');
}
// Function to find the maximum product subString
static String maximumProduct(String str, int n)
{
// To store subStrings
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);
// 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
public static void main(String[] args)
{
String str = "sdtfakdhdahdzz";
int n = str.length();
// Function call
System.out.print(maximumProduct(str, n) +"\n");
}
}
// This code is contributed by 29AjayKumar
Python3
# 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
C#
// C# program to find the maximum product subString
using System;
public class GFG{
// Function to return the value of a character
static int value(char x)
{
return (int)(x - 'a');
}
// Function to find the maximum product subString
static String maximumProduct(String str, int n)
{
// To store subStrings
String answer = "", curr = "";
long maxProduct = 0, product = 1;
for (int i = 0; i < n; i++) {
product *= 1L * 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
public static void Main(String[] args)
{
String str = "sdtfakdhdahdzz";
int n = str.Length;
// Function call
Console.Write(maximumProduct(str, n) +"\n");
}
}
// This code is contributed by PrinciRaj1992
Javascript
输出:
hdzz
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live