forked from coder2hacker/Explore-open-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDetect Capital
42 lines (38 loc) · 1.21 KB
/
Detect Capital
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Detect Capital
Level-Easy
We define the usage of capitals in a word to be right when one of the following cases holds:
All letters in this word are capitals, like "USA".
All letters in this word are not capitals, like "leetcode".
Only the first letter in this word is capital, like "Google".
Given a string word, return true if the usage of capitals in it is right.
Example 1:
Input: word = "USA"
Output: true
Example 2:
Input: word = "FlaG"
Output: false
Constraints:
1 <= word.length <= 100
word consists of lowercase and uppercase English letters.
Time Complexity-o(n)
Space Complexity-o(1)
Java Solution
class Solution {
public boolean detectCapitalUse(String word) {
if(word.length()>=2 && word.charAt(0)>=97 && word.charAt(0)<=123 && word.charAt(1)>=65 && word.charAt(1)<=91)
return false;
for(int i=1;i<word.length()-1;i++){
if(word.charAt(i)>=65 && word.charAt(i)<=91){
if(word.charAt(i+1)>=65 && word.charAt(i+1)<=91);
else
return false;
}
else{
if(word.charAt(i+1)>=97 && word.charAt(i+1)<=123);
else
return false;
}
}
return true;
}
}