-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3517-SmallestPalindromicRearrangementI.go
More file actions
124 lines (112 loc) · 3.55 KB
/
3517-SmallestPalindromicRearrangementI.go
File metadata and controls
124 lines (112 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
// 3517. Smallest Palindromic Rearrangement I
// You are given a palindromic string s.
// Return the lexicographically smallest palindromic permutation of s.
// Example 1:
// Input: s = "z"
// Output: "z"
// Explanation:
// A string of only one character is already the lexicographically smallest palindrome.
// Example 2:
// Input: s = "babab"
// Output: "abbba"
// Explanation:
// Rearranging "babab" → "abbba" gives the smallest lexicographic palindrome.
// Example 3:
// Input: s = "daccad"
// Output: "acddca"
// Explanation:
// Rearranging "daccad" → "acddca" gives the smallest lexicographic palindrome.
// Constraints:
// 1 <= s.length <= 10^5
// s consists of lowercase English letters.
// s is guaranteed to be palindromic.
import "fmt"
func smallestPalindrome(s string) string {
n := len(s)
freq := make([]int, 26)
for _, v := range s {
freq[v- 'a']++
}
oddCount, oddChar := 0, byte('a')
for i, c := range freq { // 得到单字符
if c % 2 != 0 {
oddCount++
oddChar = byte(i + 'a')
}
}
if oddCount > 1 { return "" } // 回文中单字符只能有一个(在中间)
res := make([]byte, n)
l, r := 0, n - 1
for i := 0; i < 26; i++ {
if freq[i] == 0 { continue }
c := byte(i + 'a')
for freq[i] >= 2 {
res[l], res[r] = c, c
l++
r--
freq[i] -= 2
}
}
if oddCount == 1 {
res[l] = oddChar
}
return string(res)
}
func smallestPalindrome1(s string) string {
// 1. 统计字符出现次数
freq := make([]int, 26)
for _, ch := range s {
freq[ch-'a']++
}
// 2. 构建左半部分和中间字符
left := make([]byte, 0, len(s)/2)
var mid byte
for i := 0; i < 26; i++ {
// 将出现次数的一半放到左半部分
for j := 0; j < freq[i]/2; j++ {
left = append(left, byte('a'+i))
}
// 如果是奇数频次,将它作为中间字符(仅可能有一个)
if freq[i]%2 == 1 {
mid = byte('a' + i)
}
}
// 3. 构建右半部分(为左半部分的逆序)
right := make([]byte, len(left))
for i := 0; i < len(left); i++ {
right[i] = left[len(left)-1-i]
}
// 4. 组合结果
if mid != 0 {
return string(left) + string(mid) + string(right)
}
return string(left) + string(right)
}
func main() {
// Example 1:
// Input: s = "z"
// Output: "z"
// Explanation:
// A string of only one character is already the lexicographically smallest palindrome.
fmt.Println(smallestPalindrome("z")) // z
// Example 2:
// Input: s = "babab"
// Output: "abbba"
// Explanation:
// Rearranging "babab" → "abbba" gives the smallest lexicographic palindrome.
fmt.Println(smallestPalindrome("babab")) // abbba
// Example 3:
// Input: s = "daccad"
// Output: "acddca"
// Explanation:
// Rearranging "daccad" → "acddca" gives the smallest lexicographic palindrome.
fmt.Println(smallestPalindrome("daccad")) // acddca
fmt.Println(smallestPalindrome("bluefrogbluefrog")) // befgloruurolgfeb
fmt.Println(smallestPalindrome("leetcodeleetcode")) // cdeeelottoleeedc
fmt.Println(smallestPalindrome1("z")) // z
fmt.Println(smallestPalindrome1("babab")) // abbba
fmt.Println(smallestPalindrome1("daccad")) // acddca
fmt.Println(smallestPalindrome1("bluefrogbluefrog")) // befgloruurolgfeb
fmt.Println(smallestPalindrome1("leetcodeleetcode")) // cdeeelottoleeedc
}