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].
Approach #1 Sort by count:
class Solution {
public:
string reorganizeString(string S) {
int len = S.length();
vector<int> count(26, 0);
string ans = S;
for (auto s : S) count[s-'a'] += 100;
for (int i = 0; i < 26; ++i) count[i] += i;
sort(count.begin(), count.end());
int start = 1;
int mid = (len + 1) / 2;
for (int i = 0; i < 26; ++i) {
int times = count[i] / 100;
char c = 'a' + (count[i] % 100);
if (times > mid) return "";
for (int j = 0; j < times; ++j) {
if (start >= len) start = 0;
ans[start] = c;
start += 2;
}
}
//ans += '\0';
return ans;
}
};
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 题目大意: 给一个字符串,将字符按如下规则排序,相邻两个字符一同,如果相同返回空串否则返回排序后的串. 思路: 首先找 ...
- [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 ...
- [LC] 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 ...
随机推荐
- oracle 的sys 和 system 账号
sys 和 system 账号有啥区别?一直以来懵懵懂懂,只想当然的认为就是权限大小不一样. 但是,它们都是管理员? 现在,我知道有一个区别了: [sys]只能用sysdba身份登录(也许还有syso ...
- 【BZOJ1483】[HNOI2009]梦幻布丁 链表+启发式合并
[BZOJ1483][HNOI2009]梦幻布丁 Description N个布丁摆成一行,进行M次操作.每次将某个颜色的布丁全部变成另一种颜色的,然后再询问当前一共有多少段颜色.例如颜色分别为1,2 ...
- spring源码解析——2容器的基本实现(第2版笔记)
感觉第二版写的略潦草,就是在第一版的基础上加上了新的流行特性,比如idea,springboot,但是,潦草痕迹遍布字里行间. 虽然换成了idea,但是很多截图还是eclipse的,如果不是看了第一版 ...
- Vue入门(一) 环境配置
Node.js 安装,https://nodejs.org/en/ 默认安装就可以 安装好后测试版本,cmd 键入命令 1.node -v 2.npm -v 安装,淘宝 NPM n ...
- Oracle 数据库中序列结合触发器实现主键自增长
一.数据表名称为T_OFFICE,其主键为PID(number类型) 二.首先为数据表的PID字段创建序列 序列名称:S_T_OFFICE_PID 序列详细内容: 三.创建相应的触发器 触发器名称:T ...
- 整形范围 运行Java代码的机器
Java 无关 C C++ 有关 移植 整形溢出
- 统计 与 数学 induction 归纳 deduction 演绎 吴喜之老师
“统计的思维方式是归纳(induction),也就是从数据所反映的现实得到比较一般的模型,希望以此解释数据所代表的那部分世界.这和以演绎(deduction)问哦主的数学思维方式相反,演绎是在一些人为 ...
- linux SVN 安装配置
svn服务器有2种运行方式 1.独立服务器 (例如:svn://xxx.com/xxx):2.借助apache.(例如:http://svn.xxx.com/xxx):为了不依赖apache,选择第一 ...
- 【洛谷 2405】 non天平
题目背景 non最近正在为自己的体重而苦恼,他想称量自己的体重.于是,他找来一个天平与许多砝码. 题目描述 砝码的重量均是n的幂次,n^1.n^2.n^3.n^4.n^5的……non想知道至少要多少个 ...
- 侧方位停车想一次过,掌握边线30cm很重要!
侧方位停车要想一次过关,关键在于保持车身距离库边线30cm左右的距离.但是,往往有很多学员掌控不好这个距离,导致倒库时压线.那么,如何找准这个30cm呢?下面,小编就来教大家方法,赶紧学习吧! 侧方位 ...