[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:
S
will 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 ...
随机推荐
- POJ 2014:Flow Layout 模拟水题
Flow Layout Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3091 Accepted: 2148 Descr ...
- 使用websocket实现单聊和多聊
单聊: 前端: <!DOCTYPE html> <html lang="zh-CN"> <head> <meta http-equiv=& ...
- JS-语句四
For 循环: for 循环是创建循环时常会用到的工具. 下面是 for 循环的语法: ; 语句 ; 语句 ) { 被执行的代码 } 其中语句1是初始值:语句2是.条件判断:语句3是状态改变:被执行的 ...
- 如何用Python统计《论语》中每个字的出现次数?10行代码搞定--用计算机学国学
编者按: 上学时听过山师王志民先生一场讲座,说每个人不论干什么,都应该学习国学(原谅我学了计算机专业)!王先生讲得很是吸引我这个工科男,可能比我的后来的那些同学听课还要认真些,当然一方面是兴趣.一方面 ...
- CSS3新特性—过渡、转换
过渡 转换 2D转换 2D转换包括四个方面:位移,缩放,旋转,倾斜 位移[让元素移动位置] transform: translate(100px,100px); 备注: 1. 如果只设置一个值,那么代 ...
- 【每日Scrum】第三天冲刺
一.计划会议内容 登录和个人主界面开发布局实现. 二.任务看板 三.scrum讨论照片 四.产品的状态 登录与个人界面布局实现 五.任务燃尽图
- 技术沙龙|京东云区块链进校园-京东云&深圳大学线下沙龙分享回顾
在刚刚结束的京东云&深圳大学技术沙龙活动中,多位京东云的技术大咖针对京东云BDS产品技术细节.开源计划,与深圳大学的同学和参会者进行了深入探讨,干货满满反响深刻,获得了在场同学与参会者的一致好 ...
- HDU-2544 最短路 Dijkstra模板题
题目链接:https://vjudge.net/problem/HDU-2544 题意: 题目要求找到节点1到节点n之间的一条最短路 分析: Dijkstra模板题 单源最短路径,可以用dijkstr ...
- Thread--线程工作万花筒
线程工作内存图. 线程状态.
- Java--定时
TimerTask task = new TimerTask() { @Override public void run() { // TODO Auto-generated method stub ...