Skip to content

Commit 46e643a

Browse files
committed
Create 520-detect-capital.rs
1 parent 08122ae commit 46e643a

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

520-detect-capital.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
pub fn detect_capital_use(word: String) -> bool {
2+
let word_len = word.len();
3+
let mut count = vec![0, 0];
4+
let chars = word.chars().collect::<Vec<char>>();
5+
let first_char = chars[0];
6+
for c in chars {
7+
if c.is_uppercase() {
8+
count[0] += 1
9+
} else {
10+
count[1] += 1
11+
}
12+
}
13+
if count[0] == 0 {
14+
true
15+
} else {
16+
if count[1] == 0 {
17+
true
18+
} else {
19+
count[1] == word_len - 1 && first_char.is_uppercase()
20+
}
21+
}
22+
}
23+
24+
fn main() {
25+
println!("{:?}", detect_capital_use("USA".to_string()));
26+
println!("{:?}", detect_capital_use("leetcode".to_string()));
27+
println!("{:?}", detect_capital_use("Google".to_string()));
28+
println!("{:?}", detect_capital_use("FlaG".to_string()));
29+
println!("{:?}", detect_capital_use("ffffffffffffffffffffF".to_string()));
30+
}

0 commit comments

Comments
 (0)