LeetCode 1177: Can Make Palindrome from Substring Solution

Master LeetCode problem 1177 (Can Make Palindrome from Substring), a medium 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.

1177. Can Make Palindrome from Substring

Problem Explanation

Explanation

To solve this problem, we can iterate through each query and check if the substring can be rearranged to form a palindrome by using at most ki replacements. We can achieve this by counting the frequency of each character in the substring and then calculating the number of characters that need to be replaced to make it a palindrome. If this count is less than or equal to ki, then the query result is true.

Algorithm

  1. Create a helper function canBePalindrome that takes a substring and ki as input.
  2. In the helper function, count the frequency of each character in the substring.
  3. Calculate the number of characters that need to be replaced to make the substring a palindrome.
  4. If the count is less than or equal to ki, return true; otherwise, return false.
  5. Iterate through each query, extract the substring, and call the canBePalindrome function.
  6. Store the results in the output array.

Time Complexity

The time complexity of this approach is O(n * m), where n is the length of the string and m is the number of queries.

Space Complexity

The space complexity is O(n), where n is the length of the string.

Solution Code

class Solution {
    public List<Boolean> canMakePaliQueries(String s, int[][] queries) {
        List<Boolean> result = new ArrayList<>();
        for (int[] query : queries) {
            String sub = s.substring(query[0], query[1] + 1);
            result.add(canBePalindrome(sub, query[2]));
        }
        return result;
    }
    
    private boolean canBePalindrome(String s, int k) {
        int[] count = new int[26];
        for (char c : s.toCharArray()) {
            count[c - 'a']++;
        }
        
        int oddCount = 0;
        for (int i = 0; i < 26; i++) {
            if (count[i] % 2 != 0) {
                oddCount++;
            }
        }
        
        return oddCount / 2 <= k;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1177 (Can Make Palindrome from Substring)?

This page provides optimized solutions for LeetCode problem 1177 (Can Make Palindrome from Substring) 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 1177 (Can Make Palindrome from Substring)?

The time complexity for LeetCode 1177 (Can Make Palindrome from Substring) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1177 on DevExCode?

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

Back to LeetCode Solutions