LeetCode/string/ReverseStringII.java
// characters and left the other as original.
characters and left the other as original.
characters and left the other as original .
public String reverseStr(String s, int k) {
        StringBuilder sb = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();
        while (s.trim().length() > 0) {
            if (s.length() >= 2 * k) {
                sb2.setLength(0);
                sb.append(sb2.append(s.substring(0, k).toCharArray()).reverse().toString());
                sb.append(s.substring(k, 2 * k));
                s = s.substring(2 * k, s.length());
            } else if (s.length() >= k && s.length() < 2 * k) {
                sb2.setLength(0);
                sb.append(sb2.append(s.substring(0, k).toCharArray()).reverse().toString());
                sb.append(s.substring(k, s.length()));
                s = "";
            } else {
                sb2.setLength(0);
                sb.append(sb2.append(s.substring(0, s.length()).toCharArray()).reverse().toString());
                s = "";
            }
        }
        System.out.println(sb.toString());
        return sb.toString();
    }
public String reverseStr ( String s , int k ) { StringBuilder sb = new StringBuilder ( ) ; StringBuilder sb2 = new StringBuilder ( ) ; while ( s . trim ( ) . length ( ) > 0 ) { if ( s . length ( ) >= 2 * k ) { sb2 . setLength ( 0 ) ; sb . append ( sb2 . append ( s . substring ( 0 , k ) . toCharArray ( ) ) . reverse ( ) . toString ( ) ) ; sb . append ( s . substring ( k , 2 * k ) ) ; s = s . substring ( 2 * k , s . length ( ) ) ; } else if ( s . length ( ) >= k && s . length ( ) < 2 * k ) { sb2 . setLength ( 0 ) ; sb . append ( sb2 . append ( s . substring ( 0 , k ) . toCharArray ( ) ) . reverse ( ) . toString ( ) ) ; sb . append ( s . substring ( k , s . length ( ) ) ) ; s = "" ; } else { sb2 . setLength ( 0 ) ; sb . append ( sb2 . append ( s . substring ( 0 , s . length ( ) ) . toCharArray ( ) ) . reverse ( ) . toString ( ) ) ; s = "" ; } } System . out . println ( sb . toString ( ) ) ; return sb . toString ( ) ; }
characters and left the other as original.
characters and left the other as original .