Skip to content

Commit 894ea16

Browse files
docs(strings): add reference URL to MoveHashToEnd Javadoc
1 parent 4ff775d commit 894ea16

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.thealgorithms.strings;
2+
3+
/**
4+
* Moves all '#' characters to the end of the given string while preserving
5+
* the order of the other characters.
6+
*
7+
* Example:
8+
* Input : "h#e#l#llo"
9+
* Output : "hello###"
10+
*
11+
* The algorithm works by iterating through the string and collecting
12+
* all non-# characters first, then filling the remaining positions
13+
* with '#'.
14+
*
15+
* Time Complexity: O(n)
16+
* Space Complexity: O(n)
17+
*
18+
* @see <a href="https://www.geeksforgeeks.org/move-special-char-end-string-maintain-order-alphabets/">Move all special characters to end - GeeksForGeeks</a>
19+
*/
20+
public final class MoveHashToEnd {
21+
22+
/**
23+
* Private constructor to prevent instantiation of utility class.
24+
*/
25+
private MoveHashToEnd() {
26+
}
27+
28+
/**
29+
* Moves all '#' characters in the input string to the end.
30+
*
31+
* @param str the input string containing characters and '#'
32+
* @return a new string with all '#' characters moved to the end
33+
*/
34+
public static String moveHashToEnd(String str) {
35+
if (str == null || str.isEmpty()) {
36+
return str;
37+
}
38+
39+
char[] arr = str.toCharArray();
40+
int insertPos = 0;
41+
42+
// Place all non-# characters at the beginning
43+
for (char ch : arr) {
44+
if (ch != '#') {
45+
arr[insertPos++] = ch;
46+
}
47+
}
48+
49+
// Fill remaining positions with '#'
50+
while (insertPos < arr.length) {
51+
arr[insertPos++] = '#';
52+
}
53+
54+
return new String(arr);
55+
}
56+
}

0 commit comments

Comments
 (0)