Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitedd9dcc

Browse files
add 844
1 parenta010fb3 commitedd9dcc

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2424
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
25+
|844|[Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_844.java)| O(n)| O(1)||Easy|
2526
|832|[Flipping an Image](https://leetcode.com/problems/flipping-an-image/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_832.java)| O(n)| O(1)||Easy|
2627
|830|[Positions of Large Groups](https://leetcode.com/problems/positions-of-large-groups/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_830.java)| O(n)| O(n)||Easy|
2728
|824|[Goat Latin](https://leetcode.com/problems/goat-latin/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_824.java)| O(n)| O(1)||Easy|
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
packagecom.fishercoder.solutions;
2+
3+
/**
4+
* 844. Backspace String Compare
5+
*
6+
* Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character.
7+
*
8+
* Example 1:
9+
* Input: S = "ab#c", T = "ad#c"
10+
* Output: true
11+
* Explanation: Both S and T become "ac".
12+
*
13+
* Example 2:
14+
* Input: S = "ab##", T = "c#d#"
15+
* Output: true
16+
* Explanation: Both S and T become "".
17+
*
18+
* Example 3:
19+
* Input: S = "a##c", T = "#a#c"
20+
* Output: true
21+
* Explanation: Both S and T become "c".
22+
*
23+
* Example 4:
24+
* Input: S = "a#c", T = "b"
25+
* Output: false
26+
* Explanation: S becomes "c" while T becomes "b".
27+
* Note:
28+
*
29+
* 1 <= S.length <= 200
30+
* 1 <= T.length <= 200
31+
* S and T only contain lowercase letters and '#' characters.
32+
* Follow up:
33+
*
34+
* Can you solve it in O(N) time and O(1) space?
35+
*/
36+
publicclass_844 {
37+
publicstaticclassSolution1 {
38+
publicbooleanbackspaceCompare(StringS,StringT) {
39+
StringprocessedS =process(S);
40+
StringprocessedT =process(T);
41+
returnprocessedS.equals(processedT);
42+
}
43+
44+
privateStringprocess(Stringstr) {
45+
StringBuildersb =newStringBuilder();
46+
for (inti =0;i <str.length();i++) {
47+
if (str.charAt(i) =='#') {
48+
if (sb.length() >0) {
49+
sb.deleteCharAt(sb.length() -1);
50+
}
51+
}else {
52+
sb.append(str.charAt(i));
53+
}
54+
}
55+
returnsb.reverse().toString();
56+
}
57+
}
58+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._844;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_844Test {
10+
privatestatic_844.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_844.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals(true,solution1.backspaceCompare("ab#c","ad#c"));
20+
}
21+
22+
@Test
23+
publicvoidtest2() {
24+
assertEquals(true,solution1.backspaceCompare("ab##","c#d#"));
25+
}
26+
27+
@Test
28+
publicvoidtest3() {
29+
assertEquals(true,solution1.backspaceCompare("a##c","#a#c"));
30+
}
31+
32+
@Test
33+
publicvoidtest4() {
34+
assertEquals(false,solution1.backspaceCompare("a#c","b"));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp