fueanta

LeetCode 541: Reverse String II

Jul 8th, 2021
1,211
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function reverseStr(s: string, k: number): string {
  2.     const charArray = s.split("");
  3.    
  4.     for (let i = 0; i < charArray.length; i += 2 * k) {
  5.         let start = i;
  6.         let end = Math.min(start + k - 1, charArray.length);
  7.        
  8.         while (start < end) {
  9.             [charArray[start], charArray[end]] = [charArray[end], charArray[start]];
  10.            
  11.             ++start;
  12.             --end;
  13.         }
  14.     }
  15.    
  16.     return charArray.join("");
  17. };
RAW Paste Data