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
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit 43512a4

Browse files
committed
Add problem 520
1 parent b618034 commit 43512a4

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

‎algorithms/520/README.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
## 520. Detect Capital
2+
3+
Given a word, you need to judge whether the usage of capitals in it is right or not.
4+
5+
We define the usage of capitals in a word to be right when one of the following cases holds:
6+
7+
* All letters in this word are capitals, like USA.
8+
* All letters in this word are not capitals, like leetcode.
9+
* Only the first letter in this word is capital, like Google.
10+
* Otherwise, we define that this word doesn't use capitals in a right way.
11+
12+
13+
**Example 1:**
14+
<pre>
15+
<b>Input:</b> USA
16+
<b>Output:</b> True
17+
</pre>
18+
19+
**Example 2:**
20+
<pre>
21+
<b>Input:</b> FlaG
22+
<b>Output:</b> False
23+
</pre>
24+
25+
**Note:** The input will be a non-empty word consisting of uppercase and lowercase latin letters.

‎algorithms/520/solution.go‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import "unicode"
4+
5+
type verifier func(rune) bool
6+
7+
// Verify every rune in string with the verifier
8+
func verify(s string, f verifier) bool {
9+
for _, r := range s {
10+
if !f(r) {
11+
return false
12+
}
13+
}
14+
return true
15+
}
16+
17+
// Time and space complexity, O(n) where n is length of the word
18+
func detectCapitalUse(word string) bool {
19+
if verify(word[:1], unicode.IsUpper) {
20+
return verify(word, unicode.IsUpper) || verify(word[1:], unicode.IsLower)
21+
} else {
22+
return verify(word, unicode.IsLower)
23+
}
24+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /