My java easy solution


  • 0
    Y

    public class Solution {
    public String reverseStr(String s, int k) {
    if(k>s.length()) return re(s);
    StringBuilder sb1 = new StringBuilder();
    int m = s.length()/k;
    int n = s.length()%k;
    for(int i=0;i<m;i++){
    if(i%2==0){
    sb1.append(re(s.substring(0+ik,k+ik)));
    }else{
    sb1.append(s.substring(0+ik,k+ik));
    }
    }
    if(m%2==0){
    sb1.append(re(s.substring(s.length()-n,s.length())));
    }else{
    sb1.append(s.substring(s.length()-n,s.length()));
    }
    return sb1.toString();
    }
    private String re(String s){
    StringBuilder sb = new StringBuilder();
    for(int i=s.length()-1;i>=0;i--){
    sb.append(s.charAt(i));
    }
    return sb.toString();
    }
    }


Log in to reply
 

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