Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Three Problems from LeetCode Weekly Contest 405

Tech Jun 28 2

Problem 1: Encrypted String

This is a straightforward simulation: for each index i in the original string, the encrypted character is taken from index (i + k) % n. Below is an implementation using a character array for clarity.

class Solution {
    public String getEncryptedString(String s, int k) {
        int n = s.length();
        char[] res = new char[n];
        for (int i = 0; i < n; i++) {
            res[i] = s.charAt((i + k) % n);
        }
        return new String(res);
    }
}

Problem 2: Binary Strings Without Adjacent Zeros

We need to generate all binary strings of length n that have no two consecutive zeros. A recursive backtracking approach works well. If the pervious character is ‘0’, the current must be ‘1’; otherwise we can choose either ‘0’ or ‘1’. Another efficient method uses bitmask enumeration and a bitwise trick: check that the complement of the mask has no adjacent 1-bits (which corresponds to no adjacent 0s in the original). The two methods are shown below.

Recursive DFS:

class Solution {
    public List<String> validStrings(int n) {
        List<String> result = new ArrayList<>();
        char[] path = new char[n];
        dfs(n, 0, path, result);
        return result;
    }
    private void dfs(int n, int idx, char[] path, List<String> result) {
        if (idx == n) {
            result.add(new String(path));
            return;
        }
        // Always allowed to place '1'
        path[idx] = '1';
        dfs(n, idx + 1, path, result);
        // Place '0' only if previous character (if any) is '1'
        if (idx == 0 || path[idx - 1] == '1') {
            path[idx] = '0';
            dfs(n, idx + 1, path, result);
        }
    }
}

Bitmask enumeration:

class Solution {
    public List<String> validStrings(int n) {
        List<String> ans = new ArrayList<>();
        int total = 1 << n;
        int fullMask = total - 1;
        for (int mask = 0; mask < total; mask++) {
            int flipped = fullMask ^ mask;   // bits become 1 where original had 0
            if ((flipped & (flipped >> 1)) == 0) { // no consecutive 1s in flipped → no consecutive 0s in mask
                String s = Integer.toBinaryString((1 << n) | mask).substring(1);
                ans.add(s);
            }
        }
        return ans;
    }
}

Problem 3: Count Submatrices With Equal X and Y Frequencies

We need to count submatrices (starting from top-left) where the number of ‘X’ equals the number of ‘Y’ and at least one ‘X’ exists. A 2D prefix sum for both characters is computed. For each bottom-right cell (i,j), we check if the cumulative counts (from (0,0) to (i,j)) satisfy the condition. The implementation uses a three‑dimensional array to store the two prefix sums.

class Solution {
    public int numberOfSubmatrices(char[][] grid) {
        int rows = grid.length, cols = grid[0].length;
        int[][][] prefix = new int[rows + 1][cols + 1][2];
        int count = 0;
        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < cols; c++) {
                prefix[r + 1][c + 1][0] = prefix[r][c + 1][0] + prefix[r + 1][c][0] - prefix[r][c][0] 
                                          + (grid[r][c] == 'X' ? 1 : 0);
                prefix[r + 1][c + 1][1] = prefix[r][c + 1][1] + prefix[r + 1][c][1] - prefix[r][c][1] 
                                          + (grid[r][c] == 'Y' ? 1 : 0);
                int xCount = prefix[r + 1][c + 1][0];
                int yCount = prefix[r + 1][c + 1][1];
                if (xCount > 0 && xCount == yCount) {
                    count++;
                }
            }
        }
        return count;
    }
}

Tags: LeetCode

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.