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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Longest Arithmetic Progression | DP-35
- Longest arithmetic progression with the given common difference
- Longest subarray forming an Arithmetic Progression (AP)
- Longest subsequence forming an Arithmetic Progression (AP)
- Ratio of mth and nth term in an Arithmetic Progression (AP)
- Arithmetic Progression containing X and Y with least possible first term
- Check whether Arithmetic Progression can be formed from the given array
- Count of AP (Arithmetic Progression) Subsequences in an array
- Minimum De-arrangements present in array of AP (Arithmetic Progression)
- Program for N-th term of Arithmetic Progression series
- Program to print Arithmetic Progression series
- PHP program to print an arithmetic progression series using inbuilt functions
- Convert given array to Arithmetic Progression by adding an element
- Arithmetic Progression
- Change one element in the given array to make it an Arithmetic Progression
- Check whether nodes of Binary Tree form Arithmetic, Geometric or Harmonic Progression
- Minimum elements inserted in a sorted array to form an Arithmetic progression
- Count common elements in two arrays which are in Arithmetic Progression
- Find the missing number in unordered Arithmetic Progression
- Count of subarrays forming an Arithmetic Progression (AP)
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.