Java Clean Solution


  • 0
    public String reverseStr(String s, int k) {
        StringBuffer sb = new StringBuffer(s);
        int c = 0;
        for(int i=0; i<s.length(); i+=k) {
            if(c==0) {
                int end = (i+k-1)<s.length() ? (i+k-1) : (s.length()-1);
                sb.replace(i,end+1,new StringBuffer(s.substring(i,end+1)).reverse().toString());
            }
            c = (c+1)%2;
        }
        return sb.toString();
    }

Log in to reply
 

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