【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-十大经典排序算法
随机推荐
- 【MyBatis】-----初识【MyBatis】
一.核心配置文件 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration ...
- 【ABAP系列】SAP ABAP SY-SUBRC的含义解析
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP SY-SUBR ...
- big data env setup
install Spark on CentOS: https://aodba.com/how-to-install-apache-spark-in-centos-standalone/ https:/ ...
- 【Qt开发】将内存图像数据封装成QImage V2
如何将内存图像数据封装成QImage 当采用Qt开发相机数据采集软件时,势必会遇到采集内存图像并进行处理(如缩放.旋转)操作.如果能够将内存图像数据封装成QImage,则可以利用QImage强大的图像 ...
- 动态SQL之模糊查询
模糊查询学习了三种: DAO层 // 可以使用 List<User> wherelike01(String user_name); // 忘记 List<User> where ...
- ubuntu使用iptables 持久化
iptables 持久化 安装持久化工具apt-get install iptables-persistent Ubuntu 16.04 调用语法netfilter-persistent savene ...
- vue点击除了某组件本身的其它地方, 隐藏该组件的方法
点击emoji表情标签, 出现标签组件,点击其它地方, 改组件消失的效果; <template> <div class="writeZoon"> <d ...
- Nginx 3.使用配置
转 https://www.cnblogs.com/wcwnina/p/9946747.html 本文只针对Nginx在不加载第三方模块的情况能处理哪些事情,由于第三方模块太多所以也介绍不完,当然本文 ...
- numpy数组的运算
numpy数组的运算 数组的乘法 >>> import numpy as np >>> arr=np.array([[1,2,3],[4,5,6]]) >&g ...
- ubuntu 新建root用户
1. sudo passwd :设置root用户密码 2. 切换用户 方式一:su 方式二: su root 3. 新增普通用户