Skip to content

Latest commit

 

History

History
63 lines (52 loc) · 1.96 KB

File metadata and controls

63 lines (52 loc) · 1.96 KB

1156. Swap For Longest Repeated Character Substring

You are given a string text. You can swap two of the characters in the text.

Return the length of the longest substring with repeated characters.

Example 1:

Input: text = "ababa"
Output: 3
Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa" with length 3.

Example 2:

Input: text = "aaabaaa"
Output: 6
Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa" with length 6.

Example 3:

Input: text = "aaaaa"
Output: 5
Explanation: No need to swap, longest repeated character substring is "aaaaa" with length is 5.

Constraints:

  • 1 <= text.length <= 2 * 104
  • text consist of lowercase English characters only.

Solutions (Rust)

1. Solution

impl Solution {
    pub fn max_rep_opt1(text: String) -> i32 {
        let text = text.as_bytes();
        let mut ranges = vec![vec![]; 26];
        let mut ret = 1;

        for i in 0..text.len() {
            if i == 0 || text[i] != text[i - 1] {
                ranges[(text[i] - b'a') as usize].push((i, i));
            } else {
                ranges[(text[i] - b'a') as usize].last_mut().unwrap().1 = i;
            }
        }

        for i in 0..ranges.len() {
            for j in 0..ranges[i].len() {
                ret = ret.max(ranges[i][j].1 - ranges[i][j].0 + 1 + (ranges[i].len() > 1) as usize);

                if j > 0 && ranges[i][j].0 == ranges[i][j - 1].1 + 2 {
                    ret = ret
                        .max(ranges[i][j].1 - ranges[i][j - 1].0 + (ranges[i].len() > 2) as usize);
                }
            }
        }

        ret as i32
    }
}