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].

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;
}
};
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Reorganize String.

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. [LeetCode] 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. [LC] 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. align="absmiddle" 图片的中间上下对齐

    align=absmiddle表示图像的中间与同一行中最大元素的中间对齐 AbsBottom 图像的下边缘与同一行中最大元素的下边缘对齐.AbsMiddle 图像的中间与同一行中最大元素的中间对齐.B ...

  2. 第 1 章 第 1 题 高级语言的排序问题 C++标准算法实现

    问题分析 依题意,所需程序不用过多考虑效率且暗示使用库,自然想到用高级语言实现(个人选择C++).可用顺序容器暂存数据,用标准算法解决排序问题. 代码实现 #include <iostream& ...

  3. 在 Linux 上如何清除内存的 Cache、Buffer 和交换空间

    原文链接:http://www.linuxidc.com/Linux/2015-06/118856.htm 像任何其他的操作系统一样,GNU/Linux 已经实现的内存管理不仅有效,而且更好.但是,如 ...

  4. 小程序observer函数的应用

    需求是这样的 就是构建月份的组件中,月份小于10月的时候 显示的数字都是一个位数,需要转换成两位数, 比如8月份是8 ,那就要转换为08 ,同理可得 其他低于十月份的月份也是要这样做: 打开组件的js ...

  5. jmeter中的响应断言

    断言就类似LoadRunner中的检查点.对上一个请求返回的信息,做字符串.数据包大小.HTML.XML.图片等做判断,确保返回的信息的准确性. jmeter的断言有好多,下面是一个响应断言 新建一个 ...

  6. codeforces B. Convex Shape 解题报告

    题目链接:http://codeforces.com/problemset/problem/275/B 题目内容:给出一个n * m 大小的grid,上面只有 black 和 white 两种颜色填充 ...

  7. codeforces A. Array 解题报告

    题目链接:http://codeforces.com/problemset/problem/300/A 题目意思:给出n个数,将它们分成三批:1.所有数相乘的结果 < 0    2.所有数相乘的 ...

  8. 确定mapkeeper使用的leverdb库路径

    目前libleveldb的a或so库有三个路径,/usr/lib, /usr/lib/x86_64-linux-gnu , /usr/local/lib 使用 ls -d -1 /usr/lib/*  ...

  9. Floyd算法(弗洛伊德算法) 百度百科

    核心代码 for(int k=1; k<=NODE; ++k)//对于每一个中转点 for(int i=0; i<=NODE; ++i)//枚举源点 for(int j=0; j<= ...

  10. IDEA 设置背景颜色及字号

    intellij IDEA 设置背景颜色   File--> Settings 2. Appearance & Behavior --> Appearance 设置边框背景颜色 3 ...