Skip to content

Commit 0c3bed2

Browse files
committed
[Create] LeetCode_925 Long Pressed Name
1 parent 757f548 commit 0c3bed2

1 file changed

Lines changed: 83 additions & 0 deletions

File tree

LongPressedName.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// LeetCode_925
2+
// 2021.04.28
3+
// Easy
4+
5+
import java.util.Stack;
6+
7+
public class LongPressedName {
8+
public static void main(String[] args) {
9+
System.out.println(new LongPressedName().isLongPressedName("saeed", "ssaaedd"));
10+
}
11+
public boolean isLongPressedName(String name, String typed) {
12+
StringBuilder str = new StringBuilder(name);
13+
StringBuilder typ = new StringBuilder(typed);
14+
int strIdx = 0, typIdx = 0;
15+
boolean flag = true;
16+
17+
Stack<Character> stk = new Stack<>();
18+
19+
while(typIdx != typed.length()){
20+
if(stk.empty()) {
21+
stk.push(str.charAt(strIdx));
22+
strIdx++;
23+
}
24+
25+
if(typ.charAt(typIdx) == stk.peek()){
26+
typIdx++;
27+
}else {
28+
try {
29+
stk.push(str.charAt(strIdx));
30+
strIdx++;
31+
32+
if(typ.charAt(typIdx) == stk.peek()){
33+
typIdx++;
34+
}else {
35+
flag = false;
36+
break;
37+
}
38+
}catch (IndexOutOfBoundsException e){
39+
break;
40+
}
41+
42+
}
43+
}
44+
return flag;
45+
}
46+
47+
48+
}
49+
50+
/* USE_TWOPointer 출처: leetcode
51+
class Solution {
52+
public boolean isLongPressedName(String name, String typed) {
53+
int np = 0, tp = 0;
54+
char[] name_chars = name.toCharArray();
55+
char[] typed_chars = typed.toCharArray();
56+
57+
58+
while (np < name_chars.length && tp < typed_chars.length) {
59+
if (name_chars[np] == typed_chars[tp]) {
60+
np += 1;
61+
tp += 1;
62+
} else if (tp >= 1 && typed_chars[tp] == typed_chars[tp - 1]) {
63+
tp += 1;
64+
} else {
65+
return false;
66+
}
67+
}
68+
69+
if (np != name_chars.length) {
70+
return false;
71+
} else {
72+
73+
while (tp < typed_chars.length) {
74+
if (typed_chars[tp] != typed_chars[tp - 1])
75+
return false;
76+
tp += 1;
77+
}
78+
}
79+
80+
return true;
81+
}
82+
}
83+
*/

0 commit comments

Comments
 (0)