KatInterview

651. 4 Keys Keyboard

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks for the maximum number of 'A's that can be printed on a screen using at most n key presses. We have four key operations: printing 'A', selecting all (Ctrl-A), copying (Ctrl-C), and pasting (Ctrl-V).

Solution

4 Keys Keyboard in Python

class Solution:
    def maxA(self, n: int) -> int:
        # dp[i] will store the maximum number of A's we can print with i key presses.
        dp = [0] * (n + 1)

        for i in range(1, n + 1):
            # Option 1: Just press 'A'.
            # This is equivalent to appending one 'A' to the result of i-1 presses.
            dp[i] = dp[i-1] + 1

            # Option 2: Use Ctrl-A, Ctrl-C, and a sequence of Ctrl-V.
            # Let's say we had dp[j] A's on screen after j presses.
            # We then press Ctrl-A (1 press) and Ctrl-C (1 press).
            # This takes 2 presses. The buffer now holds dp[j] A's.
            # The remaining i - (j + 2) presses can be used for Ctrl-V.
            # Each Ctrl-V pastes dp[j] A's.
            # So, with i - j - 2 pastes, we get dp[j] (original) + (i - j - 2) * dp[j] (pasted)
            # = (1 + i - j - 2) * dp[j] = (i - j - 1) * dp[j] total A's.
            #
            # The sequence Ctrl-A, Ctrl-C, Ctrl-V takes 3 presses, so j must be at most i-3.
            # range(1, i-2) in Python goes from 1 up to i-3.
            for j in range(1, i - 2):
                dp[i] = max(dp[i], dp[j] * (i - j - 1))
        
        return dp[n]

4 Keys Keyboard in Java

class Solution {
    public int maxA(int n) {
        // dp[i] will store the maximum number of A's we can print with i key presses.
        int[] dp = new int[n + 1];

        for (int i = 1; i <= n; i++) {
            // Option 1: Just press 'A'.
            // This is equivalent to appending one 'A' to the result of i-1 presses.
            dp[i] = dp[i-1] + 1;

            // Option 2: Use Ctrl-A, Ctrl-C, and a sequence of Ctrl-V.
            // Let's say we had dp[j] A's on screen after j presses.
            // We then press Ctrl-A (1 press) and Ctrl-C (1 press).
            // This takes 2 presses. The buffer now holds dp[j] A's.
            // The remaining i - (j + 2) presses can be used for Ctrl-V.
            // Each Ctrl-V pastes dp[j] A's.
            // So, with i - j - 2 pastes, we get dp[j] (original) + (i - j - 2) * dp[j] (pasted)
            // = (1 + i - j - 2) * dp[j] = (i - j - 1) * dp[j] total A's.
            //
            // The sequence Ctrl-A, Ctrl-C, Ctrl-V takes 3 presses, so j must be at most i-3.
            for (int j = 1; j <= i - 3; j++) {
                dp[i] = Math.max(dp[i], dp[j] * (i - j - 1));
            }
        }

        return dp[n];
    }
}

4 Keys Keyboard in C++

class Solution {
public:
    int maxA(int n) {
        // dp[i] will store the maximum number of A's we can print with i key presses.
        std::vector<int> dp(n + 1, 0);

        for (int i = 1; i <= n; ++i) {
            // Option 1: Just press 'A'.
            // This is equivalent to appending one 'A' to the result of i-1 presses.
            dp[i] = dp[i-1] + 1;

            // Option 2: Use Ctrl-A, Ctrl-C, and a sequence of Ctrl-V.
            // Let's say we had dp[j] A's on screen after j presses.
            // We then press Ctrl-A (1 press) and Ctrl-C (1 press).
            // This takes 2 presses. The buffer now holds dp[j] A's.
            // The remaining i - (j + 2) presses can be used for Ctrl-V.
            // Each Ctrl-V pastes dp[j] A's.
            // So, with i - j - 2 pastes, we get dp[j] (original) + (i - j - 2) * dp[j] (pasted)
            // = (1 + i - j - 2) * dp[j] = (i - j - 1) * dp[j] total A's.
            //
            // The sequence Ctrl-A, Ctrl-C, Ctrl-V takes 3 presses, so j must be at most i-3.
            for (int j = 1; j <= i - 3; ++j) {
                dp[i] = std::max(dp[i], dp[j] * (i - j - 1));
            }
        }

        return dp[n];
    }
};

4 Keys Keyboard in TypeScript

function maxA(n: number): number {
    // dp[i] will store the maximum number of A's we can print with i key presses.
    const dp: number[] = new Array(n + 1).fill(0);

    for (let i = 1; i <= n; i++) {
        // Option 1: Just press 'A'.
        // This is equivalent to appending one 'A' to the result of i-1 presses.
        dp[i] = dp[i - 1] + 1;

        // Option 2: Use Ctrl-A, Ctrl-C, and a sequence of Ctrl-V.
        // Let's say we had dp[j] A's on screen after j presses.
        // We then press Ctrl-A (1 press) and Ctrl-C (1 press).
        // This takes 2 presses. The buffer now holds dp[j] A's.
        // The remaining i - (j + 2) presses can be used for Ctrl-V.
        // Each Ctrl-V pastes dp[j] A's.
        // So, with i - j - 2 pastes, we get dp[j] (original) + (i - j - 2) * dp[j] (pasted)
        // = (1 + i - j - 2) * dp[j] = (i - j - 1) * dp[j] total A's.
        //
        // The sequence Ctrl-A, Ctrl-C, Ctrl-V takes 3 presses, so j must be at most i-3.
        for (let j = 1; j <= i - 3; j++) {
            dp[i] = Math.max(dp[i], dp[j] * (i - j - 1));
        }
    }

    return dp[n];
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.