REVERSE STRING II simple c++ O(N) solution
First of all let\'s define some variable that we will be using:\nsize -> size of the string\nleft -> how many characters left in the string to be reversed (init
First of all let\'s define some variable that we will be using:\nsize -> size of the string\nleft -> how many characters left in the string to be reversed (initially, left = size)\n\nwe will perform our reversing operation till no character is left in the string.\ncondition 1 : if left < k, then just reverse the string from current index ( i ) to end. ( cuurent index explained later)\nconditon 2 : if left >= k, then reverse the string from current index to current index + k th index.\n\nnow what is this current index. \ncurrent index = size - left; \n\nsuppose if k=5 and size = 13 = left(initially), so if i reverse first 5 index then numbers of characters left = left - 2*k that is 3 (13-10).\ntherefore in the next iteration we will reverse the string from 10 index i.e., size - left = 13 - 3 = 10 .\n\n```\nclass Solution {\npublic:\n    string reverseStr(string s, int k) {\n        int size=s.length();\n        int left=size;\n        while(left>0)\n        {\n            int i=size-left;                             // i = cuurent index = size - left;\n            if(k<=left)                           \n            {\n                reverse(s.begin()+i, s.begin()+i+k);\n                left-=2*k;                   //updating left characters after reversing operation\n            }\n            else if(left<k)\n            {      \n                reverse(s.begin()+i, s.end());\n                left=0;\n            }   \n            \n        }\n        return s;\n    }\n};\n```\n\nthankyou !! \n\nif you found this helpful please upvote.\nand if i bored you then sorryfor that :P
['C']