Java StringBuilder Solution


  • 0
    S
    public class Solution {
        public String reverseStr(String s, int k) {
            int l=s.length();
            StringBuilder sb=new StringBuilder(s);
            int i=0;
            for(i=0;i+k<l;i+=2*k)
            {
                StringBuilder temp=new StringBuilder(sb.substring(i,i+k));
                sb.replace(i,i+k,temp.reverse().toString());
            }
            if(i<l)
            {
                StringBuilder temp=new StringBuilder(sb.substring(i,l));
                sb.replace(i,l,temp.reverse().toString());
            }
            return sb.toString();
        }
    }
    

Log in to reply
 

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