【leetcode】1202. Smallest String With Swaps
题目如下:
You are given a string
s, and an array of pairs of indices in the stringpairswherepairs[i] = [a, b]indicates 2 indices(0-indexed) of the string.You can swap the characters at any pair of indices in the given
pairsany number of times.Return the lexicographically smallest string that
scan be changed to after using the swaps.Example 1:
Input: s = "dcab", pairs = [[0,3],[1,2]]
Output: "bacd"
Explaination:
Swap s[0] and s[3], s = "bcad"
Swap s[1] and s[2], s = "bacd"Example 2:
Input: s = "dcab", pairs = [[0,3],[1,2],[0,2]]
Output: "abcd"
Explaination:
Swap s[0] and s[3], s = "bcad"
Swap s[0] and s[2], s = "acbd"
Swap s[1] and s[2], s = "abcd"Example 3:
Input: s = "cba", pairs = [[0,1],[1,2]]
Output: "abc"
Explaination:
Swap s[0] and s[1], s = "bca"
Swap s[1] and s[2], s = "bac"
Swap s[0] and s[1], s = "abc"Constraints:
1 <= s.length <= 10^50 <= pairs.length <= 10^50 <= pairs[i][0], pairs[i][1] < s.lengthsonly contains lower case English letters.
解题思路:可以用并查集把所有可以交换的下标分成若干个组,每个组里面的下标都是可以互相交换的,这样只需要把每个组中下标所对应的最小字符放到最小下标的位置,次小值放到次小下标的位置...最大值放到最大下标的位置,即可得到结果。
代码如下:
class Solution(object):
def smallestStringWithSwaps(self, s, pairs):
"""
:type s: str
:type pairs: List[List[int]]
:rtype: str
"""
parent = [i for i in range(len(s))]
def find(v):
if v == parent[v]:
return v
return find(parent[v])
def union(v1,v2):
p1 = find(v1)
p2 = find(v2)
if p1 > p2:
parent[p1] = p2
else:
parent[p2] = p1 for (i,j) in pairs:
union(i,j)
dic_val = {}
for i in range(len(parent)):
p = find(i)
parent[i] = p
dic_val[p] = dic_val.setdefault(p,'') + s[i] for key in dic_val.iterkeys():
dic_val[key] = ''.join(sorted(list(dic_val[key]))) res = ''
for i in parent:
res += dic_val[i][0]
dic_val[i] = dic_val[i][1:] return res
【leetcode】1202. Smallest String With Swaps的更多相关文章
- 【LeetCode】988. Smallest String Starting From Leaf 解题报告(C++ & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- 【leetcode】988. Smallest String Starting From Leaf
题目如下: Given the root of a binary tree, each node has a value from 0 to 25representing the letters 'a ...
- 【LeetCode】481. Magical String 解题报告(Python)
[LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...
- 【LeetCode】880. Decoded String at Index 解题报告(Python)
[LeetCode]880. Decoded String at Index 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】#344 Reverse String
[Question] Write a function that takes a string as input and returns the string reversed. Example: G ...
- 【leetcode】1081. Smallest Subsequence of Distinct Characters
题目如下: Return the lexicographically smallest subsequence of text that contains all the distinct chara ...
- 【LeetCode】344. Reverse String 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 新构建字符串 原地翻转 日期 题目地址:https://lee ...
- 【LeetCode】1023. Binary String With Substrings Representing 1 To N 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】1022. Smallest Integer Divisible by K 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- postMessage——解决跨域、跨窗口消息传递
参考资料1:[http://www.cnblogs.com/dolphinX/p/3464056.html] 参考资料2:[https://developer.mozilla.org/en-US/do ...
- 项目连接oracle报错:listener does not currently know of SID given in connect descriptor
今天练习用IDEA搭建了一个ssm框架,数据库是oracle,在执行mybatis-generator时,一直报错listener does not currently know of SID giv ...
- Pytorch调整学习率
每隔一定的epoch调整学习率 def adjust_learning_rate(optimizer, epoch): """Sets the learning rate ...
- leetcode之53.最大子序和
题目详情 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输出: 6 解释: ...
- JS实现网页选取截屏 保存+打印 功能(转)
源码地址: 1.1 确定截图选取范围 用户在开始截图后,需要在页面上选取一个截图范围,并且可以直观的看到,类似如下效果: image 我们的选取范围就是鼠标开始按下的那个点到鼠标拖动然后松开的那个点之 ...
- git上传项目到github教程
1 本地下载git 2 进到你项目所在地 3 git init(初始化git仓) 4 git add .(git add <file>将文件添加到git) 5 git commit -m ...
- Mycat+Mysql主从复制实现双机热备
Mycat+Mysql主从复制实现双机热备 一.mysql主从配置原理 双机热备的概念简单说一下,就是要保持两个数据库的状态自动同步.对任何一个数据库的操作都自动应用到另外一个数据库,始终保持两个数据 ...
- laravel5.5学习2-路由系统
一.初识路由 路由系统是所有 PHP 框架的核心,路由承载的是 URL 到代码片段的映射,不同的框架所附带的路由系统是这个框架本质最真实的写照,一丝不挂,一览无余.Laravel 路由中文文档:htt ...
- PostgreSQL-临时表空间与配置表
虽然我给数据库创建了表空间,但是操作中仍遇到了些问题,需要创建临时表空间. 配置表 首先了解下 postgres 的配置表,手动修改各种配置. 打开即可看到各种配置,包括临时表空间. 临时表空间 1. ...
- gym102201F_Fruit Tree
题意 给一棵带权树,多次询问路径上出现次数超过一半的数. 分析 dfs序建主席树,维护的就是根到某个节点这段路径的值域情况. 因为题目所求的不是一般的众数,而是出现次数大于一半的,所以在主席树上可以直 ...