hi, let Avik connect you.
** You can give your thought about this content and request modifications if needed from Ask For Modifications page. Thank You.
** You can check all the posts on the Posts page.
** More about me in the About Me section.
Detect Capital
Problem: 520. Detect Capital
Problem Link: leetcode.com/problems/detect-capital/
Companies Asked: Google, Amazon
Approach:
Traverse through the given string.
Count the capital letters.
Return true, if the count becomes 1 and the 0th character is the Captial or the count becomes equal to the length of the string, or the count becomes 0.
Else return false.
Time and Space Complexity:
Time Complexity: O(N)
Space Complexity: O(1)
Code [C++]
class Solution {
public:
bool detectCapitalUse(string word) {
int cap_count = 0;
int n = word.size();
for(int i=0; i<n; i++){
if(word[i] >= 'A' && word[i] <= 'Z') cap_count++;
}
if(cap_count == n || cap_count == 0) return true;
if(cap_count == 1 && (word[0]>='A' && word[0]<='Z')) return true;
return false;
}
};