@@ -0,0 +1,2615 @@
+import org.junit.After;%0Aimport org.junit.Assert;%0Aimport org.junit.Before;%0Aimport org.junit.Test;%0A%0A/**%0A * Write a function that takes a string as input and returns the string reversed.%0A * %3Cp%3E%0A * Example:%0A * Given s = %22hello%22, return %22olleh%22.%0A * %3Cp%3E%0A * Subscribe to see which companies asked this question%0A * %3Cp%3E%0A * Tags: Two Pointers, String%0A * Similar Problems: (E) Reverse Vowels of a String%0A */%0Apublic class ReverseString %7B%0A%0A    private ReverseString rs;%0A%0A    /**%0A     * Two-pointer swapping%0A     */%0A    public String reverseString(String s) %7B%0A        if (s == null %7C%7C s.length() %3C 2) return s;%0A        int i = 0;%0A        int j = s.length() - 1;%0A        char%5B%5D chars = s.toCharArray();%0A        while (i %3C j) %7B%0A            char c = chars%5Bi%5D;%0A            chars%5Bi%5D = chars%5Bj%5D;%0A            chars%5Bj%5D = c;%0A            i++;%0A            j--;%0A        %7D%0A        return new String(chars);%0A    %7D%0A%0A    /**%0A     * Use StringBuilder::reverse%0A     */%0A    public String reverseStringB(String s) %7B%0A        return new StringBuilder(s).reverse().toString();%0A    %7D%0A%0A    /**%0A     * Use bit manipulation instead of temp variable%0A     */%0A    public String reverseStringC(String s) %7B%0A        byte%5B%5D bytes = s.getBytes();%0A        int i = 0;%0A        int j = s.length() - 1;%0A        while (i %3C j) %7B%0A            bytes%5Bi%5D = (byte) (bytes%5Bi%5D %5E bytes%5Bj%5D);%0A            bytes%5Bj%5D = (byte) (bytes%5Bi%5D %5E bytes%5Bj%5D); // bytes%5Bi%5D %5E bytes%5Bj%5D %5E bytes%5Bj%5D = bytes%5Bi%5D%0A            bytes%5Bi%5D = (byte) (bytes%5Bi%5D %5E bytes%5Bj%5D); // bytes%5Bi%5D %5E bytes%5Bj%5D %5E bytes%5Bi%5D = bytes%5Bj%5D%0A            i++;%0A            j--;%0A        %7D%0A        return new String(bytes);%0A    %7D%0A%0A    /**%0A     * Recursion (Divide and conquer)%0A     * Reverse a string can be done by reversing right substring and reversing left substring%0A     * Then concatenate them%0A     */%0A    public String reverseStringD(String s) %7B%0A        int length = s.length();%0A        if (length %3C= 1) return s;%0A        String leftStr = s.substring(0, length / 2);%0A        String rightStr = s.substring(length / 2, length);%0A        return reverseString(rightStr) + reverseString(leftStr);%0A    %7D%0A%0A%0A    @Before%0A    public void setUp() %7B%0A        rs = new ReverseString();%0A    %7D%0A%0A    @Test%0A    public void testEdgeCases() %7B%0A        Assert.assertNull(rs.reverseString(null));%0A        Assert.assertEquals(%22%22, rs.reverseString(%22%22));%0A        Assert.assertEquals(%22a%22, rs.reverseString(%22a%22));%0A        Assert.assertEquals(%22 %22, rs.reverseString(%22 %22));%0A    %7D%0A%0A    @Test%0A    public void testFunction() %7B%0A        Assert.assertEquals(%22olleh%22, rs.reverseString(%22hello%22));%0A    %7D%0A%0A    @After%0A    public void tearDown() %7B%0A        rs = null;%0A    %7D%0A%0A%7D%0A

add Reverse String