[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 ...
随机推荐
- swtich多个case使用同一操作
switch (expression) { case 'status01': case 'status02': case 'status03': alert('resultOne'); break; ...
- python函数-装饰器
python函数-装饰器 1.装饰器的原则--开放封闭原则 开放:对于添加新功能是开放的 封闭:对于修改原功能是封闭的 2.装饰器的作用 在不更改原函数调用方式的前提下对原函数添加新功能 3.装饰器的 ...
- Callable、Future、线程池简单使用
Callable.Future与线程池 在创建新线程的三种方式中,继承Thread和实现Runnable接口两种方式都都没有返回值,因此当我们想要获取子线程计算结果时只能设置共享数据,同时还需要考虑同 ...
- C++交换两个变量值的方法
简单地列一下交换两个变量值地几种方法. 1.通过第三方实现,这一种也是最最最常见普通的方法: void swap(int *a, int *b) { int tmp = *a; *a = *b; *b ...
- Linux 文件上传
Linux 文件上传操作 SecureCRT与linux互相上传和下载文件 1. 需要上传或者下载,需要使用rz和sz命令. 2. 如果linux上没有这两个命令工具,则需要先安装.可以使用yum安装 ...
- Go-方法-接口-异常处理-错误处理
方法 什么是方法? 方法其实就是一个函数,在 func 这个关键字和方法名中间加入了一个特殊的接收器类型.接收器可以是结构体类型或者是非结构体类型.接收器是可以在方法的内部访问的. package m ...
- keras_yolo3阅读
源码地址 https://github.com/qqwweee/keras-yolo3 春节期间仔细看了看yolov3的kears源码,这个源码毕竟不是作者写的,有点寒酸,可能大道至简也是这么个理.我 ...
- 基于springboot实现Ueditor并生成.html的示例
一.项目架构 二.项目代码 1.HtmlProductController.java package com.controller; import java.io.File; import java. ...
- nginx安装https证书
安装证书 文件说明:1. 证书文件xxxxx.pem,包含两段内容,请不要删除任何一段内容.2. 如果是证书系统创建的CSR,还包含:证书私钥文件xxxx.key. ( 1 ) 在Nginx的安装目录 ...
- Redis_大保健
Redis redis命令参考网址: http://doc.redisfans.com/ redis主从: 集群:一组通过网络连接的计算机,共同对外提供服务,像一个独立的服务器. 一.简介 nosql ...