C++ with simple explanation
I just considered a sliding window of size "3". \n- If the first element in the current window is "O", slide the index by 1\n- Otherwise we slide the index by 3
I just considered a sliding window of size "3". \n- If the first element in the current window is "O", slide the index by 1\n- Otherwise we slide the index by 3\n\nConsider an example "XXXXX"\n>\tIn the first window, the element at the first index (i=0) is "X"\n\t[ [XXX] XXX] \n\tSo, we convert the string to \n\t[ [OOO] XXX] \n\tand increment the index to i = i+3 = 0+3 = 3. The next window is \n\t[ OOO [XXX] ] \n\nConsider an example "OXXXXX"\n> In the first window, the element at the first index (i=0) is "O"\n[ [OXX] XXX] \nSo, we retain the string \n[ [OXX] XXX] \nand increment the index to i = i+1 = 0+1 = 1. The next window is \n[ O[XXX]XX] \n\n\n```\nclass Solution {\npublic:\n    int minimumMoves(string s) {\n        // Have a sliding window of size "3"\n        // if the first element in the current window is "O", slide window 1 index to right\n        // otherwise, slide window 3 indices\n        \n        int count = 0;\n        int N = s.size();\n        for(int i=3; i<=N; ) {\n            if(s[i-3]==\'O\') {\n                ++i;\n            } \n            else {\n                s[i-3]=\'O\'; s[i-2]=\'O\'; s[i-1]=\'O\';\n                ++count;\n                i += 3;\n            }\n        }\n        \n        // if we have an "X" in the last window\n        if(s[N-1]!=\'O\' || s[N-2]!=\'O\' || s[N-3]!=\'O\') {\n            ++count;\n        }\n        \n        return count;\n    }\n};\n```\n\n
[]