-
Notifications
You must be signed in to change notification settings - Fork 1
/
LongPressedName.java
34 lines (29 loc) · 966 Bytes
/
LongPressedName.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.smlnskgmail.jaman.leetcodejava.easy;
// https://leetcode.com/problems/long-pressed-name/
public class LongPressedName {
private final String name;
private final String typed;
public LongPressedName(String name, String typed) {
this.name = name;
this.typed = typed;
}
public boolean solution() {
int namePointer = 0;
int typedPointer = 0;
int nameLength = name.length();
int typedLength = typed.length();
while (namePointer < nameLength && typedPointer < typedLength) {
char n = name.charAt(namePointer);
char t = typed.charAt(typedPointer);
if (n == t) {
namePointer++;
typedPointer++;
} else if (typedPointer >= 1 && t == typed.charAt(typedPointer - 1)) {
typedPointer++;
} else {
return false;
}
}
return true;
}
}