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.
Input: str = “sdtfakdhdahdzz”
Here, the maximum product is for the substring “hdzz”.
product = 7 * 3 * 25 * 25 = 13125
Input: str = “geeksforgeeks”
- 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:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Find the Number of Maximum Product Quadruples
- Find four factors of N with maximum product and sum equal to N
- Find four factors of N with maximum product and sum equal to N | Set 3
- Find four factors of N with maximum product and sum equal to N | Set-2
- Find the number in a range having maximum product of the digits
- Find the row whose product has maximum count of prime factors
- Find a pair with maximum product in array of Integers
- Find maximum product of digits among numbers less than or equal to N
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Substring with highest frequency length product
- Encrypt string with product of number of vowels and consonants in substring of size k
- Lexicographical Maximum substring of string
- Maximum length substring having all same characters after k changes
- Lexicographically smallest substring with maximum occurrences containing a's and b's only
- Maximum length substring with highest frequency in a string
- Maximum length palindromic substring such that it starts and ends with given char
- Lexicographically smallest K-length substring containing maximum number of vowels
- Maximum splits in binary string such that each substring is divisible by given odd number
- Maximum number of set bits count in a K-size substring of a Binary String
- Maximum number of times str1 appears as a non-overlapping substring in str2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.