767. Reorganize String

复制代码
package LeetCode_767

import java.util.*
import kotlin.collections.HashMap

/**
 * 767. Reorganize String
 * https://leetcode.com/problems/reorganize-string/description/
 *
 * 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].
 * */
class Solution {
    fun reorganizeString(S: String): String {
        val map = HashMap<Char, Int>()
        for (c in S) {
            map.put(c, map.getOrDefault(c, 0) + 1)
        }
        val maxHeap = PriorityQueue<Pair<Char, Int>> { a, b -> b.second-a.second }
        for (m in map) {
            //if some latter's appeared frequency lager than half of S
            if (m.value > (S.length + 1) / 2) {
                return ""
            }
            val pair = Pair(m.key, m.value)
            maxHeap.offer(pair)
        }
        val result = StringBuilder()
        //two characters that are adjacent to each other
        while (maxHeap.size >= 2) {
            var top1 = maxHeap.poll()
            var top2 = maxHeap.poll()
            result.append(top1.first)
            result.append(top2.first)
            //because Pair, like most data classes, is immutable, so we use copy to change it's value
            var count = top1.second
            var count2 = top2.second
            count--
            count2--
            top1 = top1.copy(second = count)
            top2 = top2.copy(second = count2)
            if (top1.second > 0) {
                maxHeap.offer(top1)
            }
            if (top2.second > 0) {
                maxHeap.offer(top2)
            }
        }
        //check if some one left
        if (maxHeap.isNotEmpty()) {
            result.append(maxHeap.poll().first)
        }
        return result.toString().reversed()
    }
}
复制代码

 

posted @   johnny_zhao  阅读(172)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示