Problem · Dynamic Programming

Maximize the Lottery ID

Learn this problem
HardCitadelINTERNOA

Problem statement

🐣 Source note (2026-07-17): The sample lottery ID was corrected from uppercase I to lowercase l, and the source constraints were restored. The judged core task matches the visible source at about 99%.

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 on winnerID.
  • The next character of a given character ch is 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'.

Function

maximizeLotteryID(lotteryID: String, winnerID: String, k: int) → int

Examples

Example 1

lotteryID = "fpelqanxyk"winnerID = "hackerrank"k = 6return = 7
One of the optimal ways to perform the operations is as follows:
  1. Select i = 0 and move lotteryID[0] to its next character: "gpelqanxyk".
  2. Select i = 0 again: "hpelqanxyk".
  3. Select i = 2 and move lotteryID[2] to its previous character: "hpdlqanxyk".
  4. Select i = 2 again: "hpclqanxyk".
  5. Select i = 3 and move lotteryID[3] to its previous character: "hpckqanxyk".
  6. Select i = 4 and move lotteryID[4] to its next character: "hpckranxyk".
The transformed lottery ID and winnerID have a longest common subsequence of length 7; for example, "hckrank". Therefore, the maximum possible length after at most k operations is 7.

Constraints

  • 1 <= lotteryID.length, winnerID.length <= 100
  • 0 <= k <= 200

More Citadel problems

drafts saved locally
public int maximizeLotteryID(String lotteryID, String winnerID, int k) {
    // write your code here
}
lotteryID"fpelqanxyk"
winnerID"hackerrank"
k6
expected7
checking account