(Easy) Can Make Palindrome - LeetCode Contest (in progress)

Description:

Given a string s, we make queries on substrings of s.

For each query queries[i] = [left, right, k], we may rearrange the substring s[left], ..., s[right], and then choose up to k of them to replace with any lowercase English letter.

If the substring is possible to be a palindrome string after the operations above, the result of the query is true. Otherwise, the result is false.

Return an array answer[], where answer[i] is the result of the i-th query queries[i].

Note that: Each letter is counted individually for replacement so if for example s[left..right] = "aaa", and k = 2, we can only replace two of the letters.  (Also, note that the initial string s is never modified by any query.)

Example :

Input: s = "abcda", queries = [[3,3,0],[1,2,0],[0,3,1],[0,3,2],[0,4,1]]
Output: [true,false,false,true,true]
Explanation:
queries[0] : substring = "d", is palidrome.
queries[1] : substring = "bc", is not palidrome.
queries[2] : substring = "abcd", is not palidrome after replacing only 1 character.
queries[3] : substring = "abcd", could be changed to "abba" which is palidrome. Also this can be changed to "baab" first rearrange it "bacd" then replace "cd" with "ab".
queries[4] : substring = "abcda", could be changed to "abcba" which is palidrome.

Constraints:

  • 1 <= s.length, queries.length <= 10^5
  • 0 <= queries[i][0] <= queries[i][1] < s.length
  • 0 <= queries[i][2] <= s.length
  • s only contains lowercase English letters.

Solution:

Attempt1 :

Failed one test case , time out exception:

class Solution {
    public List<Boolean> canMakePaliQueries(String s, int[][] queries) {

        if(queries==null||s==null ||s.length()==0||queries.length==0||queries[0].length==0){
            return null;
        }

        List<Boolean> list = new ArrayList<Boolean>();

        for(int i = 0; i<queries.length;i++){

            String tmp = "";

                int st = queries[i][0];
                int en = queries[i][1];
                int k = queries[i][2];

                boolean test = isPalindrome2(s.substring(st,en+1),k);

                list.add(test);

        }

        return list;

    }

    /*
    public boolean isPalindrome(String s, int k){

        int begin = 0;
        int end = s.length()-1;
        int times =0;
        while(begin<end){
            if(s.charAt(begin)!= s.charAt(end)){
                if(times <k){
                    times = times+1;
                }
                else{
                    return false;
                }
            }
            begin++;
            end--;
        }
        return true;
    }
    */

    public boolean isPalindrome2(String s, int k){

        int count =0;

       HashMap<Character, Integer> map = new HashMap<>();

        for(int i = 0; i<s.length(); i++){

            if(!map.containsKey(s.charAt(i))){

                int tmp  = Count(s,s.charAt(i));

                if(tmp%2!=0){
                    map.put(s.charAt(i),tmp);
                }
            }

        }

       System.out.println(s+" "+ s.length()+" "+map.size());

        if(s.length()%2==0){

            if(map.size()/2<=k)
                return true;

        }
        else{

            if(map.size()/2-1<k){
                return true;
            }
        }

       return false; 

    }

    public int Count(String s, char a){
        int count = 0;
        for(int i =0; i<s.length(); i++){
            if(s.charAt(i)==a){
                count++;
            }
        }
        return count;
    }
}

原文地址:https://www.cnblogs.com/codingyangmao/p/11441909.html

时间: 2024-11-06 07:26:53

(Easy) Can Make Palindrome - LeetCode Contest (in progress)的相关文章

(Easy) Diet Plan Performance LeetCode Contest

Description: 5174. Diet Plan Performance My SubmissionsBack to Contest User Accepted:0 User Tried:0 Total Accepted:0 Total Submissions:0 Difficulty:Easy A dieter consumes calories[i] calories on the i-th day.  For every consecutive sequence of k days

Valid Palindrome leetcode java

题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a palindrome. Note: Have you consider

(Easy) Valid Palindrome -LeetCode

Description: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. Note: For the purpose of this problem, we define empty string as valid palindrome. Example 1: Input: "A man, a plan, a canal: P

Valid Palindrome leetcode

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you consider that th

(Easy) To Lower Case LeetCode

Description Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase. Example 1: Input: "Hello" Output: "hello" Example 2: Input: "here" Output: "here" Example 3: In

(Easy) Last Stone Weight LeetCode

class Solution { public int lastStoneWeight(int[] stones) { int len = stones.length; int i = len -1; int minus = 0; int remain = len; if(stones.length ==1){ return stones[0]; } else { Arrays.sort(stones); do{ if(stones[i]==stones[i-1]){ stones[i] = 0

(Easy) Occurences After Bigram LeetCode

Description: Given words first and second, consider occurrences in some text of the form "first second third", where second comes immediately after first, and third comes immediately after second. For each such occurrence, add "third"

(Easy) Flipping an Image LeetCode

Description: Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image. To flip an image horizontally means that each row of the image is reversed.  For example, flipping [1, 1, 0] horizontally re

(Easy) Find Common Characters LeetCode

Description Given an array A of strings made only from lowercase letters, return a list of all characters that show up in all strings within the list (including duplicates).  For example, if a character occurs 3 times in all strings but not 4 times,