1544. Make The String Great Easy
1/**
2 * [1544] Make The String Great
3 *
4 * Given a string s of lower and upper case English letters.
5 * A good string is a string which doesn't have two adjacent characters s[i] and s[i + 1] where:
6 *
7 * 0 <= i <= s.length - 2
8 * s[i] is a lower-case letter and s[i + 1] is the same letter but in upper-case or vice-versa.
9 *
10 * To make the string good, you can choose two adjacent characters that make the string bad and remove them. You can keep doing this until the string becomes good.
11 * Return the string after making it good. The answer is guaranteed to be unique under the given constraints.
12 * Notice that an empty string is also good.
13 *
14 * Example 1:
15 *
16 * Input: s = "leEeetcode"
17 * Output: "leetcode"
18 * Explanation: In the first step, either you choose i = 1 or i = 2, both will result "leEeetcode" to be reduced to "leetcode".
19 *
20 * Example 2:
21 *
22 * Input: s = "abBAcC"
23 * Output: ""
24 * Explanation: We have many possible scenarios, and all lead to the same answer. For example:
25 * "abBAcC" --> "aAcC" --> "cC" --> ""
26 * "abBAcC" --> "abBA" --> "aA" --> ""
27 *
28 * Example 3:
29 *
30 * Input: s = "s"
31 * Output: "s"
32 *
33 *
34 * Constraints:
35 *
36 * 1 <= s.length <= 100
37 * s contains only lower and upper case English letters.
38 *
39 */
40pub struct Solution {}
41
42// problem: https://leetcode.com/problems/make-the-string-great/
43// discuss: https://leetcode.com/problems/make-the-string-great/discuss/?currentPage=1&orderBy=most_votes&query=
44
45// submission codes start here
46
47impl Solution {
48 pub fn make_good(s: String) -> String {
49 let mut vec: Vec<char> = vec![];
50 for c in s.chars() {
51 match vec.last() {
52 Some(x) => {
53 if (*x as i32 - c as i32).abs() == 32 {
54 vec.pop();
55 } else {
56 vec.push(c);
57 }
58 },
59 None => {
60 vec.push(c);
61 }
62 }
63 }
64
65 vec.iter().collect()
66 }
67}
68
69// submission codes end
70
71#[cfg(test)]
72mod tests {
73 use super::*;
74
75 #[test]
76 fn test_1544() {
77 assert_eq!(Solution::make_good("leEeetcode".to_owned()), "leetcode".to_owned());
78 assert_eq!(Solution::make_good("leEeEeEeEe".to_owned()), "le".to_owned());
79 assert_eq!(Solution::make_good("da".to_owned()), "da".to_owned());
80 assert_eq!(Solution::make_good("EeEeEeEeEe".to_owned()), "".to_owned());
81 }
82}
83
Back
© 2025 bowen.ge All Rights Reserved.