Javascript Function to Detect Capital String

  • Time:2020-09-07 13:13:13
  • Class:Weblog
  • Read:30

Given a word, you need to judge whether the usage of capitals in it is right or not. 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”.
Otherwise, we define that this word doesn’t use capitals in a right way.

Example 1:
Input: “USA”
Output: True

Example 2:
Input: “FlaG”
Output: False

Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters.

Detect Capital String using Javascript Function

One intutive way is to count the number of Captial letters in the given string. Then it is a valid capital string if: the count is zero (no captial letters), or count is equal to the length (all are captial letters), or count is one and the first character is uppercase.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
 * @param {string} word
 * @return {boolean}
 */
var detectCapitalUse = function(word) {
    const len = word.length;
    if (len === 0) return false;
    let cnt = 0;
    let first = false;
    for (let i = 0; i < len; ++ i) {
        const ch = word.charAt(i);
        if ((ch >= 'A') && (ch <= 'Z')) {
            cnt ++;
            if (i == 0) first = true;
        }
    }
    return (cnt === 0) || ((cnt === 1) && first) || (cnt === len);
};
/**
 * @param {string} word
 * @return {boolean}
 */
var detectCapitalUse = function(word) {
    const len = word.length;
    if (len === 0) return false;
    let cnt = 0;
    let first = false;
    for (let i = 0; i < len; ++ i) {
        const ch = word.charAt(i);
        if ((ch >= 'A') && (ch <= 'Z')) {
            cnt ++;
            if (i == 0) first = true;
        }
    }
    return (cnt === 0) || ((cnt === 1) && first) || (cnt === len);
};

Using RegExp to Detect Capital String

Using Regular Expression in Javascript helps to make the solution one-line and concise:

1
2
3
4
5
6
7
/**
 * @param {string} word
 * @return {boolean}
 */
var detectCapitalUse = function(word) {
    return /^([A-Z]+|[A-Z][a-z]*|[a-z]+)$/.test(word);
};
/**
 * @param {string} word
 * @return {boolean}
 */
var detectCapitalUse = function(word) {
    return /^([A-Z]+|[A-Z][a-z]*|[a-z]+)$/.test(word);
};

–EOF (The Ultimate Computing & Technology Blog) —

Recommend:
Bruteforce/DFS/Backtracking Algorithm using Javascript to Solve
DFS and BFS Algorithm to Find Numbers With Same Consecutive Diff
How to Remove Items/Entries with Specific Values from Map/HashMa
Find the Real Root of 4^x + 6^x = 9^x
Depth First Search (Backtracking) Algorithm to Solve a Sudoku Ga
Using Bitmasking Algorithm to Compute the Combinations of an Arr
Flashing the BIOS of HPZ800 Server to 3.61 Rev.A
Algorithm to Sum The Fibonacci Numbers
How to Adapt Your Blog to Increasing Zero-Click Searches
Understanding Marketing Automation & Its Perks for Your Busi
Share:Facebook Twitter
Comment list
Comment add