(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
- 没有准考证号我是如何暴力查询英语六级成绩的
- asp.net web api 2.2 基础框架(带例子)
- 走近科学:如何一步一步解码复杂的恶意软件
- OpenAI 开源机器人模拟 Python 库:优化API接口提升400%处理速度
- 远控木马上演白利用偷天神技:揭秘假破解工具背后的盗刷暗流
- 设计模式学习(三): 装饰者模式 (附C#实现)
- 浅谈非PE的攻击技巧
- hbase源码系列(十)HLog与日志恢复
- hbase源码系列(六)HMaster启动过程
- 如何写好一篇漏洞报告(国外篇)
- hbase源码系列(八)从Snapshot恢复表
- hbase源码系列(七)Snapshot的过程
- CVE-2017-3085:Adobe Flash泄漏Windows用户凭证
- hbase源码系列(九)StoreFile存储格式
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法