[case of algorithm thousand questions] practice LeetCode punch in once a day - 101 Long press and type

Posted by khujo56 on Fri, 21 Jan 2022 16:39:11 +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 101st day of the continuous clock out of the force deduction algorithm 🎈!
πŸš€ Algorithm problem πŸš€

🌲 Example of original question: long press and type

Your friend is using the keyboard to enter his name. Occasionally, when typing the character c, the key may be pressed for a long time, and the character may be entered one or more times.

You will check the character typed on the keyboard. If it may correspond to your friend's name (some characters may be long pressed), it returns True.

Example 1:

Input: name = "alex", typed = "aaleex"
Output: true
 Explanation:'alex' Medium 'a' and 'e' Long pressed.

Example 2:

Input: name = "saeed", typed = "ssaaedd"
Output: false
 Explanation:'e' It must be typed twice, but in typed This is not the case in the output of.

Example 3:

Input: name = "leelee", typed = "lleeelee"
Output: true

Example 4:

Input: name = "laiden", typed = "laiden"
Output: true
 Explanation: it is not necessary to long press the characters in the name.

Tips:

  • name.length <= 1000
  • typed.length <= 1000
  • Both name and typed characters are lowercase letters.

🌻 C# method: loop traversal

If the same, compare the next pair of letters.

If not, if the letter on type D is the same as the previous letter on name, long press the letter to skip.

If not, return false.

code:

public class Solution {
    public bool IsLongPressedName(string name, string typed) 
    {
        int i = 0;
            for (int j = 0; j < typed.Length; j++)
            {
                if (i < name.Length && name[i] == typed[j])//The same compares the next character
                {
                    i++;
                }
                else if (i>0 && name[i - 1] == typed[j])
                {
                    continue;
                }
                else
                {
                    return false;
                }
            }
            if (i != name.Length)//Input is shorter than name
                return false;
            else
                return true;
    }
}

results of enforcement

adopt
 Execution time: 76 ms,At all C# Beat 66.14% of users in submission
 Memory consumption: 36.9 MB,At all C# Beat 5.70% of users in submission

🌻 Java method: double pointer

Train of thought analysis
According to the meaning of the question, it can be concluded that each character of string typed has and only has two "uses":

As part of name. A character in name is "matched"

As part of the long key in. At this point, it should be the same as the previous character.

If there is a character in the typed and neither of its two conditions is satisfied, it should directly return false;
Otherwise, after the typed scan is completed, we will check whether each character of name is "matched".

In implementation, we use two subscripts I and j to track the position of name and typed.

  • When name[i]=typed[j], it indicates that there is a pair of matching characters in the two strings. At this time, add 1 to both I and J.
  • Otherwise, if typed[j]=typed[j − 1], it indicates that there is a long press to type, and only j plus 1 will be added at this time.

Finally, if I = name Length, indicating that each character of name is "matched".

code:

class Solution {
    public boolean isLongPressedName(String name, String typed) {
        int i = 0, j = 0;
        while (j < typed.length()) {
            if (i < name.length() && name.charAt(i) == typed.charAt(j)) {
                i++;
                j++;
            } else if (j > 0 && typed.charAt(j) == typed.charAt(j - 1)) {
                j++;
            } else {
                return false;
            }
        }
        return i == name.length();
    }
}

results of enforcement

adopt
 Execution time: 1 ms,At all Java  Defeated 76 in submission.41%User
 Memory consumption: 36.4 MB,At all Java Defeated 23 in submission.53%User

Complexity analysis

Time complexity: O( M+N )among M,N Is the length of two strings.
Space complexity: O(1) 

πŸ’¬ summary

  • Today is the 101st 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