Simple StringBuilder Java Solution


  • 0
    public class Solution {
        public String reverseStr(String s, int k) {
            
            StringBuilder sb = new StringBuilder();
            char[] letters = s.toCharArray();
            
            int i = 0;
            while(i*k < s.length()){
                if(i%2 == 0){
                    for(int j = Math.min(s.length() - i*k, k); j > 0; j--){
                        sb.append(letters[j + i*k - 1]);
                    }
                }
                else{
                    for(int j = 0; j < Math.min(s.length() - i*k, k); j++){
                        sb.append(letters[j + i*k]);
                    }
                }
                i++;
            }
            
            return sb.toString();
        }
    }
    

Log in to reply
 

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