Movatterモバイル変換


[0]ホーム

URL:


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

Commit3e96af6

Browse files
committed
min window
1 parentb388fc4 commit3e96af6

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-
modlongest_repeating_character;
2-
uselongest_repeating_character::Solution;
1+
modminimum_window_substring;
2+
useminimum_window_substring::Solution;
33
fnmain(){
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+
pubstructSolution{}
3+
implSolution{
4+
pubfnmin_window(s:String,t:String) ->String{
5+
if t.len() ==0 || t.len() > s.len(){
6+
return"".to_string();
7+
}
8+
letmut map:HashMap<char,i32> =HashMap::new();
9+
letmut window:HashMap<char,i32> =HashMap::new();
10+
let s_chars:Vec<char> = s.chars().collect();
11+
for cin t.chars(){
12+
map.entry(c).and_modify(|e|*e +=1).or_insert(1);
13+
}
14+
letmut have =0;
15+
let need = map.len();
16+
letmut result:(i32,i32) =(-1, -1);
17+
letmut length = i32::MAX;
18+
letmut lp =0;
19+
for rpin0..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)asi32 +1 < length{
31+
result =(lpasi32, rpasi32);
32+
length =(rp - lp +1)asi32;
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.0asusize..result.1asusize +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+
pubstructSolution{}
3+
implSolution{
4+
pubfncheck_inclusion(s1:String,s2:String) ->bool{
5+
letmut result:bool =true;
6+
letmut s1_map:HashMap<char,i32> =HashMap::new();
7+
letmut 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 iin0..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+
letmut matches =0;
25+
for valin alphabet{
26+
if s1_map.contains_key(&val) && s2_map.contains_key(&val){
27+
matches +=1;
28+
}
29+
}
30+
letmut lp =0;
31+
for rpin s1_chars.len()..s2_chars.len(){
32+
if matches ==26{
33+
returntrue;
34+
}
35+
if s1_map.entry(rpasi32) == s2_map.entry(rpasi32){
36+
matches +=1;
37+
}elseif s1_map.entry(rpasi32) +1 == s2_map.entry(rpasi32){
38+
matches -=1;
39+
}
40+
}
41+
42+
result
43+
}
44+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp