【leetcode】1202. Smallest String With Swaps
题目如下:
You are given a string
s
, and an array of pairs of indices in the stringpairs
wherepairs[i] = [a, b]
indicates 2 indices(0-indexed) of the string.You can swap the characters at any pair of indices in the given
pairs
any number of times.Return the lexicographically smallest string that
s
can 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^5
0 <= pairs.length <= 10^5
0 <= pairs[i][0], pairs[i][1] < s.length
s
only 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 ...
随机推荐
- APP Store上架QA&注意事项
一. App Store上架费用,要多少钱. 这个因产品而异,一般是6000-10000元人民币. 二. App Store上架周期,要多久过. 这个因产品而异,正常的话一周内,如果产品老是出问题,被 ...
- 【HANA系列】SAP Vora(SAP HANA和Hadoop)简析
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP Vora(SAP HAN ...
- LeetCode.970-强大的整数(Powerful Integers)
这是悦乐书的第367次更新,第395篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第229题(顺位题号是970).给定两个正整数x和y,如果对于某些整数i >= 0 ...
- linux 更改文件权限命令 chmod
chmod -change file mode bits :更改文件权限 chmod是用来改变文件或者目录权限的命令,但只有文件的属主和超级用户(root)才有这种权限. 更改文件权限的2种方式: 一 ...
- PTA(Basic Level)1010.一元多项式求导
设计函数求一元多项式的导数.(注:\(x^n\)(\(n\)为整数)的一阶导数为\(nx^{n−1}\).) 输入格式: 以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过 1000 的整数) ...
- poj 1061 青蛙的约会+拓展欧几里得+题解
青蛙的约会+拓展欧几里得+题解 纵有疾风起 题意 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出 ...
- HDU 1159 Common Subsequence 最长公共子序列
HDU 1159 Common Subsequence 最长公共子序列 题意 给你两个字符串,求出这两个字符串的最长公共子序列,这里的子序列不一定是连续的,只要满足前后关系就可以. 解题思路 这个当然 ...
- Python笔记(二)
python笔记 函数式编程 函数 函数是Python内建支持一种封装(将大段代码拆成函数) 通过函数的调用,可以将复制的任务分解. 函数式编程(Functional Programming) 计算机 ...
- java springmvc poi 导出Excel,先简单记录,后期会详细描写
POI jar包下载 : http://poi.apache.org/download.html jsp代码 <%@ page language="java" content ...
- luogu P2791 幼儿园篮球题
传送门 先看我们要求的是什么,要求的期望就是总权值/总方案,总权值可以枚举进球的个数\(i\),然后就应该是\(\sum_{i=0}^{k} \binom{m}{i}\binom{n-m}{k-i}i ...