Problem · Dynamic Programming
Maximize the Lottery ID
The newspaper HackerRankNews distributed lottery tickets to everyone, and the winning lottery ID is described in the paper denoted by winnerID.
In order to win the lottery, a customer having a lottery ID denoted by lotteryID needs to maximize the length of the longest common subsequence of the strings lotteryID and winnerID. To do so, the customer can perform up to k operations. In each operation, the customer can change any character in lotteryID either to its next or previous character.
Find the maximum possible length of the longest common subsequence after at most k such operations.
Note:
- Operations can be performed only on
lotteryID, not onwinnerID. - The next character of a given character
chis the character that comes just after it in the alphabet. Similarly, the previous character is the character that comes just before it in the alphabet. For example, the next character of 'm' is 'n' and the previous is 'l'. For 'z' the next character is 'a' and for 'a' the previous character is 'z'.
Examples
01 · Example 1
lotteryID = "fpeIqanxyk" winnerID = "hackerrank" k = 6 return = 7
The original explanation is not completed, I add my own understanding in the end for your reference. And also Output = 7 is calculated by me, it may not be accurate. It is just here for your refernce. Once I find more source, I will modify it immediately.
One of the optimal ways to perform the operations is as follows:
Select i = 0, convert
Select i = 0, convert
Select i = 2, convert
Select i = 2, convert
The maximum possible length of the longest common subsequence after at most
lotteryID[0] to its next character, lotteryID = "gpeIqanxyk"lotteryID[0] to its next character, lotteryID = "hpeIqanxyk"lotteryID[2] to its previous character, lotteryID = "hpdIqanxyk"lotteryID[2] to its previous character, lotteryID = "hpcIqanxyk"k operations is 7.More Citadel problems
- Minimum Path Sum to Target in Binary TreePHONE SCREEN · Seen Apr 2026
- Social Media SuggestionsSeen May 2025
- Best Sum Downward Tree PathSeen May 2025
- Price CheckSeen Feb 2025
- Palindromic Substrings (LC 647 :)Seen Jan 2025
- Get Distinct Goodness ValuesSeen Jan 2025
- Get Min OperationsSeen Jan 2025
- Count Stable SegmentsSeen Jan 2025
public int maximizeLotteryID(String lotteryID, String winnerID, int k) {
// write your code here
}
lotteryID"fpeIqanxyk"
winnerID"hackerrank"
k6
expected7
sign in to submit