Skip to content

Commit

Permalink
add: 字典序最小回文串
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Dec 13, 2023
1 parent 1e108c2 commit 03a6872
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
"Deque",
"devmode",
"dialoguer",
"efcfe",
"egcfe",
"emibcn",
"erbottlewat",
"esac",
Expand All @@ -71,6 +73,7 @@
"nagaram",
"negabinary",
"networkidle",
"neven",
"nums",
"pcxbc",
"pcxbcf",
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ Rust标准库`std::collections`提供了4种通用容器类型,包含一下8

### 字符串

- [字典序最小回文串](src/string/lexicographically_smallest_palindrome.rs) [贪心, 双指针, 字符串]

- LeetCode 2697. 字典序最小回文串 <https://leetcode.cn/problems/lexicographically-smallest-palindrome>

- [子串能表示从 1 到 N 数字的二进制串](src/string/binary_string_with_substrings_representing_1_to_n.rs) [字符串]

- LeetCode 1016. 子串能表示从 1 到 N 数字的二进制串 <https://leetcode.cn/problems/binary-string-with-substrings-representing-1-to-n>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions src/string/lexicographically_smallest_palindrome.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 字典序最小回文串
// https://leetcode.cn/problems/lexicographically-smallest-palindrome
// INLINE ../../images/string/lexicographically_smallest_palindrome.jpeg

pub struct Solution;

impl Solution {
pub fn make_smallest_palindrome(s: String) -> String {
let mut s: Vec<char> = s.chars().collect();
let mut left = 0;
let mut right = s.len() - 1;

while left < right {
if s[left] != s[right] {
s[left] = std::cmp::min(s[left], s[right]);
s[right] = std::cmp::min(s[left], s[right]);
}
left += 1;
right -= 1;
}

s.into_iter().collect()
}
}
1 change: 1 addition & 0 deletions src/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ pub mod number_of_valid_clock_times;

// 这是一个 Rust 模块,用于生成一个二进制字符串,其中包含表示 1 到 n 的所有子串
pub mod binary_string_with_substrings_representing_1_to_n;
pub mod lexicographically_smallest_palindrome;
25 changes: 25 additions & 0 deletions tests/string/lexicographically_smallest_palindrome_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use rust_practice::string::lexicographically_smallest_palindrome::Solution;

#[test]
fn make_smallest_palindrome() {
// 示例 1:
// 输入:s = "egcfe"
// 输出:"efcfe"
// 解释:将 "egcfe" 变成回文字符串的最小操作次数为 1 ,修改 1 次得到的字典序最小回文字符串是 "efcfe",只需将 'g' 改为 'f' 。
let s = "egcfe".to_string();
assert_eq!(Solution::make_smallest_palindrome(s), "efcfe".to_string());

// 示例 2:
// 输入:s = "abcd"
// 输出:"abba"
// 解释:将 "abcd" 变成回文字符串的最小操作次数为 2 ,修改 2 次得到的字典序最小回文字符串是 "abba" 。
let s = "abcd".to_string();
assert_eq!(Solution::make_smallest_palindrome(s), "abba".to_string());

// 示例 3:
// 输入:s = "seven"
// 输出:"neven"
// 解释:将 "seven" 变成回文字符串的最小操作次数为 1 ,修改 1 次得到的字典序最小回文字符串是 "neven" 。
let s = "seven".to_string();
assert_eq!(Solution::make_smallest_palindrome(s), "neven".to_string());
}
1 change: 1 addition & 0 deletions tests/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ pub mod last_substring_in_lexicographical_order_test;
pub mod minimum_number_of_frogs_croaking_test;
pub mod number_of_valid_clock_times_test;
pub mod binary_string_with_substrings_representing_1_to_n_test;
pub mod lexicographically_smallest_palindrome_test;

0 comments on commit 03a6872

Please sign in to comment.