Skip to content

Commit 4ff775d

Browse files
feat(strings): add MoveHashToEnd algorithm with tests
- Add MoveHashToEnd utility class that moves all '#' characters to the end of a string while preserving the order of other characters - Algorithm runs in O(n) time and O(n) space using a two-pass approach: first collects non-'#' chars, then fills remaining positions with '#' - Add null and empty string guards - Add MoveHashToEndTest with 9 unit tests covering normal, edge, and boundary cases (null, empty, all-hash, no-hash, single char)
1 parent 5e06b15 commit 4ff775d

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.thealgorithms.strings;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
public class MoveHashToEndTest {
9+
10+
@Test
11+
void testBasicCase() {
12+
assertEquals("hello###", MoveHashToEnd.moveHashToEnd("h#e#l#llo"));
13+
}
14+
15+
@Test
16+
void testNoHash() {
17+
assertEquals("hello", MoveHashToEnd.moveHashToEnd("hello"));
18+
}
19+
20+
@Test
21+
void testAllHashes() {
22+
assertEquals("###", MoveHashToEnd.moveHashToEnd("###"));
23+
}
24+
25+
@Test
26+
void testHashAtEnd() {
27+
assertEquals("hello#", MoveHashToEnd.moveHashToEnd("hello#"));
28+
}
29+
30+
@Test
31+
void testHashAtStart() {
32+
assertEquals("hello#", MoveHashToEnd.moveHashToEnd("#hello"));
33+
}
34+
35+
@Test
36+
void testEmptyString() {
37+
assertEquals("", MoveHashToEnd.moveHashToEnd(""));
38+
}
39+
40+
@Test
41+
void testNullInput() {
42+
assertNull(MoveHashToEnd.moveHashToEnd(null));
43+
}
44+
45+
@Test
46+
void testSingleHash() {
47+
assertEquals("#", MoveHashToEnd.moveHashToEnd("#"));
48+
}
49+
50+
@Test
51+
void testSingleNonHashChar() {
52+
assertEquals("a", MoveHashToEnd.moveHashToEnd("a"));
53+
}
54+
}

0 commit comments

Comments
 (0)