题目如下:

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. eetCode刷题-递归篇

    递归是算法学习中很基本也很常用的一种方法,但是对于初学者来说比较难以理解(PS:难点在于不断调用自身,产生多个返回值,理不清其返回值的具体顺序,以及最终的返回值到底是哪一个?).因此,本文将选择Lee ...

  2. 原生js之addEventListener,removeEventListener

    使用addEventListener添加事件 <!DOCTYPE html> <html lang="en"> <head> <meta ...

  3. [深度应用]·Keras极简实现Attention结构

    [深度应用]·Keras极简实现Attention结构 在上篇博客中笔者讲解来Attention结构的基本概念,在这篇博客使用Keras搭建一个基于Attention结构网络加深理解.. 1.生成数据 ...

  4. switch-case分支结构总结

    1,格式 switch(表达式){ case 常量1:执行语句1: case 常量1:执行语句1: ... ... case 常量n:执行语句n: default:执行语句:} 2,说明: 根据swi ...

  5. spring boot-11.全局捕获异常

    1.在Spring boot 中如果发生错误,浏览器访问会默认跳转到Whitelabel Error Page 这个错误页面,如果是客户端访问的话返回JSON格式的错误数据,说明spring boot ...

  6. springboot - 应用实践(3)springboot的核心

    1.springboot的启动类与核心注解@SpringBootApplication 2.springboot基本配置 3.springboot自动配置原理

  7. java 集合 队列(Queue)

    特点:特殊线性表,先进先出(FIFO first-in-first-out) 方法: java5中 添加java.util.Queue接口,java.util.Collection 的扩展类 add( ...

  8. 洛谷 P5043 树的同构 题解

    题面 本题的难度其实不及紫题的难度.主要是在hash时的处理细节比较繁琐: 首先是树hash的模板: long long treehash(int u,int fa) { ]; ; ; for(int ...

  9. 动态class,style,src绑定写法 vue

    :class="{active:menuName===item.title}" :style="thisTitle==='一张图展示'?styles:''" : ...

  10. symfony学习笔记——路由

    symfony的路由其实就是通过url映射到控制器的一个设置 _test:    path: /test/{type}/{page} methods: [GET]    defaults: {_con ...