Post

1302 Delete Characters To Make Fancy String

1302 Delete Characters To Make Fancy String

Delete Characters to Make Fancy String image

A fancy string is a string where no three consecutive characters are equal.

Given a string s, delete the minimum possible number of characters from s to make it fancy.

Return the final string after the deletion. It can be shown that the answer will always be unique.

 

Example 1:

1
2
3
4
5
6
7
**Input:** s = "leeetcode"
**Output:** "leetcode"
**Explanation:**
Remove an 'e' from the first group of 'e's to create "leetcode".
No three consecutive characters are equal, so return "leetcode".

Example 2:

1
2
3
4
5
6
7
8
**Input:** s = "aaabaaaa"
**Output:** "aabaa"
**Explanation:**
Remove an 'a' from the first group of 'a's to create "aabaaaa".
Remove two 'a's from the second group of 'a's to create "aabaa".
No three consecutive characters are equal, so return "aabaa".

Example 3:

1
2
3
4
5
**Input:** s = "aab"
**Output:** "aab"
**Explanation:** No three consecutive characters are equal, so return "aab".

 

Constraints:

1
2
1 <= s.length <= 105
s consists only of lowercase English letters.
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

use std::collections::HashMap;
impl Solution {
    pub fn make_fancy_string(s: String) -> String {
        let mut left : usize = 0;
        let mut right : usize = 0 ;
        let mut result : String = "".to_string();
        let mut chars = HashMap::new();
        let s_chars: Vec<char> = s.chars().collect();

        while right < 2 && right < s.len() {
            chars.entry(s_chars[right]).and_modify(|x| *x += 1).or_insert(1);
            result += &(s_chars[right]).to_string();
            right += 1;
        }
 
        while right < s.len() {
            chars.entry(s_chars[right]).and_modify(|x| *x += 1).or_insert(1);
            //println!("{:?}", chars);
            if chars.len() == 1 {
                
            } else {
                result += &(s_chars[right]).to_string()
            }
            chars.entry(s_chars[left]).and_modify(|x| *x -= 1);
            if chars.get(&s_chars[left]) == Some(&0) {
                chars.remove(&s_chars[left]);
            }
            left += 1 ;
            
            right += 1;
        }
        result

    }
}



This post is licensed under CC BY 4.0 by the author.