Java : 0ms | Faster than 100% | Clean | Efficient | Pure Logic | Explained
Please upvote if you find this useful!!!\n\nMath.min(index + k - 1, len - 1) means that we want to iterate till end index i.e. swap elements at start index with
**Please upvote if you find this useful!!!**\n\n**Math.min(index + k - 1, len - 1)** means that we want to iterate till **end index** i.e. swap elements at **start index** with elements at **end index**, but if the characters left are less than k, then we will take **end index** as len - 1.\neg: "abcdefgh", and k = 3.\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;       01234567    <-- Indexes\nHere, index + k - 1, will work fine for "abc" as index + k - 1 == 0 + 3 - 1 ==> 2 which is a valid index.\nNow after skipping "def", we are left with only **2 indexes** which are less than k(i.e. 3), so if use (index + k - 1), then it will try to access element which is not in the scope i.e. **OutOfBounds** because if we start from \'g\' having index 6 and use (index + k - 1), then 6 + 3 - 1 ==> 8 and it will try to access 8th element which is not in our scope. But we want to access the 6th and 7th element too. So, we will loop till index 7 to reverse "gh" as "hg" and for that we will use (len - 1) as length of "abcdefgh" is 8 and (len - 1) == (8 - 1) == 7\nNow, from Math.min(index + k - 1, len - 1), we will get 7 (as 8 > 7)\n\nWe can also understand in this way:\n**if(index + k > len)**\n**end = len - 1;**\n**else**\n**end = index + k - 1;**\n\nOR \n**end = index + k > len ? len - 1 : index + k - 1; ==> Math.min(index + k - 1, len - 1)**\n\n\n```\nclass Solution {\n    public String reverseStr(String s, int k) {\n        \n        char[] ch = s.toCharArray();\n        final int len = ch.length;\n\t\t\n        for(int index = 0 ; index < len ; index += k << 1) // index += k << 1 ==> index = index + k * 2, but use bit operator for faster execution\n            reverse(ch, index, Math.min(index + k - 1, len - 1)); // Math.min(index + k - 1, len - 1) ; take end index of whichever is lesser; explained above\n        \n        return new String(ch);\n    }\n    \n    void reverse(char[] ch, int start, int end){\n        \n        char temp;\n        \n        while(start < end){\n            temp = ch[start];\n            ch[start++] = ch[end];\n            ch[end--] = temp;\n        }\n    }        \n}\n\n```
['Java']