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的更多相关文章

  1. [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 就是给你一个字符串,能不 ...

  2. 767. Reorganize String - LeetCode

    Question 767. Reorganize String Solution 题目大意: 给一个字符串,将字符按如下规则排序,相邻两个字符一同,如果相同返回空串否则返回排序后的串. 思路: 首先找 ...

  3. 767. Reorganize String

    Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...

  4. [LeetCode] 767. Reorganize String 重构字符串

    Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...

  5. LeetCode - 767. Reorganize String

    Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...

  6. 【LeetCode】767. Reorganize String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/reorganiz ...

  7. [LeetCode] Reorganize String 重构字符串

    Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...

  8. [Swift]LeetCode767. 重构字符串 | Reorganize String

    Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...

  9. LeetCode - Reorganize String

    Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...

随机推荐

  1. Unity3d中渲染到RenderTexture的原理,几种方式以及一些问题

    超级搬运工 http://blog.csdn.net/leonwei/article/details/54972653 ---------------------------------------- ...

  2. mysql第四篇--SQL逻辑查询语句执行顺序

    mysql第四篇--SQL逻辑查询语句执行顺序 一.SQL语句定义顺序 SELECT DISTINCT <select_list> FROM <left_table> < ...

  3. mysql四种事务隔离级别

    mysql事务并发问题 ACID什么的就不啰嗦了.mysql多个事务并发的时候,可能会出现如下问题: 1. 更新丢失 即两个事务同时更新某一条数据,后执行的更新操作会覆盖先执行的更新操作,导致先执行的 ...

  4. Mac 用终端(命令行)打开vscode编辑器

    1.打开控制面板(⇧⌘P) 2.输入 shell command 在提示里看到 Shell Command: Install ‘code’ command in PATH, 就可以了. 3.使用: c ...

  5. Dlib笔记一:基本数据结构和基本操作

    编译了Dlib之后就开始想着怎么用起来,先从基本的数据类型说起吧,因为是图像,所以难免会跟OpenCV的数据类型比较.在Dlib中,图像是用二维阵列(array2d)或者矩阵(matrix)来表示的, ...

  6. 开源PLM软件Aras详解八 Aras之RelationshipTypes关系类详解

    在Aras中,在之前ItemType解析中有提到,Aras中实际ItemType对应的就是一张表,那么,ItemType与ItemType之间是如何关联的呢, 如果我们需要捋清楚ItemType与It ...

  7. JavaWeb之Servlet入门(一)

    1. Servlet介绍 Servlet(Server Applet),全称Java Servlet,是用Java编写的服务器端程序.其主要功能在于交互式地浏览和修改数据,生成动态Web内容. 2. ...

  8. Ansible-大保健

    一.Ansible大纲 Ansible被红帽收购 1.什么是Ansible 2.Ansible特性\优点 3.Ansible基础架构 控制端\被控端\inventory\ad-hoc\playbook ...

  9. mysql not in 或 in 优化

    在MySQL 中,not in 或in 优化思路, 利用left join 来优化,类似如下的查询方式: select id from a where id in (select id from b ...

  10. C语言如何获得精确到毫秒的时间

    在做测试或性能优化时,经常要知道程序运行的时间,在Linux系统可以使用time命令来计算程序运行运行所消耗的时间,能精确到毫秒,如果要精确到代码块或某个操作运行时所消耗的时间,time命令就不给力了 ...