LeetCode 3518: Smallest Palindromic Rearrangement II Solution

Master LeetCode problem 3518 (Smallest Palindromic Rearrangement II), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

3518. Smallest Palindromic Rearrangement II

Problem Explanation

Explanation

To solve this problem, we can follow these steps:

  1. Generate all possible palindromic permutations of the input string s.
  2. Sort the permutations lexicographically.
  3. Return the k-th permutation if it exists.

The algorithm involves generating palindromic permutations by considering half of the string and then creating the full palindromic permutations from it. We will use backtracking to generate all the permutations.

Time Complexity

The time complexity of this algorithm is O(n!) where n is the length of the input string s.

Space Complexity

The space complexity of this algorithm is O(n) due to the recursive call stack.

Solution Code

class Solution {
    public String kthSmallestPath(String s, int k) {
        char[] arr = s.toCharArray();
        Arrays.sort(arr);
        Set<String> set = new HashSet<>();
        backtrack(arr, new boolean[arr.length], new StringBuilder(), set);
        
        List<String> sortedList = new ArrayList<>(set);
        Collections.sort(sortedList);
        
        if (k > sortedList.size()) {
            return "";
        }
        
        return sortedList.get(k - 1);
    }
    
    private void backtrack(char[] arr, boolean[] visited, StringBuilder sb, Set<String> set) {
        if (sb.length() == arr.length) {
            set.add(sb.toString());
            return;
        }
        
        for (int i = 0; i < arr.length; i++) {
            if (visited[i] || (i > 0 && arr[i] == arr[i - 1] && !visited[i - 1])) {
                continue;
            }
            
            visited[i] = true;
            sb.append(arr[i]);
            backtrack(arr, visited, sb, set);
            sb.deleteCharAt(sb.length() - 1);
            visited[i] = false;
        }
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3518 (Smallest Palindromic Rearrangement II)?

This page provides optimized solutions for LeetCode problem 3518 (Smallest Palindromic Rearrangement II) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 3518 (Smallest Palindromic Rearrangement II)?

The time complexity for LeetCode 3518 (Smallest Palindromic Rearrangement II) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3518 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3518 in Java, C++, or Python.

Back to LeetCode Solutions