[LC] 767. Reorganize String
Given a string S, check if the letters can be rearranged so that two characters that are adjacent to each other are not the same.
If possible, output any possible result. If not possible, return the empty string.
Example 1:
Input: S = "aab"
Output: "aba"
Example 2:
Input: S = "aaab"
Output: ""
Note:
Swill consist of lowercase letters and have length in range[1, 500].
class Solution {
public String reorganizeString(String S) {
Map<Character, Integer> map = new HashMap<>();
for (char c : S.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
PriorityQueue<Map.Entry<Character, Integer>> pq = new PriorityQueue<>((a, b) -> (b.getValue() - a.getValue()));
for (Map.Entry<Character, Integer> entry: map.entrySet()) {
pq.offer(entry);
}
Map.Entry<Character, Integer> prev = null;
StringBuilder sb = new StringBuilder();
while (!pq.isEmpty()) {
Map.Entry<Character, Integer> cur = pq.poll();
sb.append(cur.getKey());
cur.setValue(cur.getValue() - 1);
if (prev != null) {
pq.offer(prev);
}
if (cur.getValue() > 0) {
prev = cur;
} else {
prev = null;
}
}
return sb.length() == S.length() ? sb.toString() : "";
}
}
[LC] 767. Reorganize String的更多相关文章
- [leetcode]Weekly Contest 68 (767. Reorganize String&&769. Max Chunks To Make Sorted&&768. Max Chunks To Make Sorted II)
766. Toeplitz Matrix 第一题不说,贼麻瓜,好久没以比赛的状态写题,这个题浪费了快40分钟,我真是...... 767. Reorganize String 就是给你一个字符串,能不 ...
- 767. Reorganize String - LeetCode
Question 767. Reorganize String Solution 题目大意: 给一个字符串,将字符按如下规则排序,相邻两个字符一同,如果相同返回空串否则返回排序后的串. 思路: 首先找 ...
- 767. Reorganize String
Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...
- [LeetCode] 767. Reorganize String 重构字符串
Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...
- LeetCode - 767. Reorganize String
Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...
- 【LeetCode】767. Reorganize String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/reorganiz ...
- [LeetCode] Reorganize String 重构字符串
Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...
- [Swift]LeetCode767. 重构字符串 | Reorganize String
Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...
- LeetCode - Reorganize String
Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...
随机推荐
- MySQL的DDL和DML
SQL语句:结构化查询语句,使用SQL与数据库“沟通”,完成相应的数据库操作. 语句分类 DDL(Data Definition Languages)语句:即数据库定义语句,用来创建数据库中的表.索引 ...
- HTML5 Canvas——基础入门
认识canvas html5的新标签 <canvas>标签只是图像容器,必须使用js来绘制图形 可以通过多种方法使用canvas绘制路径,盒,圆,字符以及添加图像 canvas画布 < ...
- 吴裕雄--天生自然MySQL学习笔记:MySQL 选择数据库
连接到 MySQL 数据库后,可能有多个可以操作的数据库,所以你需要选择你要操作的数据库. 从命令提示窗口中选择MySQL数据库 在 mysql> 提示窗口中可以很简单的选择特定的数据库.可以使 ...
- 吴裕雄--天生自然 JAVASCRIPT开发学习:Number 对象
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- MySQL--索引和外键
来自:http://www.jb51.net/article/32149.htm 1.添加PRIMARY KEY(主键索引) ALTER TABLE `table_name` ADD PRIMARY ...
- -mtime
大家在使用find命令中的mtime参数时候,会看到官方的解释如下: -mtime n File's data was last modified n*24 hours ...
- ICRA 2019最佳论文公布 李飞飞组的研究《Making Sense of Vision and Touch: Self-Supervised Learning of Multimodal Representations for Contact-Rich Tasks》获得了最佳论文
机器人领域顶级会议 ICRA 2019 正在加拿大蒙特利尔举行(当地时间 5 月 20 日-24 日),刚刚大会公布了最佳论文奖项,来自斯坦福大学李飞飞组的研究<Making Sense of ...
- Android开发—错误记录1:W/System.err: java.net.ConnectException: Connection refused
W/System.err: java.net.ConnectException: Connection refused 前台访问后台时,出现访问被拒绝情况:W/System.err: java.net ...
- Python笔记_第四篇_高阶编程_魔法(术)方法详解(重载的再详解)
1. 魔法方法是什么? 魔法方法(Magic Method)是Python比较独特的应用,它可以给你的类增加特殊的方法,如果你的对象实现了(重载),这些方法中的某一个,就会被Python所调用.正如装 ...
- python3转义编码
s = 'dy电影' print(s) # dy电影 print(type(s)) # <class 'str'> print(s.encode('utf-8')) # b'dy\xe7\ ...