
Hi! My name is Nhut Nguyen. I am a software engineer and a writer in Copenhagen, Denmark.
Learn more about me at nhutnguyen.com
Search for a command to run...

Hi! My name is Nhut Nguyen. I am a software engineer and a writer in Copenhagen, Denmark.
Learn more about me at nhutnguyen.com
No comments yet. Be the first to comment.
A simple example of using C++ switch

Two dynamic programming techniques to solve Leetcode 120. Triangle. One has space complexity O(n^2). The other is O(n).

An example of using a sliding window approach and an unordered map to track character positions

A simple C++ solution to Leetcode 1695. Maximum Erasure Value using a sliding window approach and prefix sums.

Strategies to avoid them will help you excel in your following technical interview

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.
Input: word = "USA"
Output: true
Input: word = "FlaG"
Output: false
1 <= word.length <= 100,
word consists of lowercase and uppercase English letters.
Only when the first two characters of the word are uppercase, the rest must be the same. Otherwise, the rest is always lowercase.
#include <string>
#include <iostream>
using namespace std;
bool isValidCase(const char& c, const bool isLower) {
if (isLower) {
return 'a' <= c && c <= 'z';
}
return 'A' <= c && c <= 'Z';
}
bool detectCapitalUse(string word) {
if (word.length() == 1) {
return true;
}
bool isLower = true;
if (isValidCase(word[0], false) && isValidCase(word[1], false)) {
isLower = false;
}
for (int i = 1; i < word.length(); i++) {
if (!isValidCase(word[i], isLower)) {
return false;
}
}
return true;
}
int main() {
cout << detectCapitalUse("USA") << endl;
cout << detectCapitalUse("FlaG") << endl;
cout << detectCapitalUse("leetcode") << endl;
cout << detectCapitalUse("Google") << endl;
}
Output:
1
0
1
1
Runtime: O(N), where N = word.length.
Extra space: O(1).
Thanks for reading. Feel free to share your thought about my content and check out my FREE book “10 Classic Coding Challenges”.