Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3e96af6

Browse files
committed
min window
1 parent b388fc4 commit 3e96af6

File tree

3 files changed

+100
-4
lines changed

3 files changed

+100
-4
lines changed

‎src/main.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
mod longest_repeating_character;
2-
use longest_repeating_character::Solution;
1+
mod minimum_window_substring;
2+
use minimum_window_substring::Solution;
33
fn main() {
4-
let s = "AABABBBBB".to_string();
5-
let result = Solution::character_replacement(s, 0);
4+
let s1 = "aa".to_string();
5+
let s2 = "aa".to_string();
6+
let result = Solution::min_window(s1, s2);
67
print!("{:?}", result);
78
}

‎src/minimum_window_substring.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use std::collections::HashMap;
2+
pub struct Solution {}
3+
impl Solution {
4+
pub fn min_window(s: String, t: String) -> String {
5+
if t.len() == 0 || t.len() > s.len() {
6+
return "".to_string();
7+
}
8+
let mut map: HashMap<char, i32> = HashMap::new();
9+
let mut window: HashMap<char, i32> = HashMap::new();
10+
let s_chars: Vec<char> = s.chars().collect();
11+
for c in t.chars() {
12+
map.entry(c).and_modify(|e| *e += 1).or_insert(1);
13+
}
14+
let mut have = 0;
15+
let need = map.len();
16+
let mut result: (i32, i32) = (-1, -1);
17+
let mut length = i32::MAX;
18+
let mut lp = 0;
19+
for rp in 0..s.len() {
20+
let c = s_chars[rp];
21+
window.entry(c).and_modify(|e| *e += 1).or_insert(1);
22+
if map.contains_key(&c) {
23+
let window_count = window.get(&c).unwrap();
24+
let map_count = map.get(&c).unwrap();
25+
if *window_count == *map_count {
26+
have += 1;
27+
}
28+
}
29+
while have == need {
30+
if (rp - lp) as i32 + 1 < length {
31+
result = (lp as i32, rp as i32);
32+
length = (rp - lp + 1) as i32;
33+
}
34+
window.entry(s_chars[lp]).and_modify(|e| *e -= 1);
35+
if map.contains_key(&s_chars[lp]) {
36+
let window_count = window.get(&s_chars[lp]).unwrap();
37+
let map_count = map.get(&s_chars[lp]).unwrap();
38+
if window_count < map_count {
39+
have -= 1;
40+
}
41+
}
42+
lp += 1;
43+
}
44+
}
45+
if length != i32::MAX {
46+
s[result.0 as usize..result.1 as usize + 1].to_string()
47+
} else {
48+
"".to_string()
49+
}
50+
}
51+
}

‎src/permutation_in_string.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use std::collections::HashMap;
2+
pub struct Solution {}
3+
impl Solution {
4+
pub fn check_inclusion(s1: String, s2: String) -> bool {
5+
let mut result: bool = true;
6+
let mut s1_map: HashMap<char, i32> = HashMap::new();
7+
let mut s2_map: HashMap<char, i32> = HashMap::new();
8+
let s1_chars: Vec<char> = s1.chars().collect();
9+
let s2_chars: Vec<char> = s2.chars().collect();
10+
let alphabet = vec![
11+
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
12+
'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
13+
];
14+
for i in 0..s1.len() {
15+
s1_map
16+
.entry(s1_chars[i])
17+
.and_modify(|e| *e += 1)
18+
.or_insert(1);
19+
s2_map
20+
.entry(s2_chars[i])
21+
.and_modify(|e| *e += 1)
22+
.or_insert(1);
23+
}
24+
let mut matches = 0;
25+
for val in alphabet {
26+
if s1_map.contains_key(&val) && s2_map.contains_key(&val) {
27+
matches += 1;
28+
}
29+
}
30+
let mut lp = 0;
31+
for rp in s1_chars.len()..s2_chars.len() {
32+
if matches == 26 {
33+
return true;
34+
}
35+
if s1_map.entry(rp as i32) == s2_map.entry(rp as i32) {
36+
matches += 1;
37+
} else if s1_map.entry(rp as i32) + 1 == s2_map.entry(rp as i32) {
38+
matches -= 1;
39+
}
40+
}
41+
42+
result
43+
}
44+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /