LeetCode925. Long key input (Java+loop)

Your friend is using the keyboard to enter his name. Occasionally, when typing the character c, the key may be long-pressed, and the character may be entered one or more times.
You will check the typed characters entered on the keyboard. If it corresponds to your friend's name (some characters may be long-pressed), then return True.
Example 1:
Input: name = "alex", typed = "aaleex"
Output: true
Explanation: The'a' and'e' in'alex ' are long pressed.
Example 2:
Input: name = "saeed", typed = "ssaaedd"
Output: false
Explanation:'e' must be typed twice, but this is not the case in the output of typed.

Topic analysis

The typed string may be formed by inputting multiple characters in the name.
1. If the characters in the typed string are not in the name, it is false.
2. The content of typed and name is the same and the length is equal, indicating true.
We can count the number of consecutive occurrences of each character in the name cnt, and traverse the typed string to see if the character is in the typed string, and then count how many consecutive characters there are. If the statistical result is >=cnt, then true, Otherwise, it is false.

Q1: How to traverse the string and find the number of consecutive occurrences of each character?

public void countNum(String name) {
    
    
		for (int i = 0; i < name.length(); i++) {
    
    
			int j = i + 1;// 从0开始,依次往后找
			int cnt = 1;
			// cnt设为1是因为,如果该字符是连续的,则cnt++,cnt就为2,表示2个相同字符
			// 如果不连续,那该字符出现1次
			while (j < name.length() && name.charAt(i) == name.charAt(j)) {
    
    
				j++;// 如果是连续的,继续往后,看有多少个连续的
				cnt++;
			}
			// while循环出来的j是下一个即将参与比较的字符,而循环体一次循环之后会i++
			// 所以i=j-1,让i回退一步,从而i++之后就是下一个即将参与比较的字符
			i = j - 1;
			System.out.println(name.charAt(i) +" "+ cnt);
		}
	}

Results:
Insert picture description here
Q2: For typed strings, is the above method used to count the number of consecutive characters?
Yes, but it is not necessary.
We set an index and record the typed subscript. The index starts from 0, through the while loop: typed.charAt(index)==name.charAt(i), to determine whether the typed element matches the name element, then index++ is matched, and continue to look for the same in the typed string character of. At this time, also set cnt2=0, which is used to calculate the number of consecutive characters in the typed string. If cnt2>=cnt, it means long key press, otherwise it returns false.

But there are many details that have not been considered:
1. Boundary issues: If the name string has been traversed, and the typed has not been traversed yet, it means that there are other characters that are not in the name.
2. If the name has not been traversed, and the index has crossed the boundary, it means that the typed string is not enough for comparison.
3. Only when typed.charAt(index)==name.charAt(i) can the circular comparison and calculation be performed, if typed.charAt(index)! =name.charAt(i), indicating that the characters do not match, do not enter the loop, and return false directly.

Complete code

public class LongPressedName {
    
    
	public static void main(String[] args) {
    
    
		LongPressedName test = new LongPressedName();
		boolean flag = test.isLongPressedName("alex", "alexxr");
		System.out.println(flag);
	}

	public boolean isLongPressedName(String name, String typed) {
    
    
		if (name.equals(typed))
			return true;
		int index = 0;
		for (int i = 0; i < name.length(); i++) {
    
    
			int j = i + 1;
			int cnt = 1;
			while (j < name.length() && name.charAt(i) == name.charAt(j)) {
    
    
				j++;
				cnt++;
			}
			i = j - 1;
			if (index >= typed.length() || typed.charAt(index) != name.charAt(i)) {
    
    
				return false;
			}
			int cnt2 = 0;
			while (index < typed.length() && typed.charAt(index) == name.charAt(i)) {
    
    
				index++;
				cnt2++;
			}
			if (cnt2 < cnt) {
    
    
				return false;
			}
		}
		if (index < typed.length()) {
    
    
			return false;
		}
		return true;
	}
}

Reflection

Learned:
(1) How to count the number of consecutive characters in a string.
(2) Consider the boundary conditions
end.

Guess you like

Origin blog.csdn.net/weixin_44998686/article/details/109203761