题目如下:

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

解题思路:刚看到这个题目,我觉得有点无从下手。但是仔细考虑之后,我觉得这个题目满足一个经典的算法场景——把一个数组分成两个子数组,使得两个子数组和最接近。例如,输入的字符串S="aabc",可以被实例化成字段d = {a:2,b:1,c:1},那么三个字符出现的次数就构成了[2,1,1],把数组分成和最接近的两个子数组[2]和[1,1]。最后是生成输出的字符串,只要在两个数组中依次各取一个字符拼接即可。注意:如何两个子数组和差值大于2的话,那么是不可能组成题目要求的字符串的。

代码如下:

//动态规划思想分割子数组
var diff = function(map){
var len = map.length
var sum = 0
for(var i =0;i<len;i++){
sum += map[i].v
}
var dp = []
for(var i =0;i<=len;i++){
var list = []
for(var j = 0;j <= sum/2;j ++){
list.push(0)
}
dp.push(list)
}
for(var i =1;i<=len;i++) {
for(var j = 1;j <= sum/2;j ++) {
if(j>=map[i-1].v){
dp[i][j] = Math.max(dp[i-1][j],dp[i-1][j-map[i-1].v]+map[i-1].v);
}
else {
dp[i][j] = dp[i - 1][j];
}
}
}
//子数组和的差值大于等于2,直接返回空
if (sum - 2*dp[len][parseInt(sum / 2)] >=2){
return ""
}
var t = parseInt(sum/2)
var s1 = ""
var s2 = ""
//确定每个子数组的分配的元素
for(var i=len;i>0;i--){
if(dp[i][t] > dp[i-1][t]){ // 找到第一个接近 sum/2 的,然后与 它上面的比较,如果大于,则代表当前 i 被选中
t -= map[i-1].v;
var tmp = map[i-1].v
while (tmp-- > 0){
s1 += map[i-1].k
}
}
else{
var tmp = map[i-1].v
while (tmp-- > 0){
s2 += map[i-1].k
}
}
}
//生成返回值,依次从每个子数组中取一个元素,注意和较大的子数组排在前面
var s = ""
for(var i = 0;i<Math.min(s1.length,s2.length);i++){
if(s1.length > s2.length){
s += s1[i]
s += s2[i]
}
else{
s += s2[i]
s += s1[i]
} }
if(s1.length != s2.length){
s += (s1.length > s2.length ? s1[s1.length-1] : s2[s2.length-1])
}
return s
} var reorganizeString = function(S) {
var dic = new Array(26)
for(var i=0;i<S.length;i++){
var a = S[i].charCodeAt() - 'a'.charCodeAt()
if(dic[a] == undefined){
dic[a] = 1
}
else{
dic[a] ++
}
}
var d2 = []
for(var i=0;i<dic.length;i++){
if (dic[i] == undefined){
continue
}
var ascii = String.fromCharCode(i + 'a'.charCodeAt())
d2.push({'k':ascii,'v':dic[i]})
} d2.sort(function(a,b){
return b.v - a.v
})
return diff(d2) };

【leetcode】Reorganize String的更多相关文章

  1. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

  2. 【LeetCode】8. String to Integer (atoi) 字符串转换整数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...

  3. 【LeetCode】984. String Without AAA or BBB 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字符串构造 日期 题目地址:https://leet ...

  4. 【leetcode】Scramble String

    Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...

  5. 【leetcode】 Interleaving String (hard)

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...

  6. 【leetcode】 Scramble String (hard)★

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  7. 【leetcode】Interleaving String

    Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...

  8. 【LeetCode】8. String to Integer (atoi) 字符串转整数

    题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...

  9. 【leetcode】8. String to Integer (atoi)

    题目描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...

随机推荐

  1. IIS部署网站 HTTP 错误 500.21 - Internal Server Error

    HTTP 错误 500.21 - Internal Server Error处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipel ...

  2. LeetCode.961-2N数组中N次重复的元素(N-Repeated Element in Size 2N Array)

    这是悦乐书的第365次更新,第393篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第227题(顺位题号是961).在大小为2N的数组A中,存在N+1个唯一元素,并且这些元 ...

  3. TIOBE11月份编程语言排行榜:C非常接近Java,分析下中美的就业情况

    TIOBE公布11月份编程语言排行榜:C非常接近Java Swift挤进前10,分析下中美的就业情况. 我们先看看他们官方对数据的解读 本月TIOBE指数前20位出现了一些有趣的变动.首先,C语言现在 ...

  4. web.xml文件的的param-name

    第一个阶段 配置阶段  web.xml配置,如下图   第二个阶段 初始化阶段  init(ServletConfig config) 1.加载配置文件 获取web.xml文件的的param-name ...

  5. python 并发编程 多线程 开启线程的两种方式

    一 threading模块介绍 multiprocess模块的完全模仿了threading模块的接口,二者在使用层面,有很大的相似性 二 开启线程的两种方式 第一种 每造一个进程,默认有一个线程,就是 ...

  6. angular [NgClass] [NgStyle],NgIf,[ngSwitch][ngSwitchCase]

    [NgClass]  CSS 类会根据表达式求值结果进行更新,更新逻辑取决于结果的类型: string - 会把列在字符串中的 CSS 类(空格分隔)添加进来, Array - 会把数组中的各个元素作 ...

  7. 2019JAVA第五次实验报告

    Java实验报告 班级 计科二班 学号 20188442 姓名 吴怡君 完成时间2019/10/11 评分等级 实验四 类的继承 实验目的 理解抽象类与接口的使用: 了解包的作用,掌握包的设计方法. ...

  8. []转帖]linux 上修改了nginx.conf 怎么重新加载配置文件生效

    linux 上修改了nginx.conf 怎么重新加载配置文件生效 https://www.cnblogs.com/zhuyeshen/ 步骤如下先利用/usr/local/nginx/sbin/ng ...

  9. property可以声明得位置

    property可以声明的位置有4处 1 @interface处 2 扩展处 3 protocol处 4 分类处 其中分类处的property不会合成实例变量,并且编译器也不会自动合成实例变量

  10. Java实现龟兔赛跑

    闲极无聊,加上翻手机看到龟兔赛跑的词语,想到了可以通过java起两个线程来实现龟兔赛跑的实现. 代码实现其实很简单: 首先是乌龟类: 然后是兔子类: 最后是赛跑类: 接下里让我们看一下输出结果吧: 乌 ...