Skip to content

Commit

Permalink
[Create] LeetCode_925 Long Pressed Name
Browse files Browse the repository at this point in the history
  • Loading branch information
holmir97 committed Apr 28, 2021
1 parent 757f548 commit 0c3bed2
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions LongPressedName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// LeetCode_925
// 2021.04.28
// Easy

import java.util.Stack;

public class LongPressedName {
public static void main(String[] args) {
System.out.println(new LongPressedName().isLongPressedName("saeed", "ssaaedd"));
}
public boolean isLongPressedName(String name, String typed) {
StringBuilder str = new StringBuilder(name);
StringBuilder typ = new StringBuilder(typed);
int strIdx = 0, typIdx = 0;
boolean flag = true;

Stack<Character> stk = new Stack<>();

while(typIdx != typed.length()){
if(stk.empty()) {
stk.push(str.charAt(strIdx));
strIdx++;
}

if(typ.charAt(typIdx) == stk.peek()){
typIdx++;
}else {
try {
stk.push(str.charAt(strIdx));
strIdx++;

if(typ.charAt(typIdx) == stk.peek()){
typIdx++;
}else {
flag = false;
break;
}
}catch (IndexOutOfBoundsException e){
break;
}

}
}
return flag;
}


}

/* USE_TWOPointer 출처: leetcode
class Solution {
public boolean isLongPressedName(String name, String typed) {
int np = 0, tp = 0;
char[] name_chars = name.toCharArray();
char[] typed_chars = typed.toCharArray();
while (np < name_chars.length && tp < typed_chars.length) {
if (name_chars[np] == typed_chars[tp]) {
np += 1;
tp += 1;
} else if (tp >= 1 && typed_chars[tp] == typed_chars[tp - 1]) {
tp += 1;
} else {
return false;
}
}
if (np != name_chars.length) {
return false;
} else {
while (tp < typed_chars.length) {
if (typed_chars[tp] != typed_chars[tp - 1])
return false;
tp += 1;
}
}
return true;
}
}
*/

0 comments on commit 0c3bed2

Please sign in to comment.