【leetcode】1177. Can Make Palindrome from Substring
题目如下:
Given a string
s
, we make queries on substrings ofs
.For each query
queries[i] = [left, right, k]
, we may rearrange the substrings[left], ..., s[right]
, and then choose up tok
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 isfalse
.Return an array
answer[]
, whereanswer[i]
is the result of thei
-th queryqueries[i]
.Note that: Each letter is counted individually for replacement so if for example
s[left..right] = "aaa"
, andk = 2
, we can only replace two of the letters. (Also, note that the initial strings
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.
解题思路:对于给定一个query = [left,right,k],很容易能求出这个区间内每个字符出现的次数,如果某个字符出现了偶数次,那说明不需要经过任何改变,这个字符就能组成回文。所以这里只需要计算有多少个字符出现的次数是奇数,假设有x个字符出现的次数为奇数,那么至少就需要经过x/2次改变,才能形成回文。这里有一种情况例外,那就是只有一个字符出现的次数为奇数,那么可以不需要做任何改变。
代码如下:
class Solution(object):
def canMakePaliQueries(self, s, queries):
"""
:type s: str
:type queries: List[List[int]]
:rtype: List[bool]
"""
grid = [[0] * len(s) for _ in range(26)]
count = [0] * 26 for i,v in enumerate(s):
for j in range(26):
grid[j][i] = grid[j][i-1]
inx = ord(v) - ord('a')
count[inx] += 1
grid[inx][i] = count[inx] res = [] for left,right,k in queries:
diff = 0
for i in range(26):
if left > 0 and (grid[i][right] - grid[i][left-1]) % 2 != 0:
diff += 1
elif left == 0 and grid[i][right] % 2 != 0:
diff += 1
if diff == 1 or diff / 2 <= k:
res.append(True)
else:
res.append(False) return res
【leetcode】1177. Can Make Palindrome from Substring的更多相关文章
- 【LeetCode】9 & 234 & 206 - Palindrome Number & Palindrome Linked List & Reverse Linked List
9 - Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Som ...
- 【leetcode】1147. Longest Chunked Palindrome Decomposition
题目如下: Return the largest possible k such that there exists a_1, a_2, ..., a_k such that: Each a_i is ...
- 【LeetCode】9、Palindrome Number(回文数)
题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
随机推荐
- mount挂载相关指令
最近需要重新挂载一块数据盘,增加挂载设置,遇到一些问题做下记录. step1:df -h 或 lsblk 查看分区挂载和对应挂载的目录 /dev/xxx /data step2:umount /dev ...
- 【MM系列】SAP OX09中的地址如何取
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP OX09中的地址如何取 ...
- 【ABAP系列】SAP ABAP系统变量及注释
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP系统变量及注释 ...
- Nginx/Nginx基础学习
Nginx与node.js 一.Nginx与Node.js Nginx是一款轻量级的HTTP服务器,采用事件驱动的异步非阻塞处理方式框架,这让其具有极好的IO性能,时常用于服务端的反向代理和负载均衡. ...
- Pyinstaller-封装python
1. 当程序中没有调用matplotlib模块 ① pip intall pyinstaller ② 在cmd环境下,pyinstaller -F xxx.py 2.当程序中调用matplotlib ...
- CentOS 6.X Python 2.6升级到Python 2.7 【转】
前言:一些第三方框架为了降低复杂性,新的版本已经开始不支持旧版本的python,比如Django这个web框架1.8版本及以上仅仅只支持python2.7及以上版本(记忆中是这个1.8版本),pip安 ...
- axios入门使用
vue项目中axios的基本使用和简单封装 axios中文文档官网 http://www.axios-js.com/docs/ 一:不封装直接使用 npm install axios 在main.js ...
- Tomcat 一台机器运行多个Tomcat
转 https://www.cnblogs.com/andy1234/p/8866588.html 在一台Win10 PC 上面同时开启两个Tomcat系统为例. 1. 硬件环境 2. 到Tomcat ...
- IDEA使用指北教程
来自官网的指导手册: https://www.jetbrains.com/help/idea/2019.1/run-for-the-first-time.html?section=Windows 记得 ...
- 列表、元组和range
小知识点 s = " 5 " print(int(s)) print(s.replace(" ","")) 结果: 5 5 id()#获取对 ...