267. Palindrome Permutation II

复制代码
package LeetCode_267

import java.lang.StringBuilder

/**
 * 267. Palindrome Permutation II
 * (Peime)
 * Given a string s, return all the palindromic permutations (without duplicates) of it.
 * Return an empty list if no palindromic permutation could be form.

Example 1:
Input: "aabb"
Output: ["abba", "baab"]

Example 2:
Input: "abc"
Output: []
 * */
class Solution {
    /*
    * solution: DFS+backtracking
    * Time complexity:O((n/2)!), Space complexity:O(n): the depth of recursion is at most n/2
    * */
    fun generatePalindromes(s: String): List<String> {
        val result = ArrayList<String>()
        val charList = ArrayList<Char>()
        //count number of c and odd
        var oddCount = 0
        var mid=""
        val map = HashMap<Char, Int>()
        for (c in s) {
            map.put(c, map.getOrDefault(c, 0) + 1)
            oddCount += if (map.get(c)!! % 2 != 0) 1 else -1
        }
        //cannot form any palindrome string
        if (oddCount > 1) {
            return result
        }
        for ((key, value) in map) {
            //if is odd, add string in mid
            if (value % 2 != 0) {
                mid += key
            }
            //get half of string to gen palindrome permutation
            for (i in 0 until value / 2) {
                charList.add(key)
            }
        }
        dfs(charList, mid, BooleanArray(charList.size), StringBuilder(), result)
        //println(result)
        return result
    }

    private fun dfs(chatList:ArrayList<Char>, mid:String, visited:BooleanArray, cur:StringBuilder, result: ArrayList<String>) {
        if (cur.length == chatList.size) {
           //create the palindrome string
           result.add(cur.toString() + mid + cur.reverse().toString())
           //set it back
           cur.reverse()
           return
       }
        for (i in 0 until chatList.size) {
            if (!visited[i]) {
                visited[i] = true
                cur.append(chatList.get(i))
                dfs(chatList, mid, visited, cur, result)
                //backtracking
                cur.deleteCharAt(cur.lastIndex)
                visited[i] = false
            }
        }
    }
}
复制代码

 

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