diff --git a/src/main/java/com/thealgorithms/twopointer/MoveHash.java b/src/main/java/com/thealgorithms/twopointer/MoveHash.java new file mode 100644 index 000000000000..cc1df3861548 --- /dev/null +++ b/src/main/java/com/thealgorithms/twopointer/MoveHash.java @@ -0,0 +1,84 @@ +/** + * Moves all '#' characters to the end of the string. + * + * Time Complexity: O(n) + * Space Complexity: O(1) + * + * Inspired by Move Zeroes problem: + * https://leetcode.com/problems/move-zeroes/ + * + * @author Shyam Chavda + */ + +package com.thealgorithms.twopointer; + +public class MoveHash { + + /** default constructor */ + public MoveHash() {} + + /** + * Place all hash to end of the String and return new String. + * + * @param s the input string. + * @return the new string where hash place at the end. + * returns null if the input string is null. + */ + + public static String movehashtoend(String s) { + /** return null if inputed string is null */ + if (s == null) { + return null; + } + + /** converts string into character array + for example., + string = roman, + character array = ['r','o','m','a','n'] + */ + + char[] c = s.toCharArray(); + + /** j works like a tracker the hash position */ + int j = 0; + + /** i traverse whole character array. + * if character is non-hash then easily swap with tracker(j) position + * + * for example., + * input string = a#bc; + * at first traverse current non-hash character(a) swap with j position. + * + * string becomes #abc. + * this continuos untill i reaches to end of string. + */ + + for (int i = 0; i < c.length; i++) { + if (c[i] != '#') { + swap(i, j, c); + j++; + } + } + + /** converts the character array to String at a time and returns. */ + return new String(c); + } + + /** simple swapping logic with third variable. */ + public static void swap(int a, int b, char[] c) { + char tmp = c[a]; + c[a] = c[b]; + c[b] = tmp; + } + + public static void main(String[] args) { + /** input part. */ + String input = "h#e#l###l#o"; + + /** output catches through the function. */ + String output = movehashtoend(input); + + /** display appropriate output. */ + System.out.println(output); + } +} diff --git a/src/test/java/com/thealgorithms/twopointer/MoveHashTest.java b/src/test/java/com/thealgorithms/twopointer/MoveHashTest.java new file mode 100644 index 000000000000..8c57580cb03b --- /dev/null +++ b/src/test/java/com/thealgorithms/twopointer/MoveHashTest.java @@ -0,0 +1,64 @@ +package com.thealgorithms.twopointer; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import org.junit.jupiter.api.Test; + +public class MoveHashTest { + + @Test + void testMoveHash() { + assertEquals("hello#####", MoveHash.movehashtoend("h#e#l###l#o")); + } + + @Test + void testEmpty() { + assertEquals("", MoveHash.movehashtoend("")); + } + + @Test + void testNull() { + assertNull(MoveHash.movehashtoend(null)); + } + + @Test + void testNoHash() { + assertEquals("hello", MoveHash.movehashtoend("hello")); + } + + @Test + void testAllHash() { + assertEquals("#####", MoveHash.movehashtoend("#####")); + } + + @Test + void testHashAtEnd() { + assertEquals("hello##", MoveHash.movehashtoend("hello##")); + } + + @Test + void testHashAtStart() { + assertEquals("hello#####", MoveHash.movehashtoend("######hello")); + } + + @Test + void testSingleCharacter() { + assertEquals("#", MoveHash.movehashtoend("#")); + } + + @Test + void testSingleCharacterNoHash() { + assertEquals("a", MoveHash.movehashtoend("a")); + } + + @Test + void testSwapFunction() { + char[] arr = {'a', 'b', 'c'}; + MoveHash.swap(0, 2, arr); + assertEquals('c', arr[0]); + assertEquals('a', arr[2]); + } +} + } +} \ No newline at end of file