[case of algorithm thousand questions] daily LeetCode punch in - 79. Keyboard line

Posted by qwave on Wed, 17 Nov 2021 21:20:00 +0100

πŸ“’ preface

πŸš€ Algorithm problem πŸš€
  • 🌲 Punching out an algorithm problem every day is not only a learning process, but also a sharing process 😜
  • 🌲 Tip: the problem-solving programming languages in this column are C# and Java
  • 🌲 To maintain a state of learning every day, let's work together to become the great God of algorithm 🧐!
  • 🌲 Today is the 79th day of punching out the force deduction algorithm 🎈!
πŸš€ Algorithm problem πŸš€

🌲 Example of original question: keyboard line

Give you a string array words, which only returns words that can be printed with letters on the same line of the American keyboard. The keyboard is shown in the figure below.

American keyboard:

  • The first line consists of the character "qwertyuiop".
  • The second line consists of the character "asdfghjkl".
  • The third line consists of the character "zxcvbnm".

Example 1:

Input: words = ["Hello","Alaska","Dad","Peace"]
Output:["Alaska","Dad"]

Example 2:

Input: words = ["omk"]
Output:[]

Example 3:

Input: words = ["adsdf","sfd"]
Output:["adsdf","sfd"]

Tips:

  • 1 <= words.length <= 20
  • 1 <= words[i].length <= 100
  • words[i] consists of English letters (lowercase and uppercase)

🌻 C# method: sort traversal

We mark the line number on the corresponding keyboard for each English letter, and then detect whether the line numbers corresponding to all characters in the string are the same.

  • We can preprocess and calculate the line number corresponding to each character.

  • When traversing the string, the uppercase letters are uniformly converted into lowercase letters for easy calculation.

code:

public class Solution {
    public string[] FindWords(string[] words) {
        IList<string> list = new List<string>();
        string rowIdx = "12210111011122000010020202";
        foreach (string word in words) {
            bool isValid = true;
            char idx = rowIdx[char.ToLower(word[0]) - 'a'];
            for (int i = 1; i < word.Length; ++i) {
                if (rowIdx[char.ToLower(word[i]) - 'a'] != idx) {
                    isValid = false;
                    break;
                }
            }
            if (isValid) {
                list.Add(word);
            }
        }

        string[] ans = new string[list.Count];
        for (int i = 0; i < list.Count; ++i) {
            ans[i] = list[i];
        }
        return ans;
    }
}

results of enforcement

adopt
 Execution time: 152 ms,At all C# Defeated 49.50% of users in submission
 Memory consumption: 41.4 MB,At all C# Defeated 24.90% of users in submission

🌻 Java methods: counting

Train of thought analysis
We mark the line number on the corresponding keyboard for each English letter, and then detect whether the line numbers corresponding to all characters in the string are the same.

  • We can preprocess and calculate the line number corresponding to each character.

  • When traversing the string, the uppercase letters are uniformly converted into lowercase letters for easy calculation.

code:

class Solution {    
    public String[] findWords(String[] words) {
        List<String> list = new ArrayList<String>();
        String rowIdx = "12210111011122000010020202";
        for (String word : words) {
            boolean isValid = true;
            char idx = rowIdx.charAt(Character.toLowerCase(word.charAt(0)) - 'a');
            for (int i = 1; i < word.length(); ++i) {
                if (rowIdx.charAt(Character.toLowerCase(word.charAt(i)) - 'a') != idx) {
                    isValid = false;
                    break;
                }
            }
            if (isValid) {
                list.add(word);
            }
        }
        String[] ans = new String[list.size()];
        for (int i = 0; i < list.size(); ++i) {
            ans[i] = list.get(i);
        }
        return ans;
    }
}

results of enforcement

adopt
 Execution time: 0 ms,At all Java  Defeated 100 in submission.76%User
 Memory consumption: 36.4 MB,At all Java Defeated 89 in submission.40%User

πŸ’¬ summary

  • Today is the 79th day of punching out the force deduction algorithm!
  • This paper uses C# and Java programming languages to solve problems
  • Some methods are also written by Likou God, and they are also shared while learning. Thanks again to the algorithm bosses
  • That's the end of today's algorithm sharing. See you tomorrow!

Topics: Algorithm leetcode