Given a non-empty string S of uppercase alphabets of length L and the task is to find the longest string from the given string with characters arranged in descending order of their ASCII code and in arithmetic progression such that the common difference should be as low as possible and the characters of the string to be of higher ASCII value.
Note : The string contains minimum three different characters.
Input : S = “ABCPQR”
Output : “RQP”
Two strings of maximum length are possible – “CBA” and “RPQ”. But since
the string should be of higher ASCII value hence, the output is “RPQ”.
Input : S = “ADGJPRT”
Output : “JGDA”
Approach : The maximum possible common difference for minimum 3 characters to be in arithmetic progression is 12. Hence, precompute all characters that are present in the string using a hashmap and then iterate from the character having maximum ASCII value i.e. ‘Z’ to the character having minimum ASCII value i.e. ‘A’. If the current character exists in the given string, consider it as the starting character of the arithmetic progression sequence and iterate again over all possible common differences i.e. from 1 to 12. Check for every current common difference that if the character exists in the given string, increment the current length of the longest required string. Now, there exist two cases when maximum length ans minimum common difference needs to be updated.
- When the current length is more than the maximum length.
- When the current length is equal to the maximum length and current common difference is less than the minimum common difference, then common difference needs to be updated.
Also, at every updation of these two parameters, starting character of the string or arithmetic progression sequence must also be updated.
Below is the implementation of above approach:
Time Complexity : O(|S| + 26*12*26), where |S| is the size of the string.
- Longest Arithmetic Progression | DP-35
- Longest arithmetic progression with the given common difference
- Arithmetic Progression
- Ratio of mth and nth term in an Arithmetic Progression (AP)
- Count of AP (Arithmetic Progression) Subsequences in an array
- Check whether Arithmetic Progression can be formed from the given array
- Program to print Arithmetic Progression series
- Program for N-th term of Arithmetic Progression series
- Find the missing number in Arithmetic Progression
- Convert given array to Arithmetic Progression by adding an element
- Minimum De-arrangements present in array of AP (Arithmetic Progression)
- PHP program to print an arithmetic progression series using inbuilt functions
- Average of ASCII values of characters of a given string
- Convert the ASCII value sentence to its equivalent string
- Check if a string contains only alphabets in Java using ASCII values
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.