My JAVA Solution Easy Understand


  • 0
    J
    public class Solution {
        public String reverseStr(String s, int k) {
            if (s == null || s.length() == 0 || k <= 0) return s;
            int len = s.length();
            StringBuilder sb = new StringBuilder();
            StringBuilder buil = new StringBuilder();
            int count1 = 0, count2 = 0;
            
            for (int i = 0; i < len; i++) {
                
                while (i < len && (i+1)%(2*k) >= 1 && (i+1)%(2*k) <= k) {
                    buil.append(s.substring(i, i + 1));
                    i++;
                }
                if (buil.length() > 0) sb.append(buil.reverse());
                if (i < len) sb.append(s.charAt(i));
                buil = new StringBuilder();
            }
            
            return sb.toString();
        }
    }

Log in to reply
 

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