You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solution/0500-0599/0520.Detect Capital/README_EN.md
+19-9Lines changed: 19 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -34,17 +34,22 @@
34
34
35
35
## Solutions
36
36
37
-
### Solution 1
37
+
### Solution 1: Count the Number of Uppercase Letters
38
+
39
+
We can count the number of uppercase letters in the string, and then determine whether it meets the requirements of the problem based on the number of uppercase letters.
40
+
41
+
- If the number of uppercase letters is 0 or equal to the length of the string, then return `true`.
42
+
- If the number of uppercase letters is 1 and the first letter is an uppercase letter, then return `true`.
43
+
- Otherwise, return `false`.
44
+
45
+
The time complexity is $O(n)$, where $n$ is the length of the string `word`. The space complexity is $O(1)$.
38
46
39
47
<!-- tabs:start -->
40
48
41
49
```python
42
50
classSolution:
43
51
defdetectCapitalUse(self, word: str) -> bool:
44
-
cnt =0
45
-
for c in word:
46
-
if c.isupper():
47
-
cnt +=1
52
+
cnt =sum(c.isupper() for c in word)
48
53
return cnt ==0or cnt ==len(word) or (cnt ==1and word[0].isupper())
0 commit comments