package fundamentals.string.manipulate.lc344_reversestring;

/**
 * Write a function that takes a string as input and returns the string reversed.
 * Example: Given s = "hello", return "olleh".
 */
public class Solution {

    // My 2nd: use char array
    public String reverseString(String s) {
        char[] chars = s.toCharArray();
        for (int i = 0, j = chars.length - 1; i < j; i++, j--) {
            char tmp = chars[i];
            chars[i] = chars[j];
            chars[j] = tmp;
        }
        return new String(chars);
    }

    // My 1st
    public String reverseString1(String s) {
        StringBuilder result = new StringBuilder(s.length());
        for (int i = s.length() - 1; i >= 0; i--) {
            result.append(s.charAt(i));
        }
        return result.toString();
    }

}

@@ -54,16 +54,93 @@
 tring;%0A%0A
+import org.junit.jupiter.api.Assertions;%0Aimport org.junit.jupiter.api.Test;%0A%0A
 /**%0A * W
@@ -293,16 +293,603 @@
 tion %7B%0A%0A
+    @Test%0A    void test() %7B%0A        Assertions.assertEquals(%22olleh%22, reverseString(%22hello%22));%0A    %7D%0A%0A    // O(NlogN) time: rev(left+right) = rev(right) + rev(left)%0A    public String reverseString(String s) %7B%0A        if (s.length() %3C= 1) return s; // note len=1 cause dead loop: rev(substr(0)) + rev(substr(0,0))%0A        int m = s.length() / 2;%0A        return reverseString(s.substring(m)) + reverseString(s.substring(0, m)); // string concatenation takes linear time%0A    %7D%0A%0A    public String reverseString_jdk(String s) %7B%0A        return new StringBuilder(s).reverse().toString();%0A    %7D%0A%0A
     // M
@@ -933,32 +933,33 @@
 ng reverseString
+2
 (String s) %7B%0A   

reverse string