|
| 1 | +/** |
| 2 | + * [2264] Largest 3-Same-Digit Number in String |
| 3 | + * |
| 4 | + * You are given a string num representing a large integer. An integer is good if it meets the following conditions: |
| 5 | + * |
| 6 | + * It is a substring of num with length 3. |
| 7 | + * It consists of only one unique digit. |
| 8 | + * |
| 9 | + * Return the maximum good integer as a string or an empty string "" if no such integer exists. |
| 10 | + * Note: |
| 11 | + * |
| 12 | + * A substring is a contiguous sequence of characters within a string. |
| 13 | + * There may be leading zeroes in num or a good integer. |
| 14 | + * |
| 15 | + * |
| 16 | + * Example 1: |
| 17 | + * |
| 18 | + * Input: num = "6<u>777</u>133339" |
| 19 | + * Output: "777" |
| 20 | + * Explanation: There are two distinct good integers: "777" and "333". |
| 21 | + * "777" is the largest, so we return "777". |
| 22 | + * |
| 23 | + * Example 2: |
| 24 | + * |
| 25 | + * Input: num = "23<u>000</u>19" |
| 26 | + * Output: "000" |
| 27 | + * Explanation: "000" is the only good integer. |
| 28 | + * |
| 29 | + * Example 3: |
| 30 | + * |
| 31 | + * Input: num = "42352338" |
| 32 | + * Output: "" |
| 33 | + * Explanation: No substring of length 3 consists of only one unique digit. Therefore, there are no good integers. |
| 34 | + * |
| 35 | + * |
| 36 | + * Constraints: |
| 37 | + * |
| 38 | + * 3 <= num.length <= 1000 |
| 39 | + * num only consists of digits. |
| 40 | + * |
| 41 | + */ |
| 42 | +pub struct Solution {} |
| 43 | + |
| 44 | +// problem: https://leetcode.com/problems/largest-3-same-digit-number-in-string/ |
| 45 | +// discuss: https://leetcode.com/problems/largest-3-same-digit-number-in-string/discuss/?currentPage=1&orderBy=most_votes&query= |
| 46 | + |
| 47 | +// submission codes start here |
| 48 | + |
| 49 | +impl Solution { |
| 50 | + pub fn largest_good_integer(num: String) -> String { |
| 51 | + num.as_bytes() |
| 52 | + .windows(3) |
| 53 | + .filter(|bytes| bytes.iter().all(|&b| b == bytes[0])) //[[55,55,55]], [57,57,57]] |
| 54 | + .map(|b| b[0]) |
| 55 | + .max() |
| 56 | + .map(|b| (b as char).to_string().repeat(3)) |
| 57 | + .unwrap_or_default() //"999" |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// submission codes end |
| 62 | + |
| 63 | +#[cfg(test)] |
| 64 | +mod tests { |
| 65 | + use super::*; |
| 66 | + |
| 67 | + #[test] |
| 68 | + fn test_2264_example_1() { |
| 69 | + let num = "6777133339".to_string(); |
| 70 | + |
| 71 | + let result = "777".to_string(); |
| 72 | + |
| 73 | + assert_eq!(Solution::largest_good_integer(num), result); |
| 74 | + } |
| 75 | + |
| 76 | + #[test] |
| 77 | + fn test_2264_example_2() { |
| 78 | + let num = "2300019".to_string(); |
| 79 | + |
| 80 | + let result = "000".to_string(); |
| 81 | + |
| 82 | + assert_eq!(Solution::largest_good_integer(num), result); |
| 83 | + } |
| 84 | + |
| 85 | + #[test] |
| 86 | + fn test_2264_example_3() { |
| 87 | + let num = "42352338".to_string(); |
| 88 | + |
| 89 | + let result = "".to_string(); |
| 90 | + |
| 91 | + assert_eq!(Solution::largest_good_integer(num), result); |
| 92 | + } |
| 93 | +} |
0 commit comments