Java one pass solution


  • 0
    U
    public String reverseStr(String s, int k) {
        
        StringBuilder sb = new StringBuilder();
        
        for (int i = 0; i < s.length(); i = i + 2*k) {
            for (int j = i+k-1; j >= i; j--) {
                if (j < s.length()) {
                    sb.append(s.charAt(j));
                }
            }
            
            for (int m = i+k; m < i + 2*k; m++) {
                if (m < s.length()) {
                    sb.append(s.charAt(m));
                }
            }
        }
        
        return sb.toString();
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.