Skip to content

Commit d638931

Browse files
committed
2272. Substring With Largest Variance: RETRY
1 parent 18184f8 commit d638931

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1720,3 +1720,4 @@ mod s2267_check_if_there_is_a_valid_parentheses_string_path;
17201720
mod s2269_find_the_k_beauty_of_a_number;
17211721
mod s2270_number_of_ways_to_split_array;
17221722
mod s2271_maximum_white_tiles_covered_by_a_carpet;
1723+
mod s2272_substring_with_largest_variance;
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* [2272] Substring With Largest Variance
3+
*
4+
* The variance of a string is defined as the largest difference between the number of occurrences of any 2 characters present in the string. Note the two characters may or may not be the same.
5+
* Given a string s consisting of lowercase English letters only, return the largest variance possible among all substrings of s.
6+
* A substring is a contiguous sequence of characters within a string.
7+
*
8+
* Example 1:
9+
*
10+
* Input: s = "aababbb"
11+
* Output: 3
12+
* Explanation:
13+
* All possible variances along with their respective substrings are listed below:
14+
* - Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
15+
* - Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
16+
* - Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
17+
* - Variance 3 for substring "babbb".
18+
* Since the largest possible variance is 3, we return it.
19+
*
20+
* Example 2:
21+
*
22+
* Input: s = "abcde"
23+
* Output: 0
24+
* Explanation:
25+
* No letter occurs more than once in s, so the variance of every substring is 0.
26+
*
27+
*
28+
* Constraints:
29+
*
30+
* 1 <= s.length <= 10^4
31+
* s consists of lowercase English letters.
32+
*
33+
*/
34+
pub struct Solution {}
35+
36+
// problem: https://leetcode.com/problems/substring-with-largest-variance/
37+
// discuss: https://leetcode.com/problems/substring-with-largest-variance/discuss/?currentPage=1&orderBy=most_votes&query=
38+
39+
// submission codes start here
40+
41+
impl Solution {
42+
pub fn largest_variance(s: String) -> i32 {
43+
0
44+
}
45+
}
46+
47+
// submission codes end
48+
49+
#[cfg(test)]
50+
mod tests {
51+
use super::*;
52+
53+
#[test]
54+
#[ignore]
55+
fn test_2272_example_1() {
56+
let s = "aababbb".to_string();
57+
58+
let result = 3;
59+
60+
assert_eq!(Solution::largest_variance(s), result);
61+
}
62+
63+
#[test]
64+
#[ignore]
65+
fn test_2272_example_2() {
66+
let s = "abcde".to_string();
67+
68+
let result = 0;
69+
70+
assert_eq!(Solution::largest_variance(s), result);
71+
}
72+
}

0 commit comments

Comments
 (0)