java recursive solution

  • 0
    public String reverseStr(String s, int k) {
            if(s.length()<k) return reverse(s);
            else if(s.length()>=k&&s.length()<2*k) return reverse(s.substring(0,k))+s.substring(k);
                int len = s.length()/(2*k);
                for(int i=0;i<len;i++){
                    return reverse(s.substring(0,k))+s.substring(k,2*k)+reverseStr(s.substring(2*k),k);
            return null;
        public String reverse(String s){
            StringBuffer sb = new StringBuffer(s);
            return sb.reverse().toString();

Log in to reply

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