question_slug
stringlengths
3
77
title
stringlengths
1
183
slug
stringlengths
12
45
summary
stringlengths
1
160
author
stringlengths
2
30
certification
stringclasses
2 values
created_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
updated_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
hit_count
int64
0
10.6M
has_video
bool
2 classes
content
stringlengths
4
576k
upvotes
int64
0
11.5k
downvotes
int64
0
358
tags
stringlengths
2
193
comments
int64
0
2.56k
count-number-of-teams
simple java solution
simple-java-solution-by-amir-ammar-ztks
\tclass Solution {\n\t\tpublic static int numTeams(int[] rating) {\n\t\t\tint n = rating.length;\n\t\t\tint [][] dp = new int[n][2]; // 0 smaller than, 1 greate
amir-ammar
NORMAL
2022-03-23T20:56:49.821327+00:00
2022-03-23T20:56:49.821367+00:00
767
false
\tclass Solution {\n\t\tpublic static int numTeams(int[] rating) {\n\t\t\tint n = rating.length;\n\t\t\tint [][] dp = new int[n][2]; // 0 smaller than, 1 greater than\n\t\t\tint ans = 0;\n\t\t\tfor (int i = 1; i < n; i++) {\n\t\t\t\tfor (int j = 0; j < i; j++) {\n\t\t\t\t\tif(rating[i] < rating[j]){dp[i][0] += 1;ans+=dp[j][0];}\n\t\t\t\t\tif(rating[i] > rating[j]){dp[i][1] += 1;ans+=dp[j][1];}\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn ans;\n\t\t}\n}
4
0
['Dynamic Programming', 'Java']
2
count-number-of-teams
Java Solution with Explanation
java-solution-with-explanation-by-am0611-jaxv
\nclass Solution {\n public int numTeams(int[] ratings) {\n int total = 0;\n \n for(int i = 1; i < ratings.length; i++)\n {\n
am0611
NORMAL
2022-03-05T22:26:34.454117+00:00
2022-04-10T18:14:13.119576+00:00
481
false
```\nclass Solution {\n public int numTeams(int[] ratings) {\n int total = 0;\n \n for(int i = 1; i < ratings.length; i++)\n {\n // For ascending order -> rating[i] < rating[j] < rating[k]\n int leftLess = 0;\n int rightGreater = 0;\n \n // For descending order -> rating[i] > rating[j] > rating[k]\n int leftGreater = 0;\n int rightLess = 0;\n \n // Find the values smaller/greater on the left side of ith element\n for(int j = 0; j < i; j++)\n {\n if(ratings[j] < ratings[i])\n {\n leftLess++;\n }\n else\n {\n leftGreater++;\n }\n }\n \n // Find the values smaller/greater on the right side of ith element\n for(int j = ratings.length - 1; j > i; j--)\n {\n if(ratings[i] < ratings[j])\n {\n rightGreater++;\n }\n else\n {\n rightLess++;\n }\n }\n \n // total number of teams from index i\n total += leftLess * rightGreater + leftGreater * rightLess;\n }\n \n return total;\n }\n}\n```
4
0
['Java']
1
count-number-of-teams
C++ easy to understand solution
c-easy-to-understand-solution-by-maitrey-aosp
I tried the brute force solution first and as expected, it showed me an TLE (big discovery! as stupid me previously thought leetcode will give TLE above 10^9 an
maitreya47
NORMAL
2021-08-16T07:58:17.527108+00:00
2021-08-16T07:58:17.527137+00:00
440
false
I tried the brute force solution first and as expected, it showed me an TLE (big discovery! as stupid me previously thought leetcode will give TLE above 10^9 and not 10^9 itself).\nAnyways, here is the optimal solution that traverses the vector and first calculates the number of ratings that are both less and greater than the current rating on both left and right side. \nBasically the idea being, the current element is the middle element of the sequence rating[i]<rating[j]<rating[k] OR rating[i]>rating[j]>rating[k]. And hence, by calculating the count of these 4 values, we can find the number of teams that have ascending rating (leftless * rightgreater) and number of teams having descending rating (leftgreater * rightless). Again, remember, current element is the middle element and hence we multiply and add the both ascending and descending teams. \nAtlast return the summation of total number of teams.\n```\nclass Solution {\npublic:\n int numTeams(vector<int>& rating) {\n int n = rating.size();\n int teams = 0;\n for(int i=1; i<n-1; i++)\n {\n int leftless = 0, rightgreater = 0, leftgreater = 0, rightless = 0;\n for(int j=i-1; j>=0; j--)\n {\n if(rating[j]>rating[i])\n leftgreater++;\n else\n leftless++;\n }\n for(int j = i+1; j<n; j++)\n {\n if(rating[j]>rating[i])\n rightgreater++;\n else\n rightless++;\n }\n teams+=(leftless*rightgreater) + (leftgreater*rightless);\n }\n return teams;\n }\n};\n```\nTime complexity: O(n^2)\nSpace complexity: O(1)
4
1
['C', 'C++']
0
count-number-of-teams
Java O(n^2) and O(N) memory, beats 97%
java-on2-and-on-memory-beats-97-by-larun-tcc3
For every element e in array \n\t1. For every element b before this where b < e\n - Coutn number of elements before b and less than b\n 2. For ever
larunrahul
NORMAL
2021-06-22T06:33:44.810903+00:00
2021-06-22T06:33:44.810944+00:00
253
false
For every element **e** in array \n\t1. For every element **b** before this where **b < e**\n - Coutn number of elements before b and less than b\n 2. For every element **b** before this where **b > e**\n - Coutn number of elements before b and greather than b\n\n```\npublic int numTeams(int[] rating) {\n\tint[] dp = new int[rating.length];\n\tint[] dp2 = new int[rating.length];\n\tint ans = 0;\n\tfor(int i = 1; i < rating.length; i++){\n\t\tfor(int j = i - 1; j >= 0; j--){\n\t\t\tif(rating[j] < rating[i]){\n\t\t\t\tdp[i]++;\n\t\t\t\tans += dp[j];\n\t\t\t} \n\t\t\tif(rating[j] > rating[i]){\n\t\t\t\tdp2[i]++;\n\t\t\t\tans += dp2[j];\n\t\t\t}\n\t\t}\n\t}\n\treturn ans;\n}\n```\n\t\t\n
4
0
[]
0
count-number-of-teams
[DP, O(N^2)] JS Solution
dp-on2-js-solution-by-hbjorbj-lj1m
\n/*\nStringtly increasing subsequence of size 3 or \nStrictly decreasing subsequence of size 3 is a valid team.\nWe need to find all such subsequences.\n*/\nva
hbjorbj
NORMAL
2021-06-05T02:03:45.186745+00:00
2021-06-05T02:03:45.186774+00:00
301
false
```\n/*\nStringtly increasing subsequence of size 3 or \nStrictly decreasing subsequence of size 3 is a valid team.\nWe need to find all such subsequences.\n*/\nvar numTeams = function(rating) {\n // dp1[i] = number of elements less than rating[i] in rating[0...i-1]\n // dp2[i] = number of elements greater than rating[i] in rating[0...i-1]\n let dp1 = new Array(rating.length).fill(0);\n let dp2 = new Array(rating.length).fill(0);\n let count = 0;\n for (let i = 0; i < rating.length; i++) {\n for (let j = 0; j < i; j++) {\n if (rating[j] < rating[i]) {\n dp1[i]++;\n // dp1[j] is the number of elements less than a number, which is less than current number\n // so, if dp1[j] is 1, (lets\'s say, some number x), then we have a team of (x, rating[j], rating[i])\n // if dp1[j] is 2, (let\'s say, some number x and y), then we have a team of\n // (x, rating[j], rating[i]) and (y, rating[j], rating[i])\n count += dp1[j]; \n }\n if (rating[j] > rating[i]) {\n dp2[i]++;\n count += dp2[j];\n }\n }\n }\n return count;\n // T.C: O(N^2)\n // S.C: O(N)\n};\n```
4
0
['JavaScript']
0
count-number-of-teams
C++ O(n log n) Using binary indexed tree (Fenwick tree)
c-on-log-n-using-binary-indexed-tree-fen-y5j3
\nclass Solution {\npublic:\n void update(vector<int>&bit,int index,int val)\n {\n while(index < bit.size())\n {\n bit[index] +=
objectobject
NORMAL
2021-06-01T09:39:49.545391+00:00
2021-06-01T09:39:49.545426+00:00
201
false
```\nclass Solution {\npublic:\n void update(vector<int>&bit,int index,int val)\n {\n while(index < bit.size())\n {\n bit[index] += val;\n index += index&(-index);\n }\n }\n int prefix(vector<int>&bit,int index)\n {\n int sum = 0;\n while(index > 0)\n {\n sum += bit[index];\n index -= index&(-index);\n }\n return sum;\n }\n int suffix(vector<int>&bit,int index)\n {\n return prefix(bit,1e5+4)-prefix(bit,index-1);\n }\n int numTeams(vector<int>& rating) \n {\n vector<int>bitl(100000+5,0);\n vector<int>bitr(100000+5,0);\n for(auto r:rating)\n {\n update(bitr,r,1);\n }\n int ans = 0;\n for(auto r:rating)\n {\n int leftLess = prefix(bitl,r-1);\n int leftGreat = suffix(bitl,r+1);\n int rightLess = prefix(bitr,r-1);\n int rightGreat = suffix(bitr,r+1);\n update(bitl,r,1);\n update(bitr,r,-1);\n ans += (leftLess*rightGreat);\n ans += (leftGreat*rightLess);\n }\n return ans;\n }\n};\n```
4
1
[]
0
count-number-of-teams
simple C++ sol with explaination || faster than 80%
simple-c-sol-with-explaination-faster-th-598x
We are basically storing the number of elements that are smaller and larger to the left of a particular element in an array dp.\nif in an array ith element is
saiteja_balla0413
NORMAL
2021-05-17T13:58:32.362029+00:00
2021-05-17T14:00:06.715325+00:00
293
false
We are basically storing the number of elements that are smaller and larger to the left of a particular element in an array dp.\nif in an array ith element is greater than jth element then the number of triplets would be the number of elements less than the jth elements.\nsame would be the case for the decreasing subsequences\n\n```\nclass Solution {\npublic:\n int numTeams(vector<int>& rating) {\n vector<vector<int>> dp(rating.size(),vector<int>(2,0));\n //dp[i][0] indicates the number of elements that are less than rating[i] to left\n //dp[i][1] indicates the number of elements that are greater tha rating[i]\n //to left\n int res=0;\n for(int i=0;i<rating.size();i++){\n for(int j=0;j<i;j++){\n if(rating[i]>rating[j]){\n dp[i][0]++;\n res+=dp[j][0];\n }\n else{\n dp[i][1]++;\n res+=dp[j][1];\n }\n }\n }\n return res;\n }\n};\n```\n\n**If you like my explaination please do upvote!.It supports me to post more solutions for you**
4
4
['Array']
0
count-number-of-teams
Simple Java Solution comparing optimization with brute-force version
simple-java-solution-comparing-optimizat-gs8w
optimization version\n\n public int numTeams(int[] rating) {\n int res=0;\n for(int i=1; i<rating.length-1; i++) {\n int inc1=0, inc
duck67
NORMAL
2021-02-13T14:00:39.153825+00:00
2021-02-14T01:59:15.143866+00:00
713
false
1. optimization version\n```\n public int numTeams(int[] rating) {\n int res=0;\n for(int i=1; i<rating.length-1; i++) {\n int inc1=0, inc2=0;\n int dec1=0, dec2=0;\n for(int j=0; j<rating.length;j++) {\n if(i>j) {\n if(rating[j] < rating[i]) inc1++;\n else dec1++;\n }\n if(i<j) {\n if(rating[j] > rating[i]) inc2++;\n else dec2++;\n }\n }\n res = res + (inc1*inc2) + (dec1*dec2);\n }\n return res;\n }\n``` \n\n2. brute-force version\n```\n public int numTeams(int[] rating) {\n int res=0;\n for(int i=0; i<rating.length-2; i++) {\n for(int j=i+1; j<rating.length-1; j++) {\n if(rating[i] < rating[j]) {\n for(int k=j+1; k<rating.length; k++) {\n if(rating[j] < rating[k]) {\n res++;\n }\n }\n }\n if(rating[i] > rating[j]) {\n for(int k=j+1; k<rating.length; k++) {\n if(rating[j] > rating[k]) {\n res++;\n }\n }\n }\n }\n }\n return res;\n }\n```
4
0
['Java']
1
count-number-of-teams
N^2 explained (with pictures) ^^
n2-explained-with-pictures-by-andrii_khl-j82u
Time O(N^2), space O(N)\n\n\nint numTeams(vector<int>& r) \n{\n\tmap<int, int> m;\n\tfor(auto i{0}; i<size(r); ++i) \n\t{\n\t\tm[r[i]] = i;\n\t\tr[i] = distance
andrii_khlevniuk
NORMAL
2020-11-23T15:15:09.172014+00:00
2021-02-03T20:55:27.880912+00:00
757
false
**Time `O(N^2)`, space `O(N)`**\n\n```\nint numTeams(vector<int>& r) \n{\n\tmap<int, int> m;\n\tfor(auto i{0}; i<size(r); ++i) \n\t{\n\t\tm[r[i]] = i;\n\t\tr[i] = distance(begin(m), m.find(r[i]));\n\t}\n\n int out{0};\n\tfor(auto i{begin(m)}; i!=end(m); ++i)\n\t{\n\t\tauto L = r[i->second];\n\t\tauto R = distance(begin(m), i) - L; \n\t\tout += L*(size(r)-i->second-1-R) + R*(i->second-L);\n\t}\n\n\treturn out; \n}\n```\n\n**Brief explanation**\n\nThe main idea is to consider every element in a vector as a potential middle element in a tripple and to count the raitings that are less/greater and are to the left/right:\n\n![image](https://assets.leetcode.com/users/images/88624a32-ea51-4965-bf29-be3808b4d7a4_1606151754.1310136.png)\n\n\n\n<br>\n\n\nI use ordered `map m` to store pairs `{rating, index}` and alter the input vector `r` on the fly, as I tranfer data from it to the `map`.\nI reuse `r[i]` to store the number of `ratings` with indices **less** than `i` that are **less** than the `rating` previously store at `r[i]`.\nIn other words in `r[i]` I store the number of `ratings` that were less than `r[i]` and were to the **left** of it (left green bars). With it I can calculate the number of `ratings` that were greater than `r[i]` and were to the **left** of it (left red bars).\nTo find out number of `ratings` that were less/greater than `r[i]` and were to the **right** of it I need a fully constructed map.\n\nIn the second loop \n* I use `r` to find out the number of `ratings` `L` **less** than the current one with the indices **less** than the index of the current `rating`.\n* I use the `map` to find out the number of `ratings` `R` **less** than the current one with the indices **greater** than the index of the current `rating`.\n\nI use `L` and `R` and the index of the current `rating` to cook up the number of triples which have current `rating` as a middle element.\n\n**P.S.** Map construction that takes `O(Nlog(N))` but `std::distance` is `O(N)`.
4
0
['C', 'C++']
1
count-number-of-teams
Python - Optimal solution easy to understand
python-optimal-solution-easy-to-understa-4q4y
python\nclass Solution:\n def numTeams(self, rating: List[int]) -> int:\n N = len(rating)\n\t\tif N < 3:\n return 0\n num_teams = 0\
reupiey
NORMAL
2020-11-09T17:27:02.984418+00:00
2020-11-09T17:27:02.984465+00:00
326
false
```python\nclass Solution:\n def numTeams(self, rating: List[int]) -> int:\n N = len(rating)\n\t\tif N < 3:\n return 0\n num_teams = 0\n for i in range(1, N - 1):\n middle = rating[i]\n inferiors_left, inferiors_right, superiors_left, superiors_right = 0, 0, 0, 0\n for j in range(0, i):\n if rating[j] < middle:\n inferiors_left += 1\n else:\n superiors_left += 1\n for k in range(i + 1, N):\n if rating[k] < middle:\n inferiors_right += 1\n else:\n superiors_right += 1\n num_teams += inferiors_left * superiors_right + superiors_left * inferiors_right\n return num_teams\n```
4
0
[]
2
count-number-of-teams
Python | Super Easy Method | SortedList (Balanced Binary Tree)
python-super-easy-method-sortedlist-bala-hbnb
Python | Super Easy Method | SortedList (Balanced Binary Tree)\n\n\nfrom sortedcontainers import SortedList\nclass Solution:\n def findLH(self,B,x):\n
aragorn_
NORMAL
2020-08-18T22:17:20.104852+00:00
2020-10-27T16:44:43.198379+00:00
965
false
**Python | Super Easy Method | SortedList (Balanced Binary Tree)**\n\n```\nfrom sortedcontainers import SortedList\nclass Solution:\n def findLH(self,B,x):\n \'\'\'\n Count number of values higher (H) and lower (L) than "x"\n \'\'\'\n L = B.bisect_left (x)\n H = len(B) - B.bisect_right(x)\n return L,H\n def numTeams(self, A):\n left = SortedList()\n right = SortedList(A)\n res = 0\n #\n for x in A:\n right.remove(x)\n #\n L_left ,H_left = self.findLH(left ,x)\n L_right,H_right = self.findLH(right,x)\n #\n # Update: [Nums lower than "x" to the left] * [Nums higher than "x" to the right]\n # + [Nums higher than "x" to the left] * [Nums lower than "x" to the right]\n #\n res += L_left * H_right + H_left * L_right\n #\n left .add (x)\n return res\n```
4
0
['Python', 'Python3']
1
count-number-of-teams
very simple clear code with O(n^2)
very-simple-clear-code-with-on2-by-mt201-zgkd
class Solution {\npublic:\n int numTeams(vector& rating) {\n \n int smaller1=0,greater1=0,smaller2=0,greater2=0;\n int sum=0;\n \
mt2019006
NORMAL
2020-06-27T08:29:03.187726+00:00
2020-06-27T08:29:03.187778+00:00
284
false
class Solution {\npublic:\n int numTeams(vector<int>& rating) {\n \n int smaller1=0,greater1=0,smaller2=0,greater2=0;\n int sum=0;\n \n for(int i=0;i<rating.size();i++)\n {\n smaller1=0;greater1=0;smaller2=0;greater2=0;\n \n for(int j=0;j<i;j++)\n {\n if(rating[i]>rating[j])\n smaller1++;\n else\n greater1++; \n }\n \n for(int j=i+1;j<rating.size();j++)\n {\n if(rating[i]>rating[j])\n smaller2++;\n else\n greater2++; \n }\n \n sum=sum+(smaller1*greater2)+(greater1*smaller2);\n }\n \n \n return sum;\n }\n};
4
0
[]
3
count-number-of-teams
Javascript Backtracking
javascript-backtracking-by-fbecker11-sh5o
\n/**\n * @param {number[]} rating\n * @return {number}\n */\nvar numTeams = function (rating) {\n const res = [];\n btk(rating, res);\n return res.length;\n
fbecker11
NORMAL
2020-03-30T17:48:01.916136+00:00
2020-03-30T18:00:08.095349+00:00
1,189
false
```\n/**\n * @param {number[]} rating\n * @return {number}\n */\nvar numTeams = function (rating) {\n const res = [];\n btk(rating, res);\n return res.length;\n};\n\nfunction btk(rating, res, arr = [], index = 0) {\n if (arr.length === 3) {\n res.push(arr)\n return;\n }\n\n for (let i = index; i < rating.length; i++) {\n const last = arr[arr.length - 1];\n const first = arr[0];\n if (!last || last < rating[i] && last >= first) {\n arr.push(rating[i])\n btk(rating, res, arr.concat(), i + 1)\n arr.pop();\n } else if (first && last > rating[i] && last <= first) {\n arr.push(rating[i])\n btk(rating, res, arr.concat(), i + 1)\n arr.pop();\n }\n }\n}\n```
4
0
['Backtracking', 'JavaScript']
2
count-number-of-teams
Python O(n^2) sol by sliding range [w/ Diagram] 有中文解析文章
python-on2-sol-by-sliding-range-w-diagra-0hy2
\u4E2D\u6587\u89E3\u6790\u6587\u7AE0\n\nPython O(n^2) sol by sliding range\n\n---\n\nDiagram and abstract model:\n\n\n\n\n\n---\n\nImplementation:\n\n\nclass So
brianchiang_tw
NORMAL
2020-03-29T13:02:18.240325+00:00
2024-07-29T09:15:57.832050+00:00
764
false
[\u4E2D\u6587\u89E3\u6790\u6587\u7AE0](https://leetcode.com/problems/count-number-of-teams/solutions/555469/python-o-n-2-sol-by-sliding-range-w-diagram/?envType=daily-question&envId=2024-07-29)\n\nPython O(n^2) sol by sliding range\n\n---\n\n**Diagram** and **abstract model**:\n\n![image](https://assets.leetcode.com/users/brianchiang_tw/image_1585491916.png)\n\n\n\n---\n\n**Implementation**:\n\n```\nclass Solution:\n def numTeams(self, rating: List[int]) -> int:\n r, size = rating, len( rating )\n \n\t\t# compute statistics of sliding range\n left_smaller = [ sum( r[i] < r[j] for i in range(0,j) ) for j in range(size) ]\n right_bigger = [ sum( r[j] < r[k] for k in range(j+1, size) ) for j in range(size)]\n \n num_of_teams = 0\n\t\t\n\t\t# j slides from 0 to ( n-1 ), and j stands for the index of middle element\n for j in range( 0, size):\n\t\t\n num_of_ascending_team = left_smaller[j] * right_bigger[j]\n\t\t\t\n num_of_descending_team = ( j - left_smaller[j] ) * ( size-1 - j - right_bigger[j] )\n \n num_of_teams += ( num_of_ascending_team + num_of_descending_team )\n \n return num_of_teams\n```\n\n---\n\n**Implementation**, rewritten in generator expression:\n\n```\nclass Solution:\n def numTeams(self, rating: List[int]) -> int:\n r, size = rating, len( rating )\n \n\t\t# compute statistics of sliding range\n left_smaller = [ sum( r[i] < r[j] for i in range(0,j) ) for j in range(size) ]\n right_bigger = [ sum( r[j] < r[k] for k in range(j+1, size) ) for j in range(size)]\n \n \n return sum( left_smaller[j] * right_bigger[j] + ( j - left_smaller[j] ) * ( size-1 - j - right_bigger[j] ) for j in range( 0, size) )\n```\n\n---\n\nReference:\n\n[1] [Python official docs about generator expression](https://www.python.org/dev/peps/pep-0289/)
4
0
['Array', 'Dynamic Programming', 'Sliding Window', 'Python', 'Python3']
0
count-number-of-teams
Java, 5ms
java-5ms-by-peacewalker-594t
For ith soldier: consider he/she is the middle person of 3 person team.\n\nThen, for a given line up, soldier can be part of smallL * bigR teams if the ratings
peacewalker
NORMAL
2020-03-29T05:35:14.181879+00:00
2020-03-29T05:36:03.668671+00:00
310
false
For `i`th soldier: consider he/she is the middle person of 3 person team.\n\nThen, for a given line up, soldier can be part of `smallL * bigR` teams if the ratings are in `ascending` order. \nSimalarly, `bigL * smallR` for ratings in `descending` order.\n```\nclass Solution {\n public int numTeams(int[] rating) {\n int teams = 0;\n int smallL = 0, smallR = 0;\n int bigL = 0, bigR = 0;\n \n for(int i = 1; i < rating.length-1; i++) {\n for(int j = i-1; j >=0; j--) {\n if(rating[i] > rating[j]) {\n smallL++;\n } else {\n bigL++;\n }\n }\n for(int j = i+1; j < rating.length; j++) {\n if(rating[i] < rating[j]) {\n bigR++;\n } else {\n smallR++;\n }\n }\n teams += (smallL * bigR) + (bigL * smallR);\n smallL = smallR = bigR = bigL = 0;\n }\n return teams;\n }\n}\n```
4
0
[]
1
count-number-of-teams
JavaScript, DP O(N^2), BruteForce O(N^3)
javascript-dp-on2-bruteforce-on3-by-hon9-1gn5
Brute Force\n- Time Complexity: O(N^3)\n- Space Complexity: O(1)\njavaScript\n/**\n * @param {number[]} rating\n * @return {number}\n */\nvar numTeams = functio
hon9g
NORMAL
2020-03-29T04:23:36.458824+00:00
2020-03-30T07:02:44.873412+00:00
742
false
### Brute Force\n- Time Complexity: O(N^3)\n- Space Complexity: O(1)\n```javaScript\n/**\n * @param {number[]} rating\n * @return {number}\n */\nvar numTeams = function(rating) {\n let y = 0;\n for (let i = 0; i + 2 < rating.length; i++) {\n for (let j = i + 1; j + 1 < rating.length; j++) {\n for (let k = j + 1; k < rating.length; k++) {\n if (rating[i] < rating[j] && rating[j] < rating[k]) y++;\n if (rating[i] > rating[j] && rating[j] > rating[k]) y++;\n }\n }\n }\n return y;\n};\n```\n### DP\n- Time Complexity: O(N^2)\n- Space Complexity: O(N)\n```JavaScript\n/**\n * @param {number[]} rating\n * @return {number}\n */\nvar numTeams = function(rating) {\n const des = new Array(rating.length).fill(0),\n asc = new Array(rating.length).fill(0);\n for (let i = rating.length - 2; 0 <= i; i--) {\n for (let j = i + 1; j < rating.length; j++) {\n if (rating[i] > rating[j]) {\n des[i]++;\n }\n if (rating[i] < rating[j]) {\n asc[i]++;\n }\n }\n }\n let y = 0;\n for (let i = 0; i + 2 < rating.length; i++) {\n for (let j = i + 1; j + 1 < rating.length; j++) {\n if (rating[i] > rating[j]) {\n y += des[j];\n }\n if (rating[i] < rating[j]) {\n y += asc[j]\n }\n }\n }\n return y;\n};\n```
4
1
['JavaScript']
1
count-number-of-teams
Clean Python 3, DFS
clean-python-3-dfs-by-lenchen1112-m1ew
Straightforward DFS solution.\n\nTime: C(N, 3) = O(N ^ 3)\nSpace: O(N), deep of dfs tree.\n\nclass Solution:\n def numTeams(self, rating: List[int]) -> int:\
lenchen1112
NORMAL
2020-03-29T04:10:40.953925+00:00
2020-03-29T04:24:45.034911+00:00
810
false
Straightforward DFS solution.\n\nTime: `C(N, 3) = O(N ^ 3)`\nSpace: `O(N)`, deep of dfs tree.\n```\nclass Solution:\n def numTeams(self, rating: List[int]) -> int:\n def dfs(i: int, prefix: List[int], increasing: bool) -> int:\n if len(prefix) == 3: return 1\n if i == len(rating): return 0\n result = 0\n rate = rating[i]\n if (increasing and prefix[-1] < rate) or (not increasing and prefix[-1] > rate):\n result += dfs(i+1, prefix + [rate], increasing)\n result += dfs(i+1, prefix, increasing)\n return result\n\n result = 0\n for i in range(len(rating)):\n result += dfs(i + 1, [rating[i]], True) + dfs(i + 1, [rating[i]], False)\n return result\n```
4
1
[]
2
count-number-of-teams
Java DP
java-dp-by-hobiter-f8ma
\nclass Solution {\n int[] arr;\n public int numTeams(int[] rating) {\n arr = rating;\n return findLarge() + findSmall();\n }\n privat
hobiter
NORMAL
2020-03-29T04:05:12.552718+00:00
2020-03-29T04:24:15.884964+00:00
887
false
```\nclass Solution {\n int[] arr;\n public int numTeams(int[] rating) {\n arr = rating;\n return findLarge() + findSmall();\n }\n private int findLarge() {\n int[] dp = new int[arr.length];\n // first loop, count of smaller on the left;\n for (int i = 0; i < arr.length; i++) {\n for (int j = 0; j < i; j++) {\n if (arr[j] < arr[i]) {\n dp[i]++;\n }\n }\n }\n //second loop, collct count of subsequence ending of i;\n int res = 0;\n for (int i = 0; i < arr.length; i++) {\n for (int j = 0; j < i; j++) {\n if (arr[j] < arr[i]) {\n res += dp[j];\n }\n }\n }\n return res;\n }\n private int findSmall() { // same as count Larger\n int[] dp = new int[arr.length];\n for (int i = 0; i < arr.length; i++) {\n for (int j = 0; j < i; j++) {\n if (arr[j] > arr[i]) {\n dp[i]++;\n }\n }\n }\n int res = 0;\n for (int i = 0; i < arr.length; i++) {\n for (int j = 0; j < i; j++) {\n if (arr[j] > arr[i]) {\n res += dp[j];\n }\n }\n }\n return res;\n }\n}\n```
4
0
[]
2
minimum-moves-to-convert-string
C++ Easy to understand, no change in string
c-easy-to-understand-no-change-in-string-sju5
When we encounter a \'X\' we will make a move, irrespective of the fact that there might be \'O\'. After the move we will move the pointer by 3 steps since we a
badri7489
NORMAL
2021-10-03T05:21:25.267570+00:00
2021-10-03T05:21:25.267614+00:00
5,313
false
### When we encounter a \'X\' we will make a ***move***, irrespective of the fact that there might be \'O\'. After the move we will move the pointer by 3 steps since we are gauranteed that there won\'t be any more \'X\' till the position we have swapped.\n\n```\nint minimumMoves(string s) {\n\tint i = 0, n = s.length(), count = 0;\n\twhile (i < n) {\n\t\tif (s[i] == \'O\') // If we find \'O\' we simply move the pointer one step\n\t\t\ti++;\n\t\telse\n\t\t\tcount++, i += 3; // When we find \'X\' we increment the count and move the pointer by 3 steps\n\t}\n\treturn count;\n}\n```\n\n**Time Complexity: O(N)**\n**Space Complexity: O(1)**
93
1
['C', 'C++']
13
minimum-moves-to-convert-string
Java | O(N) greedy with explanation how to come up with the greedy idea
java-on-greedy-with-explanation-how-to-c-gox8
Many of us can write the correct greedy strategy. But most discussions don\'t tell how it works and why it works here. If you are interested please read the fol
pollux1997
NORMAL
2021-10-03T05:07:58.817942+00:00
2021-10-04T20:15:37.990561+00:00
4,294
false
Many of us can write the correct greedy strategy. But most discussions don\'t tell how it works and why it works here. If you are interested please read the following part:)\n\nHere\'s my explanation:\n1.Because one move have to change three consecutive characters together. Change \'OOO\' is just a waste of opportunities. We can make list of all the possible situations:\n```\n// one X\nOOX OXO XOO\n//two X\nXXO XOX OXX\n//three X\nXXX\n```\nAnd we define them as basic element here. All the move we will change is those basic elements.\n2.There\'s some identical situations. eg. \n```\n//OOX is identical to XOO and OXO when the sequence is OOXOO\n//XXO is identical to OXX when the sequence is OXXO\n```\n3.Those situations indicates that we can get the same result (remove some X) and move step despite the basic elements are different.\n4.We can find an representative for group of elements and here comes with our greedy strategy: once we find an X, we add one to the steps and move the pointer 3 indexes away.\n\nThe basic idea of greedy strategy is to find the common part of situations so we can detect them using less operations. (Just like the find a dophine use the hump in its head.)\n\n```\nclass Solution {\n public int minimumMoves(String s) {\n int i=0;\n int step=0;\n while(i<s.length()){\n if(s.charAt(i)==\'X\'){\n i=i+3;\n step++;\n }\n else{\n i++;\n }\n }\n return step;\n }\n}\n```\nupvote if you really like it.
59
0
['Greedy', 'Java']
4
minimum-moves-to-convert-string
[Python3] scan
python3-scan-by-ye15-1b65
\n\nclass Solution:\n def minimumMoves(self, s: str) -> int:\n ans = i = 0\n while i < len(s): \n if s[i] == "X": \n
ye15
NORMAL
2021-10-03T04:22:24.876321+00:00
2021-10-03T04:22:24.876369+00:00
2,843
false
\n```\nclass Solution:\n def minimumMoves(self, s: str) -> int:\n ans = i = 0\n while i < len(s): \n if s[i] == "X": \n ans += 1\n i += 3\n else: i += 1\n return ans \n```
36
0
['Python3']
3
minimum-moves-to-convert-string
Greedy
greedy-by-votrubac-vyle
When going left-to-right, we must change \'X\' we encounter. When it happen, we advance our pointer two additional steps, and increment the result.\n\nC++\ncpp\
votrubac
NORMAL
2021-10-04T03:19:09.669803+00:00
2021-10-04T08:06:24.925061+00:00
1,296
false
When going left-to-right, we must change \'X\' we encounter. When it happen, we advance our pointer two additional steps, and increment the result.\n\n**C++**\n```cpp\nint minimumMoves(string s) {\n int res = 0;\n for (int i = 0; i < s.size(); i += s[i] == \'X\' ? 3 : 1)\n res += s[i] == \'X\';\n return res;\n}\n```
24
0
[]
3
minimum-moves-to-convert-string
✅ Short & Self Explanatory | 3 Approaches | C++ | Beginner friendly
short-self-explanatory-3-approaches-c-be-pyii
1.recursion\n\nclass Solution {\npublic:\n int minimumMoves(string s,int index=0) {\n if(index >= s.size()){\n return 0;\n }\n
rajat_gupta_
NORMAL
2021-10-03T07:46:41.478808+00:00
2021-10-03T07:46:41.478839+00:00
691
false
**1.recursion**\n```\nclass Solution {\npublic:\n int minimumMoves(string s,int index=0) {\n if(index >= s.size()){\n return 0;\n }\n \n if(s[index] == \'X\'){\n return minimumMoves(s,index+3)+1;\n }else{\n return minimumMoves(s,index+1);\n }\n }\n};\n```\n**2.traversing the complete string**\n```\nclass Solution {\npublic:\n int minimumMoves(string s) {\n int moves = 0;\n s += "OO";\n for(int i = 0; i < s.size(); i++) {\n if(s[i] == \'X\') {\n s[i] = s[i + 1] = s[i + 2] = \'O\';\n moves++;\n }\n }\n return moves;\n }\n};\n```\n**3.**\n```\nclass Solution {\npublic:\n int minimumMoves(string s) {\n int moves=0;\n for(int i=0;i<s.length();){\n if(s[i]==\'X\'){\n i+=3,moves++;\n }else{\n i++;\n }\n }\n return moves;\n }\n};\n```\n**Feel free to ask any question in the comment section.**\nI hope that you\'ve found the solution useful.\nIn that case, **please do upvote and encourage me** to on my quest to document all leetcode problems\uD83D\uDE03\nHappy Coding :)\n
12
2
['C', 'C++']
1
minimum-moves-to-convert-string
[Java] O(N) + Easy to understand + Important point
java-on-easy-to-understand-important-poi-mu11
The important thing that you might be missing in your solution is that you might be counting 3 letter groups, which won\'t give you the correct answer.\n\nWhy?\
pikachu_approves
NORMAL
2021-10-06T20:51:45.393674+00:00
2021-10-06T20:51:45.393706+00:00
1,289
false
The important thing that you might be missing in your solution is that you might be counting 3 letter groups, which won\'t give you the correct answer.\n\nWhy?\n\nThe reason for this is a test case like this:\n```\n"XXXOXXOXOXO"\n```\n\nSo, if you\'re counting letter groups of 3 at a time, you will count 1 for the first group `XXX` and then you will count 2 for `OXX` and then 3 for `OXO` and 4 for `XO`.\n\nBut this is not the minimum count, which in this case would be 3. \n\nHow?\nYou should count 1 for `XXX` and then 2 for `XXO` (skip `O` in between) and then 3 for `XOX`.\n\nBasically, you should not be counting the `O`s which can be skipped.\n\nNow that you know why this won\'t work, you might also get to the part that you should only increment your count when dealing with an `X`.\n\nSo a simple greedy approach would be something like this:\n\n```\nclass Solution {\n public int minimumMoves(String s) {\n int count = 0;\n \n for (int i = 0; i < s.length();) { \n if (s.charAt(i) == \'X\') {\n count++;\n i += 3;\n } else {\n i++;\n }\n }\n \n return count;\n }\n}\n```\n\nThe greedy approach is that if we find an `X` we count a step immediately and skip over 3 (irrespective of `X` or `O` since we can deal it all in one step).\n\nHope it helps!
11
0
['Greedy', 'Java']
1
minimum-moves-to-convert-string
Short Java, O(n), 7 lines
short-java-on-7-lines-by-climberig-b8x7
Walk to the first X, jump three position to the right, walk to the next X, jump... Count the number of jumps.\n```java\n public int minimumMoves(String s) {\n
climberig
NORMAL
2021-10-03T04:47:12.026084+00:00
2021-10-03T05:10:52.761453+00:00
570
false
Walk to the first X, jump three position to the right, walk to the next X, jump... Count the number of jumps.\n```java\n public int minimumMoves(String s) {\n int r = 0;\n for (int i = 0; i < s.length(); i++)\n if (s.charAt(i) == \'X\') {\n r++;\n i += 2;\n }\n return r;\n }
8
0
[]
1
minimum-moves-to-convert-string
Easy Python Solution | Faster than 99% (24 ms)
easy-python-solution-faster-than-99-24-m-dlz5
Easy Python Solution | Faster than 99% (24 ms)\nRuntime: 24 ms, faster than 99% of Python3 online submissions for Minimum Moves to Convert String.\nMemory Usage
the_sky_high
NORMAL
2021-10-17T08:55:33.677650+00:00
2021-10-17T08:55:33.677678+00:00
1,106
false
# Easy Python Solution | Faster than 99% (24 ms)\n**Runtime: 24 ms, faster than 99% of Python3 online submissions for Minimum Moves to Convert String.\nMemory Usage: 14.2 MB.**\n\n```\nclass Solution:\n def minimumMoves(self, s: str) -> int:\n i, m = 0, 0\n l = len(s)\n\n while i < l:\n if s[i] != \'X\':\n i += 1\n elif \'X\' not in s[i:i+1]:\n i += 2\n elif \'X\' in s[i:i+2]:\n m += 1\n i += 3\n return m\n```
6
0
['Python', 'Python3']
2
minimum-moves-to-convert-string
Short 1-liner Python Java Ruby
short-1-liner-python-java-ruby-by-stefan-rind
Ruby:\n\ndef minimum_moves(s)\n s.scan(/X.?.?/).size\nend\n\n\nPython:\n\ndef minimumMoves(self, s: str) -> int:\n return subn(\'X.?.?\', \'\', s)[1]\n\n\nJ
stefan4trivia
NORMAL
2021-10-03T20:19:11.117992+00:00
2021-10-03T21:03:12.133967+00:00
164
false
Ruby:\n```\ndef minimum_moves(s)\n s.scan(/X.?.?/).size\nend\n```\n\nPython:\n```\ndef minimumMoves(self, s: str) -> int:\n return subn(\'X.?.?\', \'\', s)[1]\n```\n\nJava:\n```\npublic int minimumMoves(String s) {\n return s.replaceAll("O*(X?).?.?", "$1").length();\n}\n```
6
2
[]
2
minimum-moves-to-convert-string
EASY JAVA SOLUTION O(n) time complexity!!!!
easy-java-solution-on-time-complexity-by-irpj
Approachtake two variables 'i' for iterating through the string elements and 'step' for keeping track of the no of steps. run a while loop through all the eleme
Mrinmoy_1315
NORMAL
2024-12-22T18:41:40.284179+00:00
2024-12-22T18:41:40.284179+00:00
519
false
# Approach take two variables **'i'** for iterating through the string elements and **'step'** for keeping track of the no of steps. run a while loop through all the elements of the string. if found an 'X' increment the pointer i an include three consecutive elemnts thereby considering it as a step. Increment **'step'** by 1. or else incremnt **'i'** by 1. break the loop and return the value storedin the variable **'step'** AND VOILAA!! # Complexity - Time complexity: **O(n)** - Space complexity: **O(1)** # Code ```java [] class Solution { public int minimumMoves(String s) { int i=0, step=0; while(i<s.length()){ if(s.charAt(i)=='X'){ i+=3; step++; }else i++; } return step; } } ```
4
0
['Java']
0
minimum-moves-to-convert-string
Beats 100% | C++
beats-100-c-by-manavtore-11b9
Approach\n1. Initialize moves to 0 for counting moves.\n\n2. Use a loop to traverse the string.\n\n3. When an \'X\' is found, increment moves and skip the next
manavtore
NORMAL
2024-07-27T09:01:41.704302+00:00
2024-07-27T09:01:41.704339+00:00
77
false
# Approach\n1. Initialize moves to 0 for counting moves.\n\n2. Use a loop to traverse the string.\n\n3. When an \'X\' is found, increment moves and skip the next two characters by moving three steps forward (i += 3).\n\n4. If the character is \'O\', move to the next character.\n\n5. Return the total number of moves.\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution {\npublic:\n int minimumMoves(string s) {\n int moves = 0;\n int n = s.size();\n int i = 0;\n \n while(i<n){\n if(s[i]==\'X\'){\n s[i]=\'0\';\n moves++;\n i+=3;\n }else{\n i++;\n }\n }\n return moves;\n }\n};\n```
4
0
['C++']
0
minimum-moves-to-convert-string
simple and easy C++ solution 😍❤️‍🔥
simple-and-easy-c-solution-by-shishirrsi-x38g
if it\'s help, please up \u2B06 vote! \u2764\uFE0F\n\n\n# Code\n\nclass Solution {\npublic:\n int minimumMoves(string s) \n {\n int i = 0, ans = 0,
shishirRsiam
NORMAL
2024-05-26T12:19:38.541601+00:00
2024-05-26T12:19:38.541622+00:00
258
false
# if it\'s help, please up \u2B06 vote! \u2764\uFE0F\n\n\n# Code\n```\nclass Solution {\npublic:\n int minimumMoves(string s) \n {\n int i = 0, ans = 0, n = s.size();\n while(i < n)\n {\n while(s[i]==\'0\') i++;\n if(s[i]==\'X\') \n {\n ans++;\n i += 3;\n }\n else i++;\n }\n return ans;\n }\n};\n```
4
0
['String', 'Greedy', 'C++']
0
minimum-moves-to-convert-string
Java | While Loop | Simple Logic
java-while-loop-simple-logic-by-divyansh-qu7c
\nclass Solution {\n public int minimumMoves(String s) {\n int i=0,step=0;\n while(i<s.length()){\n if(s.charAt(i)==\'X\'){\n
Divyansh__26
NORMAL
2022-09-16T07:55:41.799866+00:00
2022-09-16T07:55:41.799910+00:00
522
false
```\nclass Solution {\n public int minimumMoves(String s) {\n int i=0,step=0;\n while(i<s.length()){\n if(s.charAt(i)==\'X\'){\n i=i+3;\n step++;\n }\n else\n i++;\n }\n return step;\n }\n}\n```\nKindly upvote if you like the code.
4
0
['String', 'Java']
1
minimum-moves-to-convert-string
C++||Easy to Understand
ceasy-to-understand-by-return_7-negr
```\nclass Solution {\npublic:\n int minimumMoves(string s)\n {\n int ans=0;\n for(int i=0;i<s.size();i++)\n {\n if(s[i]==
return_7
NORMAL
2022-08-22T21:06:56.191054+00:00
2022-08-22T21:06:56.191089+00:00
341
false
```\nclass Solution {\npublic:\n int minimumMoves(string s)\n {\n int ans=0;\n for(int i=0;i<s.size();i++)\n {\n if(s[i]==\'X\')\n {\n ans++;\n i=i+2;\n }\n }\n return ans;\n \n }\n};\n//if you find the solution useful plz upvote.
4
0
['C']
0
minimum-moves-to-convert-string
[Python 3] Simple Solution O(n) with Explanation
python-3-simple-solution-on-with-explana-wdax
General idea:\n\nGo through each character. if it\'s X, then we count once and jump to 3 characters ahead; if it\'s O, then we ignore it and go on. In this way,
zhouxu_ds
NORMAL
2021-12-23T16:35:17.873502+00:00
2021-12-23T16:35:17.873528+00:00
277
false
General idea:\n\nGo through each character. if it\'s X, then we count once and jump to 3 characters ahead; if it\'s O, then we ignore it and go on. In this way, we count all the moves.\n\n```\ndef minimumMoves(self, s: str) -> int:\n res, i = 0, 0\n while i < len(s):\n if s[i] == \'X\':\n i += 3\n res += 1\n else:\n i += 1\n return res
4
0
['Python']
0
minimum-moves-to-convert-string
Java 0ms 100% faster
java-0ms-100-faster-by-devangsharma7861-au82
class Solution {\n\n public int minimumMoves(String s){\n \n int step = 0;\n \n \n for(int i = 0; i < s.length(); i++){\n
devangsharma7861
NORMAL
2021-11-10T16:19:12.760263+00:00
2021-11-10T16:19:12.760293+00:00
162
false
class Solution {\n\n public int minimumMoves(String s){\n \n int step = 0;\n \n \n for(int i = 0; i < s.length(); i++){\n if(s.charAt(i) == \'X\'){\n step++;\n i+=2;\n }\n }\n \n return step;\n }\n}
4
0
[]
0
minimum-moves-to-convert-string
2027 | JavaScript 1-Line Solution
2027-javascript-1-line-solution-by-spork-ws53
Runtime: 83 ms, faster than 47.58% of JavaScript online submissions\n> Memory Usage: 39.2 MB, less than 17.18% of JavaScript online submissions\n\njavascript\n
sporkyy
NORMAL
2021-10-13T13:18:43.389043+00:00
2021-10-13T13:19:10.712407+00:00
353
false
> Runtime: **83 ms**, faster than *47.58%* of JavaScript online submissions\n> Memory Usage: **39.2 MB**, less than *17.18%* of JavaScript online submissions\n\n```javascript\n const minimumMoves = s => s.match(/X.{0,2}/g)?.length ?? 0;\n ```
4
0
['JavaScript']
0
minimum-moves-to-convert-string
c++ 100% faster easiest solution || no change in string
c-100-faster-easiest-solution-no-change-cdte9
int minimumMoves(string s) {\n int ans=0;\n for(int i=0; i<s.size();i=i)\n {\n if(s[i] == \'X\'){\n i= i+3;
gravity2000
NORMAL
2021-10-03T04:14:53.322219+00:00
2021-10-03T04:14:53.322294+00:00
274
false
int minimumMoves(string s) {\n int ans=0;\n for(int i=0; i<s.size();i=i)\n {\n if(s[i] == \'X\'){\n i= i+3;\n ans++;\n }\n else{\n i++;\n }\n }\n return ans;\n }
4
3
['String', 'Sliding Window']
0
minimum-moves-to-convert-string
Runtime: 0 ms, faster than 100.00% of C++ online submissions
runtime-0-ms-faster-than-10000-of-c-onli-v4vt
\n\tclass Solution {\n\tpublic:\n\t\tint minimumMoves(string s) {\n\t\t\tint n = s.size();\n\t\t\tint ans = 0;\n\t\t\tfor(int i=0;i<n;i++){\n\t\t\t\tif(s[i] ==
PiyasaBera
NORMAL
2022-09-18T12:53:12.960750+00:00
2022-09-18T12:53:12.960795+00:00
371
false
\n\tclass Solution {\n\tpublic:\n\t\tint minimumMoves(string s) {\n\t\t\tint n = s.size();\n\t\t\tint ans = 0;\n\t\t\tfor(int i=0;i<n;i++){\n\t\t\t\tif(s[i] == \'X\'){\n\t\t\t\t\tans++;\n\t\t\t\t\ti+=2;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn ans;\n\t\t}\n\t};
3
0
[]
2
minimum-moves-to-convert-string
EASY TO UNDERSTAND || SIMPLE JAVA CODE
easy-to-understand-simple-java-code-by-p-nqk0
\nclass Solution {\n public int minimumMoves(String s) {\n int count = 0;\n for (int i = 0; i < s.length(); i++)\n if (s.charAt(i) =
priyankan_23
NORMAL
2022-08-31T17:10:20.911573+00:00
2022-08-31T17:10:20.911615+00:00
195
false
```\nclass Solution {\n public int minimumMoves(String s) {\n int count = 0;\n for (int i = 0; i < s.length(); i++)\n if (s.charAt(i) == \'X\') {\n count++;\n i += 2;\n }\n return count;\n }\n}\n```
3
0
[]
0
minimum-moves-to-convert-string
C++ with simple explanation
c-with-simple-explanation-by-jaisw7-72zb
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
jaisw7
NORMAL
2021-10-03T17:10:32.866091+00:00
2021-10-17T16:42:32.069669+00:00
112
false
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
3
0
[]
0
minimum-moves-to-convert-string
C++ Simple and Short Solution, 0ms Faster than 100%
c-simple-and-short-solution-0ms-faster-t-ony0
\nclass Solution {\npublic:\n int minimumMoves(string s) {\n int count = 0, i = 0;\n \n while (i < s.size()) {\n while (s[i]
yehudisk
NORMAL
2021-10-03T09:05:42.896417+00:00
2021-10-03T09:05:42.896458+00:00
158
false
```\nclass Solution {\npublic:\n int minimumMoves(string s) {\n int count = 0, i = 0;\n \n while (i < s.size()) {\n while (s[i] == \'O\') i++;\n \n if (s[i] == \'X\') {\n count++;\n i += 3;\n }\n }\n \n return count;\n }\n};\n```\n**Like it? please upvote!**
3
0
['C']
0
minimum-moves-to-convert-string
100.00 % | 0 ms | C++ | easiest solution possible
10000-0-ms-c-easiest-solution-possible-b-yrfn
``` \nclass Solution {\npublic:\n int minimumMoves(string s) {\n int n = s.length() ;\n int cnt = 0;\n for(int i = 0 ; i= n-3){\n
priyanshichauhan1998x
NORMAL
2021-10-03T05:14:22.607325+00:00
2021-10-03T05:14:22.607370+00:00
63
false
``` \nclass Solution {\npublic:\n int minimumMoves(string s) {\n int n = s.length() ;\n int cnt = 0;\n for(int i = 0 ; i<n;i++){\n if(s[i] == \'X\'){ \n if(i >= n-3){\n cnt++;\n break;\n }\n else {\n i= i+2;\n cnt++;\n }\n }\n }\n return cnt;\n }\n};\n
3
0
['Math', 'String']
0
minimum-moves-to-convert-string
Python easy
python-easy-by-abkc1221-0kyf
\nclass Solution:\n def minimumMoves(self, s: str) -> int:\n res = 0\n i = 0\n while i < len(s):\n #if current is X then jump
abkc1221
NORMAL
2021-10-03T04:04:39.572141+00:00
2021-10-03T04:04:39.572178+00:00
320
false
```\nclass Solution:\n def minimumMoves(self, s: str) -> int:\n res = 0\n i = 0\n while i < len(s):\n #if current is X then jump 3 positions and count by 1\n if s[i] == \'X\':\n res += 1\n i += 3\n else:\n i += 1\n return res\n```
3
2
['Python', 'Python3']
0
minimum-moves-to-convert-string
C++ || EASY SOLUTION
c-easy-solution-by-vineet_raosahab-0uqm
\nclass Solution {\npublic:\n int minimumMoves(string s) {\n int count=0;\n for(int i=0;i<s.size()-2;i++)\n {\n if(s[i]==\'X\
VineetKumar2023
NORMAL
2021-10-03T04:02:01.238022+00:00
2021-10-11T02:08:27.183537+00:00
175
false
```\nclass Solution {\npublic:\n int minimumMoves(string s) {\n int count=0;\n for(int i=0;i<s.size()-2;i++)\n {\n if(s[i]==\'X\')\n {\n count++;\n s[i]=\'O\';\n s[i+1]=\'O\';\n s[i+2]=\'O\';\n }\n }\n if(s[s.size()-1]==\'X\' || s[s.size()-2]==\'X\')\n count++;\n return count;\n }\n};\n```
3
0
[]
0
minimum-moves-to-convert-string
Easiest Solution Ever
easiest-solution-ever-by-johncarter11-tehg
IntuitionApproachComplexity Time complexity: Space complexity: Code
AbhiBhingole
NORMAL
2025-03-24T04:30:26.333156+00:00
2025-03-24T04:30:26.333156+00:00
118
false
# Intuition ![49h9ra.jpg](https://assets.leetcode.com/users/images/4f07eb51-0024-4292-9ccb-38b04652ade2_1742790621.845352.jpeg) <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```java [] class Solution { public int minimumMoves(String s) { int i = 0; int step = 0; while(i<s.length()){ if(s.charAt(i)=='X'){ i = i+3; step++; } else{ i++; } } return step; } } ```
2
0
['Java']
0
minimum-moves-to-convert-string
Rust | 0ms | O(n) | beats 100%
rust-0ms-on-beats-100-by-user7454af-n4ni
Intuition\n\n\n# Approach\nWhen an \'X\' is encountered, increment change count, and there are two more converts reamining for the next two indices. Keep decrem
user7454af
NORMAL
2024-06-29T23:56:55.748174+00:00
2024-06-29T23:56:55.748198+00:00
129
false
# Intuition\n\n\n# Approach\nWhen an \'X\' is encountered, increment change count, and there are two more converts reamining for the next two indices. Keep decrementing remaining if its greater than 0 and increment change count only if remaining is 0.\n\n# Complexity\n- Time complexity: $$O(n)$$\n\n- Space complexity: $$O(1)$$\n\n# Code\n```\nimpl Solution {\n pub fn minimum_moves(s: String) -> i32 {\n let (mut ans, mut remaining) = (0, 0);\n for b in s.into_bytes() {\n if b == b\'X\' {\n if remaining == 0 {\n ans += 1;\n remaining = 3;\n }\n }\n if remaining > 0 {\n remaining -= 1;\n }\n }\n ans\n }\n}\n```
2
0
['Rust']
0
minimum-moves-to-convert-string
Simple java code 0 ms beats 100 %
simple-java-code-0-ms-beats-100-by-arobh-odjf
\n# Complexity\n-\n\n# Code\n\nclass Solution {\n public int minimumMoves(String s) {\n char[] ch=s.toCharArray();\n int ctr=0;\n for(in
Arobh
NORMAL
2024-01-10T15:03:04.245203+00:00
2024-01-10T15:03:04.245238+00:00
183
false
\n# Complexity\n-\n![image.png](https://assets.leetcode.com/users/images/fc490b4e-89c0-4ecf-a32a-e0c1c1c6c910_1704898978.7593596.png)\n# Code\n```\nclass Solution {\n public int minimumMoves(String s) {\n char[] ch=s.toCharArray();\n int ctr=0;\n for(int i=0;i<ch.length;i++)\n {\n if(ch[i]==\'X\')\n {\n i+=2;\n ctr++;\n }\n }\n return ctr;\n }\n}\n```
2
0
['Java']
0
minimum-moves-to-convert-string
6 lines c++ easy solution | O(n) time
6-lines-c-easy-solution-on-time-by-wagdy-hlja
Approach\nwhenever we find an \'X\' we will convert it and the following 2 elements (so, no need to check them anyways)\n\n# Complexity\n- Time complexity:\nO(n
WagdySamih
NORMAL
2023-05-05T00:39:11.889252+00:00
2023-05-05T00:40:34.218148+00:00
415
false
# Approach\nwhenever we find an \'X\' we will convert it and the following 2 elements (so, no need to check them anyways)\n\n# Complexity\n- Time complexity:\nO(n)\n- Space complexity:\nO(1)\n# Code\n```\nclass Solution {\npublic:\n int minimumMoves(string s) {\n int sum = 0;\n int sz = s.size();\n for(int i = 0 ; i <sz ; i++) {\n while(i<sz && s[i]==\'X\') sum++, i+=3;\n }\n return sum;\n }\n};\n```
2
0
['C++']
0
minimum-moves-to-convert-string
2027. Run time - 95.42% | Memory - 87%
2027-run-time-9542-memory-87-by-ramana72-etac
Code\n\nclass Solution:\n def minimumMoves(self, s: str) -> int:\n ind, moves = 0, 0\n n = len(s)\n while ind < n:\n if s[ind
ramana721
NORMAL
2023-03-19T06:13:30.708881+00:00
2023-03-19T06:13:30.708916+00:00
771
false
# Code\n```\nclass Solution:\n def minimumMoves(self, s: str) -> int:\n ind, moves = 0, 0\n n = len(s)\n while ind < n:\n if s[ind] == \'X\':\n ind += 3\n moves += 1\n else:\n ind += 1\n return moves\n\n```
2
0
['String', 'Greedy', 'Python3']
1
minimum-moves-to-convert-string
C++ - 2 Solutions - Beats 100 % - Easy
c-2-solutions-beats-100-easy-by-nipunrat-4u5l
# Intuition \n\n\n\n\n\n# Complexity\n- Time complexity:O(n)\n\n\n- Space complexity:O(1)\n\n\n# Code\n\nclass Solution {\npublic:\n int minimumMoves(strin
nipunrathore
NORMAL
2023-02-06T16:26:42.223596+00:00
2023-02-06T16:26:42.223641+00:00
617
false
<!-- # Intuition -->\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n<!-- # Approach -->\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:$$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int minimumMoves(string s) {\n int moves = 0 ; \n int n = s.length() ; \n int i = 0 ; \n while (i < n)\n {\n if (s[i] == \'X\')\n {\n moves ++ ;\n i += 3 ;\n }\n else\n {\n i ++ ;\n }\n }\n return moves ; \n }\n};\n```\n![815a317f-9cdf-46e2-a397-af8869dafa2e_1673498197.3721023.png](https://assets.leetcode.com/users/images/c45dbdd3-b68d-4f5c-ab0a-d66c5f9e927a_1675700798.694205.png)\n
2
0
['String', 'Greedy', 'C++']
0
minimum-moves-to-convert-string
Simple C# solution
simple-c-solution-by-khaliljel04-u1sj
Approach\nIf you find \'O\' jump to next letter.\nIf you find \'X\' ignore the next two letters and to the third next and count that as a move.\n\n# Complexity\
Khaliljel04
NORMAL
2022-11-06T22:30:44.459677+00:00
2022-11-06T22:30:44.459701+00:00
140
false
# Approach\nIf you find \'O\' jump to next letter.\nIf you find \'X\' ignore the next two letters and to the third next and count that as a move.\n\n# Complexity\n- Time complexity: O(n)\n- Space complexity: O(1)\n\n# Code\n```\npublic class Solution {\n public int MinimumMoves(string s) {\n int moves = 0;\n for (int i = 0; i < s.Length; i++) {\n if (s[i] != \'O\') { \n moves++;\n i += 2;\n }\n }\n return moves;\n }\n}\n```
2
0
['C#']
0
minimum-moves-to-convert-string
c++ | easy to understand | step by step
c-easy-to-understand-step-by-step-by-ven-3ofc
\n\n# Code\n\nclass Solution {\npublic:\n\tint minimumMoves(string s) {\n\t\tint n = s.size();\n\t\tint ans = 0;\n\t\tfor(int i=0;i<n;i++){\n\t\t\tif(s[i] == \'
venomhighs7
NORMAL
2022-09-30T12:47:01.901007+00:00
2022-09-30T12:47:01.901041+00:00
510
false
\n\n# Code\n```\nclass Solution {\npublic:\n\tint minimumMoves(string s) {\n\t\tint n = s.size();\n\t\tint ans = 0;\n\t\tfor(int i=0;i<n;i++){\n\t\t\tif(s[i] == \'X\'){\n\t\t\t\tans++;\n\t\t\t\ti+=2;\n\t\t\t}\n\t\t}\n\t\treturn ans;\n\t}\n};\n```
2
0
['C++']
1
minimum-moves-to-convert-string
Javascript solution - Easy understanding
javascript-solution-easy-understanding-b-cbat
\nvar minimumMoves = function(s) {\n let move = 0;\n let i = 0;\n while(i<s.length){\n let char = s[i];\n\t\t// incrementing the index if we alr
SathishGunasekaran
NORMAL
2022-08-02T16:39:22.185831+00:00
2022-08-02T16:39:40.289043+00:00
318
false
```\nvar minimumMoves = function(s) {\n let move = 0;\n let i = 0;\n while(i<s.length){\n let char = s[i];\n\t\t// incrementing the index if we already have \'O\'\n if(char== \'O\'){\n i++;\n }\n\t\t// incrementing the move and index by 3 (Per move = 3 characters)\n if(char== \'X\'){\n i=i+3;\n move++;\n }\n }\n return move;\n};\n```
2
0
['Greedy', 'JavaScript']
1
minimum-moves-to-convert-string
Java Solution
java-solution-by-solved-64ru
\nclass Solution {\n public int minimumMoves(String s) {\n int index = 0;\n int result = 0;\n \n while (index < s.length()) {\n
solved
NORMAL
2022-03-25T20:24:08.469092+00:00
2022-03-25T20:24:08.469121+00:00
310
false
```\nclass Solution {\n public int minimumMoves(String s) {\n int index = 0;\n int result = 0;\n \n while (index < s.length()) {\n if (s.charAt(index) == \'X\') {\n index = index + 2;\n result++;\n }\n index++;\n }\n \n return result;\n }\n}\n```
2
0
['Java']
1
minimum-moves-to-convert-string
C++ | 3-lines code | Faster than 100%
c-3-lines-code-faster-than-100-by-dhruvj-s6xm
\n int minimumMoves(string s) {\n int ans = 0;\n \n for(int i=0; i<s.size(); i++)\n if(s[i] == \'X\'){\n ans++
Dhruvjha
NORMAL
2022-03-07T20:13:30.594484+00:00
2022-03-07T20:13:30.594512+00:00
196
false
```\n int minimumMoves(string s) {\n int ans = 0;\n \n for(int i=0; i<s.size(); i++)\n if(s[i] == \'X\'){\n ans++;\n i += 2;\n } \n return ans;\n }\n```
2
0
['C', 'C++']
0
minimum-moves-to-convert-string
Java Easy Efficient Fastest Solution
java-easy-efficient-fastest-solution-by-0ywk8
\nclass Solution {\n public int minimumMoves(String s) \n {\n int moves = 0;\n int i=0;\n \n while(i<s.length()){\n
arpit2304
NORMAL
2022-02-10T16:33:38.492838+00:00
2022-02-10T16:33:38.492888+00:00
255
false
```\nclass Solution {\n public int minimumMoves(String s) \n {\n int moves = 0;\n int i=0;\n \n while(i<s.length()){\n if(s.charAt(i)==\'X\'){\n i+=3;\n moves++;\n }else\n i++;\n } \n return moves;\n }\n}\n```\nPlease **UPVOTE** if you find this solution helpful.\nThanks : )
2
0
['Java']
2
minimum-moves-to-convert-string
Simple Python Solution
simple-python-solution-by-anish_adnani-luu8
\nclass Solution:\n def minimumMoves(self, s: str) -> int:\n \n moves = 0\n x=0\n while x<=len(s)-1:\n #print(x)\n
anish_adnani
NORMAL
2021-10-16T19:16:01.539362+00:00
2021-10-16T19:16:01.539422+00:00
62
false
```\nclass Solution:\n def minimumMoves(self, s: str) -> int:\n \n moves = 0\n x=0\n while x<=len(s)-1:\n #print(x)\n if s[x] == \'X\':\n # print("inside")\n moves+=1\n x = x+2\n \n \n x=x+1\n \n return moves\n \n \n```
2
0
[]
0
minimum-moves-to-convert-string
[Python3] O(N)
python3-on-by-sacharya1-lxt6
\tclass Solution:\n\t\tdef minimumMoves(self, s: str) -> int:\n\t\t\tcount=i=0\n\t\t\twhile i<len(s):\n\t\t\t\tif s[i]=="X":\n\t\t\t\t\tcount+=1\n\t\t\t\t\ti+=3
sacharya1
NORMAL
2021-10-05T22:46:42.849544+00:00
2021-10-05T22:46:42.849587+00:00
125
false
\tclass Solution:\n\t\tdef minimumMoves(self, s: str) -> int:\n\t\t\tcount=i=0\n\t\t\twhile i<len(s):\n\t\t\t\tif s[i]=="X":\n\t\t\t\t\tcount+=1\n\t\t\t\t\ti+=3\n\t\t\t\telse:\n\t\t\t\t\ti+=1\n\t\t\treturn count
2
0
[]
0
minimum-moves-to-convert-string
Rust solution
rust-solution-by-bigmih-0tjc
\nimpl Solution {\n pub fn minimum_moves(s: String) -> i32 {\n s.into_bytes()\n .iter()\n .enumerate()\n .filter(|&(_
BigMih
NORMAL
2021-10-04T17:07:14.197646+00:00
2021-10-04T17:07:14.197737+00:00
93
false
```\nimpl Solution {\n pub fn minimum_moves(s: String) -> i32 {\n s.into_bytes()\n .iter()\n .enumerate()\n .filter(|&(_, b)| *b == b\'X\')\n .fold((0, 0), |(mut moves, mut next_ind), (ind, b)| {\n if ind >= next_ind {\n moves += 1;\n next_ind = ind + 3\n };\n (moves, next_ind)\n })\n .0\n }\n}\n```
2
0
['Rust']
1
minimum-moves-to-convert-string
Golang solution with explanation and images
golang-solution-with-explanation-and-ima-7gxw
The idea of this solution is if we are on a \'X\' we can move the index up by three and add one to res.\n\nAn example could be:\n\ninput: s = "XXOXXXOOOXOXOXX"
nathannaveen
NORMAL
2021-10-04T01:20:40.976848+00:00
2021-10-04T01:21:46.001877+00:00
114
false
The idea of this solution is if we are on a `\'X\'` we can move the index up by three and add one to `res`.\n\nAn example could be:\n\n`input: s = "XXOXXXOOOXOXOXX"` *(I tried to capture as many edge cases as I could in this test case)*\n\nWe can start with our index `i = 0`\n\n![](https://i.imgur.com/veKMehQ.png)\n\n`s[i] == \'X\'`, so we can skip the next two values (Skip the values at indexes `1` and `2` because according to the problem we have made `s[0] = \'O\', s[1] = \'O\', s[2] = \'O\'`. We don\'t actually change the values because there is no need) and add one to `res`. So now `i = 3`, `res = 1`.\n\n![](https://i.imgur.com/Kym8Qw1.png)\n\n`s[i] == \'X\'`, so we can do the same thing as the previous `3` values, and skip the next two values. Now we are at `i = 6`, `res = 2`\n\n![](https://i.imgur.com/G0FoLNM.png)\n\n`s[i], s[i + 1]`, and `s[i + 2]` are all `\'O\'`, so I am just going to skip them.\n\n![](https://i.imgur.com/ViDnPbB.png)\n\n`s[i] == \'X\'`, so we can skip the next two values, and add one to `res`. `i == 12`, `res = 3`.\n\n![](https://i.imgur.com/DU17fi0.png)\n\n`s[i] == \'O\'`, so `i++` and continue. `i = 13`, `res = 3`\n\n![](https://i.imgur.com/iw6okPi.png)\n\n`s[i] == \'X\'`, so we can skip the next two values, `i = 15`, `res = 4`\n\nBut now `i >= len(s)`, so we can break the loop and return `res`. \n\n``` go\nfunc minimumMoves(s string) int {\n res := 0\n \n for i := 0; i < len(s); i++ {\n if s[i] == \'X\' {\n i += 2\n res++\n }\n }\n \n return res\n}\n```
2
0
['Go']
0
minimum-moves-to-convert-string
Dynamic Programming O(N) time solution with O(1) space
dynamic-programming-on-time-solution-wit-npt0
The intuition is that the minimum number of moves depends on the previous values in the string. \nThe problem can be solved via dynamic programming using the fo
shaolao
NORMAL
2021-10-03T16:51:45.378116+00:00
2021-10-03T16:51:45.378165+00:00
68
false
The intuition is that the minimum number of moves depends on the previous values in the string. \nThe problem can be solved via dynamic programming using the following relation.\n\nif current value is \'X\' => then\n`moves[index] = moves[index-3] + 1`\n\nthe reason is that the last three indices will automatically be converted to OOO if we are applying the move at the current index.\n\nIf current value is \'O\' then\n`moves[index] = moves[index-1]`\n\nbecause no move is applied so we copy the last minimum number of moves.\n\nTime Complexity : `O(N)`\nWe do a single pass over the string\n\nSpace Complexity : `O(1)`\nSince we use a circular queue of size 3 we use constant extra space. For a problem with variable length window the space complexity would be `O(W)` where W is the window length.\n\n\n```\nclass Solution {\n public int minimumMoves(String s) {\n \n // circular queue for keeping track of the minimum moves for the past three moves\n int[] moves = new int[3];\n \n int len = s.length();\n \n for (int index = 0; index < len; index++) {\n \n // index to update in the circular queue \n int id = index % moves.length;\n \n // moves[index-3] + 1\n if (s.charAt(index) == \'X\') {\n moves[id]++;\n } else {\n moves[id] = moves[ (index+2) % moves.length];// moves[index-1]\n }\n }\n \n return moves[ ( s.length()-1) % moves.length];// return the minimum moves for the last index\n }\n}\n```
2
0
[]
0
minimum-moves-to-convert-string
Easy, 100%, Simple, JAVA,O(1) Solution
easy-100-simple-javao1-solution-by-varis-44jy
class Solution {\n public int helper(String s) {\n int n=s.length(),count=0,i=0;\n while(i<n){\n char ch=s.charAt(i);\n i
varis123
NORMAL
2021-10-03T09:45:17.961627+00:00
2021-10-03T09:45:17.961668+00:00
33
false
class Solution {\n public int helper(String s) {\n int n=s.length(),count=0,i=0;\n while(i<n){\n char ch=s.charAt(i);\n if(ch==\'X\'){\n count++;\n i+=3;\n }\n else{\n i+=1;\n }\n }\n return count;\n }\n public int minimumMoves(String s) {\n return helper(s);\n }\n}
2
0
[]
0
minimum-moves-to-convert-string
[ C++ ] Easy
c-easy-by-pk_87-e5a7
```\nint minimumMoves(string s) {\n int ans=0;\n for(int i=0; i<s.size(); i++)\n {\n if(s[i] == \'X\')\n {\n
pawan_mehta
NORMAL
2021-10-03T04:03:00.338160+00:00
2021-10-03T04:03:00.338215+00:00
209
false
```\nint minimumMoves(string s) {\n int ans=0;\n for(int i=0; i<s.size(); i++)\n {\n if(s[i] == \'X\')\n {\n if(i+1<s.size())\n s[i+1]=\'O\';\n if(i+2<s.size())\n s[i+2]=\'O\';\n s[i]=\'O\';\n ans++;\n }\n }\n return ans;\n }
2
0
[]
0
minimum-moves-to-convert-string
My Simple C++ and Java Solution
my-simple-c-and-java-solution-by-vishnu2-ptnq
// C++ my simple solution\n \n\tclass Solution {\n\tpublic:\n\t\tint minimumMoves(string s) {\n\t\t\tint count = 0;\n\t\t\tfor(int i = 0; i < s.size(); i++){\n\
vishnu23kumar
NORMAL
2021-10-03T04:02:33.033529+00:00
2021-10-04T01:45:29.957558+00:00
184
false
// C++ my simple solution\n \n\tclass Solution {\n\tpublic:\n\t\tint minimumMoves(string s) {\n\t\t\tint count = 0;\n\t\t\tfor(int i = 0; i < s.size(); i++){\n\t\t\t\tif(s[i] != \'O\'){\n\t\t\t\t\tcount++;\n\t\t\t\t\ti+=2;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn count;\n\t\t}\n\t};\n\t\n\n// java solution\n\n\tclass Solution {\n\t\tpublic int minimumMoves(String s) {\n\t\t\tint count = 0;\n\t\t\tfor(int i = 0; i < s.length(); i++){\n\t\t\t\tif(s.charAt(i) != \'O\'){\n\t\t\t\t\tcount++;\n\t\t\t\t\ti+=2;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn count;\n\t\t}\n\t}
2
1
['C', 'Java']
0
minimum-moves-to-convert-string
Minimum Moves to Convert String || Beat 100% python || 0ms
minimum-moves-to-convert-string-beat-100-qecf
IntuitionApproachComplexity Time complexity:O(n) Space complexity:O(1) Code
hs024
NORMAL
2025-03-30T07:05:18.230755+00:00
2025-03-30T07:05:18.230755+00:00
58
false
# Intuition <!-- Describe your first thoughts on how to solve this problem. --> # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity:O(n) <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity:O(1) <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```python3 [] class Solution: def minimumMoves(self, s: str) -> int: move=0 i=0 n=len(s) while i<n: if s[i]=="X": move+=1 i+=3 else: i+=1 return move ```
1
0
['Python3']
0
minimum-moves-to-convert-string
Java || Runtime 100% || Memory 74%
java-runtime-100-memory-74-by-mohanraj-r-cn8s
Complexity Time complexity: O(n) Space complexity: O(1) Code
Mohanraj-R
NORMAL
2025-03-01T05:23:57.555917+00:00
2025-03-01T05:23:57.555917+00:00
198
false
# Complexity - Time complexity: $$O(n)$$ <!-- Add your time complexity here, e.g. $$O(n)$$ --> - Space complexity: $$O(1)$$ <!-- Add your space complexity here, e.g. $$O(n)$$ --> # Code ```java [] class Solution { public int minimumMoves(String s) { int count = 0; int i = 0 ; int n = s.length(); while(i < n){ if(s.charAt(i) == 'X'){ count++; i+=3; continue; } i++; } return count; } } ```
1
0
['Java']
0
minimum-moves-to-convert-string
💯 Beats || Easy and Optimal Solution || C++ 🚀
beats-easy-and-optimal-solution-c-by-dee-nkhi
✨ Intuition:The task is to determine the minimum number of moves required to convert all 'X' characters in the string s into 'O'. Each move can change up to 3 c
Deepakgariya2004
NORMAL
2025-01-10T21:34:01.676910+00:00
2025-01-10T21:34:01.676910+00:00
107
false
# ✨ Intuition: The task is to determine the minimum number of moves required to convert all 'X' characters in the string s into 'O'. Each move can change up to 3 consecutive characters starting from an 'X'. The goal is to count the minimum moves efficiently. 🌟 # 💡 Approach: 1️⃣ Start by initializing i = 0 (pointer), n (length of string s), and count = 0 (to count moves). 2️⃣ Traverse the string **while i < n:** - 🧐 If the current character s[i] is 'O', simply move the pointer ahead by 1 step. - 📈 If the current character s[i] is 'X', increment the count (one move needed) and skip the next 3 characters (i += 3) since the move covers them. 3️⃣ Finally, return count as the minimum number of moves. This solution is **O(n) in time complexity** as we process each character once and **O(1) in space complexity.** # Code ```cpp [] class Solution { public: int minimumMoves(string s) { int i=0, n=s.length(), count=0; while(i<n){ if(s[i]=='O') i++; else count++, i += 3; } return count; } }; ```
1
0
['C++']
0
minimum-moves-to-convert-string
difference between for and while loop aaj sahi maaino mein pata chala :D
difference-between-for-and-while-loop-aa-8c90
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
yesyesem
NORMAL
2024-09-03T10:19:48.329993+00:00
2024-09-03T10:19:48.330017+00:00
104
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n int minimumMoves(string s) {\nint c=0;\nint i=0;\n while(i<s.length())\n {\n if(s[i]==\'O\')\n i++;\n else\n { \n c++;\n i+=3;\n }\n \n }\n \n \nreturn c;\n }\n};\n```
1
0
['C++']
0
minimum-moves-to-convert-string
Simple Approach using one for loop
simple-approach-using-one-for-loop-by-__-500h
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
__agrim__chauhan__
NORMAL
2024-07-27T06:41:55.413064+00:00
2024-07-27T06:41:55.413100+00:00
9
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int minimumMoves(string s) {\n int n = s.size();\n int count = 0;\n for (int i = 0; i < n; i++) {\n if (s[i] == \'X\') {\n count++;\n i += 2;\n }\n }\n return count;\n }\n};\n\n```
1
0
['C++']
0
minimum-moves-to-convert-string
Simple Java Solution using Greedy | O(n) time and O(1) Space
simple-java-solution-using-greedy-on-tim-n7si
Intuition\nTry to be greedy about the utilising the "moves".\n\n# Approach\n1. Maintain a cutOff variable telling you till which index do we have all \'0\'. Ini
ayushprakash1912
NORMAL
2024-03-21T18:56:23.733875+00:00
2024-03-21T18:56:23.733895+00:00
25
false
# Intuition\nTry to be greedy about the utilising the "moves".\n\n# Approach\n1. Maintain a cutOff variable telling you till which index do we have all \'0\'. Initialise it to -1 in the beginning.\n2. Iterate through the array, and whenever encounter a \'X\'(lest say at index \'i\'), update the value of "moves" by 1(because we want all characters as \'0\'). Also, update the "cutOff" variable to (i + 2) because all and every \'X\' till (i+2) will also convert to \'0\' so we may not waste our moves.\n3. Return "moves".\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space Complexity\nO(1)\n\n\n# Code\n```\nclass Solution {\n public int minimumMoves(String s) {\n \n // cutoff: index till we have applied the operation\n int cutOff = -1, moves = 0;\n char[] arr = s.toCharArray();\n\n for(int i=0 ; i<arr.length ; i++) {\n if(arr[i]==\'X\' && i>cutOff) {\n moves++;\n cutOff = i + 2;\n }\n }\n\n return moves;\n }\n}\n```
1
0
['Greedy', 'Java']
0
minimum-moves-to-convert-string
✅ 99% beats || Python3 || While loop
99-beats-python3-while-loop-by-lutfullo_-tivc
Intuition\n\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n\n Use while.\n\n 1.Assign i pointer to while loop as given words
lutfullo_m
NORMAL
2023-10-18T09:48:47.014971+00:00
2023-10-18T09:48:47.014994+00:00
198
false
# Intuition\n![Screenshot from 2023-10-18 14-41-08.png](https://assets.leetcode.com/users/images/037b7735-243f-4693-b292-f01d5443e4a3_1697622117.6472003.png)\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n\n Use while.\n\n 1.Assign i pointer to while loop as given word`s index.\n\n 2.Ckeck if word`s char is "X", if so incraese i to 3 when it turns out "X" each time otherwise increase 1 and count each operation.\n\n\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def minimumMoves(self, s: str) -> int:\n counter = 0\n length = len(s)\n i = 0\n\n while i < length:\n\n if s[i] == "X":\n counter += 1\n i += 3\n else:\n i += 1\n return counter\n\n\nobj = Solution()\nprint(obj.minimumMoves(s="OXOXXOXX"))\n\n```
1
0
['Python3']
0
minimum-moves-to-convert-string
Python explained. [Runtime 11 ms, Beats 97.37%]
python-explained-runtime-11-ms-beats-973-er1u
\n\n# Code\n\nclass Solution(object):\n def minimumMoves(self, s):\n """\n :type s: str\n :rtype: int\n """\n \n #
saahilparmar
NORMAL
2023-04-09T15:41:19.396631+00:00
2023-04-09T15:41:19.396660+00:00
20
false
![Screenshot 2023-04-09 9.10.17 PM.png](https://assets.leetcode.com/users/images/ade7310a-56d1-4236-8e8e-ddce6f714c42_1681054849.6592138.png)\n\n# Code\n```\nclass Solution(object):\n def minimumMoves(self, s):\n """\n :type s: str\n :rtype: int\n """\n \n # Indexing int.\n a = 0\n\n # Minimum moves.\n b = 0\n\n # Looping till index is beyond length of string.\n while (a < len(s)):\n\n # If \'X\' is found.\n if s[a] == \'X\':\n\n # Index forward by 3.\n # As there is to be \'OOO\'.\n a += 3\n\n # Add 1 to b as min move.\n b += 1\n \n # If \'O\' is found.\n else:\n \n # Index forward by 1.\n a += 1\n\n # Return minimum moves.\n return b\n```
1
0
['Python']
0
minimum-moves-to-convert-string
Golang 100% fastest 0ms 2 lines of code
golang-100-fastest-0ms-2-lines-of-code-b-mgjl
\n\nfunc minimumMoves(s string) (res int) {\n for i:=0 ; i < len(s) ; i++ {\n if s[i] == \'X\' { i += 2; res++ }\n }\n return res\n\n}\n\n
gene-rode
NORMAL
2023-02-27T22:26:40.312043+00:00
2023-02-27T22:26:40.312084+00:00
36
false
\n```\nfunc minimumMoves(s string) (res int) {\n for i:=0 ; i < len(s) ; i++ {\n if s[i] == \'X\' { i += 2; res++ }\n }\n return res\n\n}\n\n```
1
0
['Go']
0
minimum-moves-to-convert-string
simple cpp solution
simple-cpp-solution-by-prithviraj26-slpr
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
prithviraj26
NORMAL
2023-01-24T18:49:45.363562+00:00
2023-01-24T18:50:01.976039+00:00
541
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\no(n)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\no(1)\n\n# Code\n```\nclass Solution {\npublic:\n int minimumMoves(string s) {\n int count=0;\n if(s.find(\'X\')==string::npos)return 0;\n int k=0;\n for(int i=0;i<s.length()-3;)\n {\n if(s[i]==\'O\')\n {\n i++;\n continue; \n }\n string a=s.substr(i,3);\n if(a.find(\'X\'!=string::npos))\n {\n count++;\n }\n k=i+3;\n i+=3;\n }\n string b=s.substr(k,s.length()-k);\n if(b.find(\'X\')!=string::npos)\n {\n count++;\n }\n return count;\n }\n};\n```
1
0
['C++']
1
minimum-moves-to-convert-string
Python3 Neat Code
python3-neat-code-by-piyushsinghgaur-i27a
Code\n\nclass Solution:\n def minimumMoves(self, s: str) -> int:\n ans=i=0\n while i<len(s):\n if s[i]==\'O\':i+=1\n else
piyushsinghgaur
NORMAL
2023-01-22T07:22:04.070828+00:00
2023-01-22T07:22:04.070871+00:00
760
false
# Code\n```\nclass Solution:\n def minimumMoves(self, s: str) -> int:\n ans=i=0\n while i<len(s):\n if s[i]==\'O\':i+=1\n else:\n i+=3\n ans+=1\n return ans\n```
1
0
['Python3']
0
minimum-moves-to-convert-string
☑️ Swift Solution | Easy to understand
swift-solution-easy-to-understand-by-clo-3s8g
Since the substitution will not be used later, it can be omitted and only the occurrences can be counted.\nMy best result\nRuntime:\xA02 ms, faster than 100.00%
clothor
NORMAL
2022-11-01T00:47:30.727921+00:00
2022-11-01T00:47:30.727962+00:00
29
false
Since the substitution will not be used later, it can be omitted and only the occurrences can be counted.\nMy best result\nRuntime:\xA0**2 ms**, faster than **100.00%** of Swift online submissions for Minimum Moves to Convert String.\n```\nclass Solution {\n\tfunc minimumMoves(_ s: String) -> Int {\n\t\tvar arr = Array(s)\n\t\tvar index = 0\n\t\tvar result = 0\n\n\t\twhile index < arr.count {\n\t\t\tif arr[index] == "O" {\n\t\t\t\tindex += 1\n\t\t\t} else {\n\t\t\t\tindex += 3\n\t\t\t\tresult += 1\n\t\t\t}\n\t\t}\n\n\t\treturn result\n\t}\n}\n```
1
0
['Swift']
0
minimum-moves-to-convert-string
PHP Simple Solution
php-simple-solution-by-leon888-ovre
\nclass Solution {\n\n /**\n * @param String $s\n * @return Integer\n */\n function minimumMoves($s) {\n $times = 0;\n while (($
leon888
NORMAL
2022-09-16T05:39:32.391516+00:00
2022-09-16T05:39:32.391562+00:00
24
false
```\nclass Solution {\n\n /**\n * @param String $s\n * @return Integer\n */\n function minimumMoves($s) {\n $times = 0;\n while (($start = strpos($s, "X")) !== false) {\n $s = substr($s, $start + 3);\n $times++;\n }\n\n return $times;\n }\n}\n```
1
0
['PHP']
0
minimum-moves-to-convert-string
Python Solution
python-solution-by-a_shekhar-tty9
```\n def minimumMoves(self, s: str) -> int:\n count = 0\n i = 0\n while i < len(s):\n if s[i] == \'X\':\n count
a_shekhar
NORMAL
2022-09-14T17:19:34.342265+00:00
2022-09-14T17:19:34.342298+00:00
471
false
```\n def minimumMoves(self, s: str) -> int:\n count = 0\n i = 0\n while i < len(s):\n if s[i] == \'X\':\n count += 1\n i += 3\n elif s.count("X") == 0:\n break\n else:\n i += 1\n return count
1
0
['Python']
0
minimum-moves-to-convert-string
Ruby - T O(n), S O(1), 100% 100%
ruby-t-on-s-o1-100-100-by-hoangphanbk10-9xao
```\n# @param {String} s\n# @return {Integer}\ndef minimum_moves(s)\n n = s.length\n i = 0\n\n result = 0\n while i < n\n if s[i] == \'X\'\n i += 3\
hoangphanbk10
NORMAL
2022-08-25T12:44:04.948402+00:00
2022-08-25T12:44:04.948456+00:00
14
false
```\n# @param {String} s\n# @return {Integer}\ndef minimum_moves(s)\n n = s.length\n i = 0\n\n result = 0\n while i < n\n if s[i] == \'X\'\n i += 3\n result += 1\n else\n i += 1\n end\n end\n\n result\nend
1
0
['Ruby']
0
minimum-moves-to-convert-string
100% Faster Very SImple o(N)
100-faster-very-simple-on-by-hustlingfor-ubjn
\nclass Solution(object):\n def minimumMoves(self, s):\n """\n :type s: str\n :rtype: int\n """\n count = 0\n i = 0
hustlingfornewjob
NORMAL
2022-08-15T20:20:14.634556+00:00
2022-08-15T20:20:14.634604+00:00
268
false
```\nclass Solution(object):\n def minimumMoves(self, s):\n """\n :type s: str\n :rtype: int\n """\n count = 0\n i = 0\n \n while i < len(s):\n if s[i] == \'O\':\n i+=1\n else:\n count+=1\n i+=3\n return count \n \n \n```
1
0
['Python']
0
minimum-moves-to-convert-string
Beats 100% (C++)
beats-100-c-by-ktheron-qf9n
\nclass Solution {\npublic:\n int minimumMoves(string s) {\n int c=0;\n for(int i=0; i<s.length(); i++)\n {\n if(s[i]==\'X\')
KTHERON
NORMAL
2022-07-19T20:48:24.805763+00:00
2022-07-19T20:48:24.805793+00:00
164
false
```\nclass Solution {\npublic:\n int minimumMoves(string s) {\n int c=0;\n for(int i=0; i<s.length(); i++)\n {\n if(s[i]==\'X\')\n {\n c++;\n s[i]=\'O\';\n if(i+1<s.length())\n s[i+1]=\'O\';\n if(i+2<s.length())\n s[i+2]=\'O\';\n }\n }\n return c;\n }\n};\n```
1
0
['C']
0
minimum-moves-to-convert-string
2027. Minimum Moves to Convert String | C#| O(N) | sliding window
2027-minimum-moves-to-convert-string-c-o-rg2j
\npublic class Solution {\n public int MinimumMoves(string s) {\n\t\tif(s==null || s.Length ==0)\n return 0;\n int windowStart =0; \n
mallikdasari
NORMAL
2022-06-28T14:06:24.906577+00:00
2022-06-28T14:09:36.777133+00:00
76
false
```\npublic class Solution {\n public int MinimumMoves(string s) {\n\t\tif(s==null || s.Length ==0)\n return 0;\n int windowStart =0; \n int result =0;\n while(windowStart<s.Length){\n if(s[windowStart]== \'O\') \n windowStart++;\n else{\n result++;\n windowStart+=3;\n }\n }\n return result;\n }\n}\n```
1
0
['Sliding Window']
0
minimum-moves-to-convert-string
C++ Solution || 0ms || 100% Faster || Greedy
c-solution-0ms-100-faster-greedy-by-anis-a0sv
Code:\n\n\nclass Solution\n{\npublic:\n int minimumMoves(string s)\n {\n int n = s.length();\n int k = 0;\n int i;\n while (i
anis23
NORMAL
2022-06-28T12:53:13.311827+00:00
2022-06-28T12:53:13.311871+00:00
252
false
**Code:**\n\n```\nclass Solution\n{\npublic:\n int minimumMoves(string s)\n {\n int n = s.length();\n int k = 0;\n int i;\n while (i < n)\n {\n if (s[i] == \'X\')\n {\n k++;\n i += 3;\n }\n else\n i++;\n }\n return k;\n }\n};\n```
1
0
['Greedy', 'C', 'C++']
0
minimum-moves-to-convert-string
C++: Easy to understand, Faster than 100%
c-easy-to-understand-faster-than-100-by-mg9dm
\nint minimumMoves(string s) {\n int res = 0;\n int it=0;\n while(it<s.length()){\n if(s[it] == \'X\'){\n res++;
akshat_1607
NORMAL
2022-06-13T13:59:01.067027+00:00
2022-06-13T13:59:01.067074+00:00
123
false
```\nint minimumMoves(string s) {\n int res = 0;\n int it=0;\n while(it<s.length()){\n if(s[it] == \'X\'){\n res++; // if \'X\' is encountered, increment result by 1 and move iterator ahead by 3 \n it+=3;\n }\n else{it++;} // else increment iterator by 1\n }\n return res;\n }\n```
1
0
['String', 'C']
0
minimum-moves-to-convert-string
c++ solution || With Approach || Faster than 100%
c-solution-with-approach-faster-than-100-xyup
APPROACH\ninitilize i=0(iterator), result=0;\niterate string s using while loop till the size of string s \nin string s if we found "X" at any index we will inc
divyanihirulkar247
NORMAL
2022-05-27T16:54:49.911357+00:00
2022-05-27T17:05:44.460699+00:00
64
false
**APPROACH**\ninitilize i=0(iterator), result=0;\niterate string s using while loop till the size of string s \nin string s if we found "X" at any index we will increase iterator by 3 and result by 1\nif we found "O" then increase iterator by 1 \nreturn result\n```\nclass Solution {\npublic:\n int minimumMoves(string s) \n {\n int n=s.length(),res=0,i=0;\n while(i<n)\n {\n if(s[i]==\'X\')\n {\n i+=3;\n res++;\n }\n else\n i++;\n }\n return res;\n }\n};\n```\n**Plz upvote if it helps**\n![image](https://assets.leetcode.com/users/images/54f32a72-4219-48c9-acb1-7e8c38b4e5d8_1653671028.9541156.png)\n\n
1
0
[]
0
minimum-moves-to-convert-string
Simple C++ greedy approach
simple-c-greedy-approach-by-priyesh_raj_-ru9n
\n int minimumMoves(string s) {\n int count = 0;\n int i = 0 ;\n while(i<s.size()){\n if(s[i]==\'X\'){\n count
priyesh_raj_singh
NORMAL
2022-03-22T18:10:22.659042+00:00
2022-03-22T18:10:22.659082+00:00
51
false
```\n int minimumMoves(string s) {\n int count = 0;\n int i = 0 ;\n while(i<s.size()){\n if(s[i]==\'X\'){\n count++;\n i+=3;\n }\n else{\n i++;\n }\n }\n return count; \n }\n```
1
0
['Greedy', 'C']
0
minimum-moves-to-convert-string
Greedy Algo.
greedy-algo-by-jay_kevadiya-6u3h
class Solution {\npublic:\n int minimumMoves(string s) {\n int ans = 0;\n for (int i = 0; i < s.size(); i += s[i] == \'X\' ? 3 : 1)\n ans += s[i
Jay_kevadiya
NORMAL
2022-03-18T04:29:45.968078+00:00
2022-03-18T04:29:45.968108+00:00
36
false
class Solution {\npublic:\n int minimumMoves(string s) {\n int ans = 0;\n for (int i = 0; i < s.size(); i += s[i] == \'X\' ? 3 : 1)\n ans += s[i] == \'X\';\n return ans;\n}\n};
1
0
['Greedy', 'C']
1
minimum-moves-to-convert-string
faster than 100% solutions :)
faster-than-100-solutions-by-peeronapppe-vdyp
class Solution {\npublic:\n int minimumMoves(string s) {\n int cnt=0;\n int i=0;\n for(i=0;i<s.length();i++){\n if(s[i]==\'O\
PeeroNappper
NORMAL
2022-03-07T14:42:13.457722+00:00
2022-03-07T14:42:13.457754+00:00
63
false
class Solution {\npublic:\n int minimumMoves(string s) {\n int cnt=0;\n int i=0;\n for(i=0;i<s.length();i++){\n if(s[i]==\'O\') continue;\n else break;\n }\n for(int j=i;j<s.length();j++){\n if(s[j]==\'O\') continue;\n int a=0;\n if(s[j]==\'X\'){\n s[j]=\'O\';\n a++;\n }\n if(j+1<=s.length())\n if(s[j+1]==\'X\'){\n s[j+1]=\'O\';\n a++;\n }\n if(j+2<=s.length())\n if(s[j+2]==\'X\'){\n s[j+2]=\'O\';\n a++;\n }\n if(a) cnt++;\n j+=2;\n }\n return cnt; \n }\n};
1
0
[]
0
minimum-moves-to-convert-string
Java solution 0ms 100%
java-solution-0ms-100-by-guptashresthy-628i
\nclass Solution {\n public int minimumMoves(String s) {\n int res=0;\n for(int i=0;i<s.length();)\n {\n if(s.charAt(i)==\'X\
guptashresthy
NORMAL
2022-03-04T01:02:18.208196+00:00
2022-03-04T01:02:18.208237+00:00
103
false
```\nclass Solution {\n public int minimumMoves(String s) {\n int res=0;\n for(int i=0;i<s.length();)\n {\n if(s.charAt(i)==\'X\')\n {\n i+=3;\n res++;\n }\n else\n i++;\n }\n return res;\n }\n}\n```
1
0
['Java']
1
minimum-moves-to-convert-string
C++ | Greedy Approach
c-greedy-approach-by-deleted_user-y377
Hint 2 : Try delaying a move as long as possible.\nExplanation : We can ignore \'O\' as long as possible.\n\t\t\t\t\t\t\t When we encounter \'X\' we have to co
deleted_user
NORMAL
2022-03-01T07:29:45.260102+00:00
2022-03-01T07:29:45.260156+00:00
64
false
**Hint 2 : Try delaying a move as long as possible.**\n**Explanation** : We can ignore \'O\' as long as possible.\n\t\t\t\t\t\t\t When we encounter \'X\' we have to count that as a move. \n\t\t\t\t\t\t\t \n```\nint minimumMoves(string s) {\n int move = 0;\n int i=0;\n while(i<s.size()){\n if(s[i]==\'X\'){\n i+=3;\n move++;\n } \n else i+=1;\n }\n return move;\n }\n
1
0
[]
0
minimum-moves-to-convert-string
js greedy
js-greedy-by-jasondecode-ttd6
```\nvar minimumMoves = function(s) {\n let i = 0;\n res = 0;\n while (i < s.length) {\n if (s[i] === \'X\') {\n s[i] = \'O\';\n
jasondecode
NORMAL
2022-02-13T07:56:41.752441+00:00
2022-02-13T07:56:57.283604+00:00
38
false
```\nvar minimumMoves = function(s) {\n let i = 0;\n res = 0;\n while (i < s.length) {\n if (s[i] === \'X\') {\n s[i] = \'O\';\n s[i + 1] = \'O\';\n s[i + 2] = \'O\';\n i += 3;\n res += 1;\n } else {\n i++;\n }\n }\n return res;\n};
1
0
['Greedy']
0
minimum-moves-to-convert-string
Simplest Python 3 code
simplest-python-3-code-by-rajatkumarrrr-268a
\nclass Solution:\n def minimumMoves(self, s: str) -> int:\n sl=list(s)\n out=0\n for i in range(0,len(sl)-2):\n if sl[i]=="X
rajatkumarrrr
NORMAL
2022-01-26T18:17:36.578483+00:00
2022-01-26T18:17:36.578527+00:00
228
false
```\nclass Solution:\n def minimumMoves(self, s: str) -> int:\n sl=list(s)\n out=0\n for i in range(0,len(sl)-2):\n if sl[i]=="X":\n sl[i]="O"\n sl[i+1]="O"\n sl[i+2]="O"\n out+=1\n elif sl[i]=="O":\n continue\n if sl[-1]=="X" or sl[-2]=="X":\n out+=1\n return out\n```
1
0
['Python3']
0
minimum-moves-to-convert-string
Java : Simple
java-simple-by-arunkumar_hg-lzcc
\nclass Solution {\n public int minimumMoves(String s) \n {\n int moves = 0;\n int i=0;\n \n while(i<s.length())\n {\n
arunkumar_hg
NORMAL
2022-01-25T10:59:00.860186+00:00
2022-01-25T10:59:00.860228+00:00
75
false
```\nclass Solution {\n public int minimumMoves(String s) \n {\n int moves = 0;\n int i=0;\n \n while(i<s.length())\n {\n if(s.charAt(i)==\'X\')\n {\n i+=3;\n moves++;\n }else\n {\n i++;\n }\n \n } \n \n return moves;\n \n }\n}\n```
1
0
['Java']
0
minimum-moves-to-convert-string
Fastest Java Solution
fastest-java-solution-by-saurabh_173-hbvg
```\nclass Solution {\n public int minimumMoves(String s) \n {\n if(!s.contains("X"))\n return 0;\n else\n {\n
saurabh_173
NORMAL
2022-01-23T15:17:29.214628+00:00
2022-01-23T15:17:29.214655+00:00
75
false
```\nclass Solution {\n public int minimumMoves(String s) \n {\n if(!s.contains("X"))\n return 0;\n else\n {\n int count=0;\n int n=s.length();\n for(int i=0;i<n;i++)\n {\n if(s.charAt(i)==\'X\')\n {\n count++;\n i+=2;\n if(i>=n)\n break;\n }\n }\n return count;\n }\n }\n}
1
0
['Java']
0
minimum-moves-to-convert-string
cpp solution 100%faster and 85% space efficient
cpp-solution-100faster-and-85-space-effi-dgdg
\n int minimumMoves(string s) \n {\n int ans=0;\n for(int i=0;i<s.length();)\n {\n if(s[i]==\'X\')\n {\n
ashutosh2015
NORMAL
2022-01-20T16:35:49.789801+00:00
2022-01-20T16:35:49.789845+00:00
58
false
```\n int minimumMoves(string s) \n {\n int ans=0;\n for(int i=0;i<s.length();)\n {\n if(s[i]==\'X\')\n {\n int t=3;\n while(i<s.length()&&t--)\n {\n s[i]=\'0\';\n i++;\n }\n ans++;\n }\n else\n i++;\n }\n return ans;\n }\n```
1
0
['C']
0
minimum-moves-to-convert-string
C# LINQ one-liner, O(n)
c-linq-one-liner-on-by-rad0mir-hh29
\npublic class Solution {\n public int MinimumMoves(string s) \n => s.Aggregate((res: 0, dist: 3), \n (pos, cur) => (pos.res + (
Rad0miR
NORMAL
2022-01-17T11:16:43.993789+00:00
2022-01-17T11:16:43.993830+00:00
224
false
```\npublic class Solution {\n public int MinimumMoves(string s) \n => s.Aggregate((res: 0, dist: 3), \n (pos, cur) => (pos.res + (cur == \'X\' && pos.dist > 2 ? 1 : 0), \n (cur == \'X\' && pos.dist > 2 ? 1 : pos.dist + 1)),\n pos => pos.res);\n}\n```
1
0
[]
2
minimum-moves-to-convert-string
intuitive solution
intuitive-solution-by-feexon-ornk
rust\nimpl Solution {\n pub fn minimum_moves(s: String) -> i32 {\n let (bytes, mut i, mut moves) = (s.as_bytes(), 0, 0);\n while i < bytes.len(
feexon
NORMAL
2021-12-19T05:30:55.436546+00:00
2021-12-19T05:32:34.343465+00:00
40
false
```rust\nimpl Solution {\n pub fn minimum_moves(s: String) -> i32 {\n let (bytes, mut i, mut moves) = (s.as_bytes(), 0, 0);\n while i < bytes.len() {\n i += match bytes[i] {\n b\'X\' => {\n moves += 1;\n 3\n }\n _ => 1,\n }\n }\n return moves;\n }\n}\n```
1
0
['Rust']
0
minimum-moves-to-convert-string
Optimal Solution
optimal-solution-by-code_soham-e2e2
Greedy Approach\nGiven problem states minimum steps to convert the XO string to only Os.\nAlso, under the constraint of the move described,\nif we choose an ind
code_soham
NORMAL
2021-12-08T08:00:14.340516+00:00
2021-12-08T08:00:14.340551+00:00
100
false
# Greedy Approach\nGiven problem states minimum steps to convert the XO string to only Os.\nAlso, under the constraint of the **move** described,\nif we choose an index for operating, we can absolutely assure that the character in the next 2 consecutive indices will also get resolved to O within that **move**. (making 3 consecutive indices)\nSo, our approach is such that we always search for the closest X position and operate. (OKAY!)\nNow,\nwe don\'t simply iterate from next index, but move our pointer ahead 3 indices of current position, as the 3 consecutive are already considered to be converted to O (By definition of the **move**).\n\nBesides we make sure to avoid index overflow by checking index < length in every iteration.\n\n```\nclass Solution {\npublic:\n int minimumMoves(string s) {\n int n=s.size();\n int res=0,i=0;\n while(i<n){\n while(s[i]==\'O\')i++;//greedily moving to next X position\n if(i>=n)break; //breaks if pointer exceeds array length \n res++;\n i+=3;\n }\n return res;\n }\n};\n```\n
1
0
['String']
0
minimum-moves-to-convert-string
[Python] Greedy Sliding Window
python-greedy-sliding-window-by-dev-josh-1uet
Think about it:\n\n "XOX" is a good deal, because we can convert two X\'s\n "XXX" is also an obvious good deal\n "XXO" is a good deal too\n\nA few bad deals:\n
dev-josh
NORMAL
2021-10-29T19:22:13.665693+00:00
2021-10-29T19:22:13.665715+00:00
138
false
Think about it:\n\n* "XOX" is a good deal, because we can convert two X\'s\n* "XXX" is also an obvious good deal\n* "XXO" is a good deal too\n\nA few bad deals:\n* "OXX"\n\t* This could easily become "XXX" if we slide the window along by one\n\t* Thus, we should only convert "XXO" because we know the next will either be\n\t\t* "XOO" or "XOX"\n* "OOX"\n\t* Obviously, this could slide and become "XXX" or "XXO" or "XOX"\n\nThus, you just need to analyze each case for the sliding window, and decide if we want to perform the operation or not.\n\n```python\ndef minimumMoves(s):\n\n\ts += "OOO"\n\n\ti, result = 0, 0\n\n\twhile i < len(s)-3:\n\t\twindow = s[i:i+3]\n\t\tif window in ["XOX", "XXX", "XOO", "XXO"]:\n\t\t\ti += 3\n\t\t\tresult += 1\n\t\telse:\n\t\t\ti += 1\n\n\treturn result\n```
1
1
['Python', 'Python3']
0
minimum-moves-to-convert-string
cpp
cpp-by-testing555111-fj60
\nclass Solution {\npublic:\n int minimumMoves(string s) {\n int ret = 0;\n for (int i=0;i<s.length();i++) {\n if (s[i]==\'X\') {\n
testing555111
NORMAL
2021-10-26T08:49:13.976933+00:00
2021-10-26T08:49:13.976967+00:00
28
false
```\nclass Solution {\npublic:\n int minimumMoves(string s) {\n int ret = 0;\n for (int i=0;i<s.length();i++) {\n if (s[i]==\'X\') {\n ret++;\n i+=2;\n }\n }\n return ret;\n }\n};\n```
1
0
[]
0
minimum-moves-to-convert-string
Rust | Greedy approach
rust-greedy-approach-by-deleted_user-zr28
Analysis:\n\nIf we need to flip an \'X\' at an index i, then any \'X\' at indices i+1 and i+2 will also be converted into a \'O\', should those indices exist.\n
deleted_user
NORMAL
2021-10-25T02:19:19.529680+00:00
2021-10-25T02:19:31.037621+00:00
65
false
**Analysis:**\n\nIf we need to flip an \'X\' at an index `i`, then any \'X\' at indices `i+1` and `i+2` will also be converted into a \'O\', should those indices exist.\n\nSo greedily, we can flip any \'X\' into a \'O\', and advance the index by 3. Otherwise flip the index by 1.\n\n**Solution:**\n\n```\nimpl Solution {\n pub fn minimum_moves(s: String) -> i32 {\n let mut i: usize = 0;\n\n let mut res: i32 = 0;\n\n while i < s.len() {\n let c: char = s.chars().nth(i).unwrap();\n\n /* If we encounter a \'X\', we need to\n turn it into a \'O\', this move will also\n change any \'X\' at index i+1 and i+2 into\n \'O\' if those indices are valid.\n After the conversion at index i, so we just\n advance by i by 3, otherwise advance by 1.\n */\n if c == \'X\' {\n res += 1;\n i += 3;\n } else {\n i += 1;\n }\n }\n\n res\n }\n}\n```
1
0
['Greedy', 'Rust']
0