File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments