Java Recursive Solution. Easiest in History :-)


  • 0
    T
    public class Solution {
        
        public String reverseStr(String s, int k) {
            
            if(s.length() <= k){
                
                return new StringBuilder(s).reverse().toString();
            }
            else{
                
                String s1 = new StringBuilder(s.substring(0, k)).reverse().toString();
                
                if(s.length() <= 2 * k)
                    return  s1 + s.substring(k);  
                else
                    return  s1 + s.substring(k, 2*k) + reverseStr(s.substring(2*k), k);
    
                
            }
            
        }
    }
    
    

Log in to reply
 

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