Skip to content

Commit f6d17b9

Browse files
add 925
1 parent 153ebae commit f6d17b9

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Your ideas/fixes/algorithms are more than welcome!
3333
|941|[Valid Mountain Array](https://leetcode.com/problems/valid-mountain-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_941.java) | O(n) | O(1) | |Easy|
3434
|933|[Number of Recent Calls](https://leetcode.com/problems/number-of-recent-calls/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_933.java) | O(n) | O(n) | |Easy|
3535
|929|[Unique Email Addresses](https://leetcode.com/problems/unique-email-addresses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_929.java) | O(n) | O(n) | |Easy|
36+
|925|[Long Pressed Name](https://leetcode.com/problems/long-pressed-name/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_925.java) | O(n) | O(1) | |Easy|
3637
|922|[Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_922.java) | O(n) | O(1) | |Easy|
3738
|917|[Reverse Only Letters](https://leetcode.com/problems/reverse-only-letters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_917.java) | O(n) | O(n) | |Easy|
3839
|900|[Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_900.java) | O(n) | O(1) | |Easy|
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 925. Long Pressed Name
5+
*
6+
* Your friend is typing his name into a keyboard.
7+
* Sometimes, when typing a character c, the key might get long pressed,
8+
* and the character will be typed 1 or more times.
9+
*
10+
* You examine the typed characters of the keyboard.
11+
* Return True if it is possible that it was your friends name,
12+
* with some characters (possibly none) being long pressed.
13+
*
14+
* Example 1:
15+
*
16+
* Input: name = "alex", typed = "aaleex"
17+
* Output: true
18+
* Explanation: 'a' and 'e' in 'alex' were long pressed.
19+
* Example 2:
20+
*
21+
* Input: name = "saeed", typed = "ssaaedd"
22+
* Output: false
23+
* Explanation: 'e' must have been pressed twice, but it wasn't in the typed output.
24+
* Example 3:
25+
*
26+
* Input: name = "leelee", typed = "lleeelee"
27+
* Output: true
28+
* Example 4:
29+
*
30+
* Input: name = "laiden", typed = "laiden"
31+
* Output: true
32+
* Explanation: It's not necessary to long press any character.
33+
*
34+
* Note:
35+
* name.length <= 1000
36+
* typed.length <= 1000
37+
* The characters of name and typed are lowercase letters.
38+
* */
39+
public class _925 {
40+
public static class Solution1 {
41+
public boolean isLongPressedName(String name, String typed) {
42+
int i = 0;
43+
for (int j = 0; i < name.length() && j < typed.length(); i++) {
44+
if (name.charAt(i) != typed.charAt(j)) {
45+
return false;
46+
} else if (i < name.length() - 1 && name.charAt(i) != name.charAt(i+1)) {
47+
j++;
48+
while (j < typed.length() && name.charAt(i) == typed.charAt(j)) {
49+
j++;
50+
}
51+
} else {
52+
j++;
53+
}
54+
55+
}
56+
return i == name.length();
57+
}
58+
}
59+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._925;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _925Test {
10+
private static _925.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _925.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(true, solution1.isLongPressedName("alex", "aaleex"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(false, solution1.isLongPressedName("saeed", "ssaaedd"));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(true, solution1.isLongPressedName("leelee", "lleeelee"));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(true, solution1.isLongPressedName("laiden", "laiden"));
35+
}
36+
37+
@Test
38+
public void test5() {
39+
assertEquals(false, solution1.isLongPressedName("pyplrz", "ppyypllr"));
40+
}
41+
42+
@Test
43+
public void test6() {
44+
assertEquals(true, solution1.isLongPressedName("leelee", "lleeelee"));
45+
}
46+
}

0 commit comments

Comments
 (0)