“This is the 20th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”


📢 preface

🚀 Algorithm 🚀
  • 🌲 punch in an algorithm every day, which is not only a learning process, but also a sharing process 😜
  • 🌲 tip: the programming languages used in this column are C# and Java
  • 🌲 to maintain a state of learning every day, let us work together to become a god of algorithms 🧐!
  • 🌲 today is the 80th day 🎈!
🚀 Algorithm 🚀

🌲 Check uppercase letters

We define that capitalizing a word is correct when:

  • All letters are uppercase, like “USA”.
  • All letters in words are not capitalized, such as “leetcode”.
  • If the word contains more than one letter, capitalize only the first letter, such as “Google”.

I give you a string word. Returns true if uppercase is used correctly; Otherwise, return false.

Example 1:

Enter: word ="USA"Output:true
Copy the code

Example 2:

Enter: word ="FlaG"Output:false
Copy the code

Tip:

  • 1 <= word.length <= 100
  • Word consists of lowercase and uppercase letters

🌻C# method: sort traversal

If the word is capitalized correctly, you must:

  • If the first letter is uppercase, all other letters must be uppercase or lowercase, that is, all other letters must have the same case as the second letter.
  • If the first letter is lowercase, all other letters must be lowercase.

According to the above rules, the following simpler judgment rules can be sorted out:

  • Whether the first letter is capitalized or not, all other letters must have the same case as the second letter;
  • If the first letter is lowercase, check whether the second letter is lowercase.

Code:

public class Solution {
    public bool DetectCapitalUse(string word) {
        // If the first letter is lower case, check whether the second letter is lower case
        if (word.Length >= 2 && char.IsLower(word[0&&])char.IsUpper(word[1]) {return false;
        }
        
        // Whether the first letter is capitalized or not, all other letters must have the same case as the second letter
        for (int i = 2; i < word.Length; ++i) {
            if (char.IsLower(word[i]) ^ char.IsLower(word[1]) {return false; }}return true; }}Copy the code

The execution result

By execution time:108Ms, in all CBeat 8.50% of users in # submissionMemory consumption:37.4MB, in all C# beat 67.90% of users in submission
Copy the code

🌻Java methods: count

If you use uppercase words correctly, you must:

  • If the first letter is uppercase, all other letters must be uppercase or lowercase, that is, all other letters must have the same case as the second letter.
  • If the first letter is lowercase, all other letters must be lowercase.

According to the above rules, the following simpler judgment rules can be sorted out:

  • Whether the first letter is capitalized or not, all other letters must have the same case as the second letter;
  • If the first letter is lowercase, check whether the second letter is lowercase.

Code:

class Solution {
    public boolean detectCapitalUse(String word) {
        // If the first letter is lower case, check whether the second letter is lower case
        if (word.length() >= 2 && Character.isLowerCase(word.charAt(0)) && Character.isUpperCase(word.charAt(1))) {
            return false;
        }
        
        // Whether the first letter is capitalized or not, all other letters must have the same case as the second letter
        for (int i = 2; i < word.length(); ++i) {
            if (Character.isLowerCase(word.charAt(i)) ^ Character.isLowerCase(word.charAt(1))) {
                return false; }}return true; }}Copy the code

The execution result

By execution time:1Ms, beat out all Java commits94.76% user memory consumption:36.8MB, beat out all Java commits26.40% of the userCopy the code

Complexity analysis

Time: O(n) Space: O(1) 
Copy the code

💬 summary

  • Today is the 80th day of buckle algorithm clocking!
  • The article USES theC# andJavaTwo programming languages to solve the problem
  • Some methods are also written by the god of reference force buckle, and they are also shared while learning, thanks again to the algorithm masters
  • That’s the end of today’s algorithm sharing, see you tomorrow!