Skip to content

Commit 7a40c4d

Browse files
committed
leetcode
1 parent c7245f7 commit 7a40c4d

File tree

4 files changed

+225
-0
lines changed

4 files changed

+225
-0
lines changed

DetectCapital/detect_capital.dart

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
3+
-* 520. Detect Capital *-
4+
5+
6+
We define the usage of capitals in a word to be right when one of the following cases holds:
7+
8+
All letters in this word are capitals, like "USA".
9+
All letters in this word are not capitals, like "leet-code".
10+
Only the first letter in this word is capital, like "Google".
11+
Given a string word, return true if the usage of capitals in it is right.
12+
13+
14+
15+
Example 1:
16+
17+
Input: word = "USA"
18+
Output: true
19+
Example 2:
20+
21+
Input: word = "FlaG"
22+
Output: false
23+
24+
25+
Constraints:
26+
27+
1 <= word.length <= 100
28+
word consists of lowercase and uppercase English letters.
29+
30+
31+
*/
32+
33+
extension Equals on String {
34+
bool equals(Object anObject) {
35+
if (identical(this, anObject)) {
36+
return true;
37+
}
38+
if (anObject is String) {
39+
String anotherString = anObject;
40+
int n = codeUnits.length;
41+
if (n == anotherString.codeUnits.length) {
42+
List<int> v1 = codeUnits;
43+
List<int> v2 = anotherString.codeUnits;
44+
int i = 0;
45+
while (n-- != 0) {
46+
if (v1[i] != v2[i]) {
47+
return false;
48+
}
49+
i++;
50+
}
51+
return true;
52+
}
53+
}
54+
return false;
55+
}
56+
}
57+
58+
class A {
59+
bool detectCapitalUse(String word) {
60+
return word.toUpperCase().equals(word) ||
61+
word.toLowerCase().equals(word) ||
62+
word.codeUnitAt(0) == word.toUpperCase() &&
63+
word
64+
.substring(1, word.length)
65+
.toLowerCase()
66+
.equals(word.substring(1, word.length));
67+
}
68+
}
69+
70+
class B {
71+
bool detectCapitalUse(String word) {
72+
int count = 0;
73+
for (String char in word.split(''))
74+
if ('Z'.codeUnitAt(0) - char.codeUnitAt(0) >= 0) count++;
75+
return ((count == 0 || count == word.length) ||
76+
(count == 1 && 'Z'.codeUnitAt(0) - word.codeUnitAt(0) >= 0));
77+
}
78+
}
79+
80+
class C {
81+
bool detectCapitalUse(String word) {
82+
if (word == "") return false;
83+
if (word.length == 1) return true;
84+
if (word.codeUnitAt(0) >= 'a'.codeUnitAt(0) &&
85+
word.codeUnitAt(1) < 'a'.codeUnitAt(0)) return false;
86+
for (int i = 2; i < word.length; i++) {
87+
if ((word.codeUnitAt(1) - 'a'.codeUnitAt(0) + 1) *
88+
(word.codeUnitAt(i) - 'a'.codeUnitAt(0) + 1) <
89+
0) {
90+
return false;
91+
}
92+
}
93+
return true;
94+
}
95+
}

DetectCapital/detect_capital.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package main
2+
3+
import "strings"
4+
5+
// 1. using strings' build-in func
6+
func detectCapitalUse(word string) bool {
7+
return word == strings.ToLower(word) || word == strings.ToUpper(word) || word == strings.Title(strings.ToLower(word))
8+
}
9+
10+
// 2. set condition for judging
11+
func isCapital(letter int32) bool {
12+
return letter <= 'Z' && letter >= 'A'
13+
}
14+
15+
/*
16+
func detectCapitalUse(word string) bool {
17+
var condition = 0
18+
19+
for index, letter := range word {
20+
switch isCapital(letter) {
21+
case true:
22+
if condition == 0 {
23+
condition = 1
24+
continue
25+
} else if condition == 1 {
26+
continue
27+
} else if condition == 2 || condition == 3 {
28+
return false
29+
}
30+
case false:
31+
if condition == 0 {
32+
condition = 2
33+
} else if condition == 1{
34+
if index == 1 {
35+
condition = 3
36+
} else {
37+
return false
38+
}
39+
} else if condition == 2 || condition == 3{
40+
continue
41+
}
42+
}
43+
}
44+
45+
return true
46+
}
47+
48+
*/

DetectCapital/detect_capital.md

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# 🔥 Detect Capital 🔥 || 3 Approaches || Simple Fast and Easy || with Explanation
2+
3+
## Solution - 1
4+
5+
### Extension to check the Equality between two Objects
6+
7+
```dart
8+
extension Equals on String {
9+
bool equals(Object anObject) {
10+
if (identical(this, anObject)) {
11+
return true;
12+
}
13+
if (anObject is String) {
14+
String anotherString = anObject;
15+
int n = codeUnits.length;
16+
if (n == anotherString.codeUnits.length) {
17+
List<int> v1 = codeUnits;
18+
List<int> v2 = anotherString.codeUnits;
19+
int i = 0;
20+
while (n-- != 0) {
21+
if (v1[i] != v2[i]) {
22+
return false;
23+
}
24+
i++;
25+
}
26+
return true;
27+
}
28+
}
29+
return false;
30+
}
31+
}
32+
```
33+
34+
```dart
35+
class Solution {
36+
bool detectCapitalUse(String word) {
37+
return word.toUpperCase().equals(word) ||
38+
word.toLowerCase().equals(word) ||
39+
word.codeUnitAt(0) != word.toUpperCase() &&
40+
word
41+
.substring(1, word.length)
42+
.toLowerCase()
43+
.equals(word.substring(1, word.length));
44+
}
45+
}
46+
```
47+
48+
## Solution - 2
49+
50+
```dart
51+
class Solution {
52+
bool detectCapitalUse(String word) {
53+
int count = 0;
54+
for (String char in word.split(''))
55+
if ('Z'.codeUnitAt(0) - char.codeUnitAt(0) >= 0) count++;
56+
return ((count == 0 || count == word.length) ||
57+
(count == 1 && 'Z'.codeUnitAt(0) - word.codeUnitAt(0) >= 0));
58+
}
59+
}
60+
```
61+
62+
## Solution - 3
63+
64+
```dart
65+
class Solution {
66+
bool detectCapitalUse(String word) {
67+
if (word == "") return false;
68+
if (word.length == 1) return true;
69+
if (word.codeUnitAt(0) >= 'a'.codeUnitAt(0) &&
70+
word.codeUnitAt(1) < 'a'.codeUnitAt(0)) return false;
71+
for (int i = 2; i < word.length; i++) {
72+
if ((word.codeUnitAt(1) - 'a'.codeUnitAt(0) + 1) *
73+
(word.codeUnitAt(i) - 'a'.codeUnitAt(0) + 1) <
74+
0) {
75+
return false;
76+
}
77+
}
78+
return true;
79+
}
80+
}
81+
```

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
174174
- [**841.** Keys and Rooms](KeysAndRooms/keys_and_rooms.dart)
175175
- [**886.** Possible Bipartition](PossibleBipartition/possible_bipartition.dart)
176176
- [**980.** Unique Paths III](UniquePathsIII/unique_paths_iii.dart)
177+
- [**520.** Detect Capital](DetectCapital/detect_capital.dart)
177178

178179
## Reach me via
179180

0 commit comments

Comments
 (0)