id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n#define MOD 1000000007\n int numWays(int steps, int arrLen) {\n vector<int> dp(arrLen, 0);\n dp[0] = 1;\n vector<int> next_dp(arrLen, 0);\n\n for (int k = 1; k <= steps; k++) {\n for (int i = 0; i < arrLen && i < steps - k + 1; i++) {\n next_dp[i] = 0;\n if (i - 1 >= 0) {\n if (dp[i - 1] == 0) continue;\n next_dp[i] += dp[i - 1];\n next_dp[i] %= MOD;\n }\n next_dp[i] += dp[i];\n next_dp[i] %= MOD;\n if (i + 1 < arrLen) {\n next_dp[i] += dp[i + 1];\n next_dp[i] %= MOD;\n }\n }\n\n swap(dp, next_dp);\n }\n\n return dp[0];\n }\n};", "memory": "23379" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int MOD = 1e9+7;\n\n int calculate(int currIndex, int arrLen, int stepsLeft, vector<vector<long long>> &dp){\n if(stepsLeft==0){\n if(currIndex==0){\n return 1;\n }\n return 0;\n }\n if(dp[currIndex][stepsLeft]!=-1){\n return dp[currIndex][stepsLeft];\n }\n int left=0,right=0,stay=0;\n if(currIndex > 0){\n left = calculate(currIndex-1, arrLen, stepsLeft-1, dp);\n }\n if(currIndex<arrLen-1){\n right = calculate(currIndex+1, arrLen, stepsLeft-1, dp);\n }\n stay = calculate(currIndex, arrLen, stepsLeft-1, dp);\n\n long long ans = ((left+right)%MOD+stay)%MOD;\n return dp[currIndex][stepsLeft] = ans;\n }\n\n \n\n int numWays(int steps, int arrLen) {\n vector<vector<long long>> dp(511, vector<long long>(steps+1,0));\n //return calculate(0, arrLen, steps, dp);\n dp[0][0] = 1;\n for(int j=1;j<=steps;j++){\n for(int i = 509;i>=0;i--){\n int left=0,right=0,stay=0;\n if(i > 0){\n left = dp[i-1][j-1];\n }\n if(i<arrLen-1){\n right = dp[i+1][j-1];\n }\n stay = dp[i][j-1];\n\n long long ans = ((left+right)%MOD+stay)%MOD;\n dp[i][j] = ans;\n }\n }\n return dp[0][steps];\n }\n};", "memory": "24555" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(vector<vector<int>>& dp, int idx, int step, int steps, int maxs){\n if(idx==0 && step==steps)\n return 1;\n if(step>=steps || idx<0 || idx>=maxs || idx>=1000)\n return 0;\n if(dp[idx][step]!=-1)\n return dp[idx][step];\n int ans= ((solve(dp, idx+1, step+1, steps, maxs)+solve(dp, idx-1, step+1, steps, maxs))%1000000007+\n solve(dp, idx, step+1, steps, maxs))%1000000007;\n dp[idx][step]=ans;\n return ans;\n \n }\n int numWays(int steps, int arrLen) {\n vector<vector<int>> dp(1000, vector<int>(steps+1, -1));\n return solve(dp, 0, 0, steps, arrLen);\n }\n};", "memory": "25731" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(vector<vector<int>>& dp, int idx, int step, int steps, int maxs){\n if(idx==0 && step==steps)\n return 1;\n if(step>=steps || idx<0 || idx>=maxs || idx>=1000)\n return 0;\n if(dp[idx][step]!=-1)\n return dp[idx][step];\n int ans= ((solve(dp, idx+1, step+1, steps, maxs)+solve(dp, idx-1, step+1, steps, maxs))%1000000007+\n solve(dp, idx, step+1, steps, maxs))%1000000007;\n dp[idx][step]=ans;\n return ans;\n \n }\n int numWays(int steps, int arrLen) {\n vector<vector<int>> dp(1000, vector<int>(steps+1, -1));\n return solve(dp, 0, 0, steps, arrLen);\n }\n};", "memory": "25731" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int mod=1e9+7;\n int dp[501][10001];\n int f(int steps,int i,int arrLen)\n {\n if(i>=arrLen || i<0)\n {\n return 0;\n }\n if(steps==0)\n {\n return i==0;\n }\n if(i<1000 && dp[steps][i]!=-1)\n return dp[steps][i];\n int ans=0;\n ans=(ans+f(steps-1,i,arrLen))%mod;\n ans=(ans+f(steps-1,i+1,arrLen))%mod;\n ans=(ans+f(steps-1,i-1,arrLen))%mod;\n \n if(i<1000)\n return dp[steps][i]=ans;\n return ans;\n \n }\n int numWays(int steps, int arrLen) {\n memset(dp,-1,sizeof dp);\n return f(steps,0,arrLen);\n \n }\n};", "memory": "26908" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int dp[502][10005];\n int solve(int ind,int steps,int len){\n if(ind==0&&steps==0) return 1;\n if(steps==0) return 0;\n int mod=1e9+7;\n if(dp[ind][steps]!=-1) return dp[ind][steps];\n int ans=0;\n if(ind>0){\n ans=(ans+solve(ind-1,steps-1,len))%mod;\n }\n if(ind<len-1){\n ans=(ans+solve(ind+1,steps-1,len))%mod;\n }\n ans=(ans+solve(ind,steps-1,len))%mod;\n return dp[ind][steps]=ans%mod;\n }\n int numWays(int steps, int arrLen) {\n memset(dp,-1,sizeof(dp));\n return solve(0,steps,arrLen);\n }\n};", "memory": "28084" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int dp[10010][505];\n int mod=1e9+7;\n int rec(int index,int pos,int &arrLen,int &steps){\n if(index < 0 || index >= arrLen){\n return 0;\n }\n if(pos == steps){\n if(index == 0){\n return 1;\n }\n return 0;\n }\n if(dp[index][pos]!= -1){\n return dp[index][pos];\n }\n int ans=0;\n ans=rec(index+1,pos+1,arrLen,steps)%mod;\n ans=(ans + rec(index,pos+1,arrLen,steps))%mod;\n ans=(ans + rec(index-1,pos+1,arrLen,steps))%mod;\n\n return dp[index][pos]=ans%mod;\n }\n int numWays(int steps, int arrLen) {\n memset(dp,-1,sizeof(dp));\n return rec(0,0,arrLen,steps);\n }\n};", "memory": "29260" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<long long>> dp;\n int mod=1e9+7;\n long long solve(int i,int steps,int n){\n if(i<0 || i>=n) return 0;\n if(steps==0 && i!=0) return 0;\n if(steps==0 && i==0) return 1;\n if(dp[i][steps]!=-1) return dp[i][steps];\n long long left=solve(i-1,steps-1,n)%mod;\n long long stay=solve(i,steps-1,n)%mod;\n long long right=solve(i+1,steps-1,n)%mod;\n return dp[i][steps]=(left+stay+right)%mod;\n }\n\n int numWays(int steps, int arrLen) {\n dp.resize(600+1,vector<long long>(steps+1,-1));\n return solve(0,steps,arrLen);\n }\n};", "memory": "29260" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\n const int MOD = 1e9 + 7;\npublic:\n int numWays(int steps, int n) {\n if (n == 1) return 1;\n vector<vector<int>> dp(2, vector<int>(n, 0));\n dp[0][0] = dp[0][1] = 1;\n\n for (int s=1; s<steps; ++s) {\n for (int i=0; i<min(n, steps); ++i) {\n dp[1][i] = 0;\n if (i > 0) dp[1][i] = (dp[1][i] + dp[0][i-1]) % MOD;;\n dp[1][i] = (dp[1][i] + dp[0][i]) % MOD;\n if (i < n-1) dp[1][i] = (dp[1][i] +dp[0][i+1]) % MOD;\n }\n\n swap(dp[0], dp[1]);\n }\n\n return dp[0][0];\n }\n};", "memory": "30436" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\n const int MOD = 1e9 + 7;\npublic:\n int numWays(int steps, int n) {\n if (n == 1) return 1;\n vector<vector<int>> dp(2, vector<int>(n, 0));\n dp[0][0] = dp[0][1] = 1;\n\n for (int s=1; s<steps; ++s) {\n for (int i=0; i<min(n, s+2); ++i) {\n dp[1][i] = 0;\n if (i > 0) dp[1][i] = (dp[1][i] + dp[0][i-1]) % MOD;;\n dp[1][i] = (dp[1][i] + dp[0][i]) % MOD;\n if (i < n-1) dp[1][i] = (dp[1][i] +dp[0][i+1]) % MOD;\n }\n\n swap(dp[0], dp[1]);\n }\n\n return dp[0][0];\n }\n};", "memory": "30436" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int generate(const int &arrLen, const int &endPos, const int &k, int i, int pos)\n {\n if (i == k)\n {\n return endPos == pos;\n }\n if ((pos - (k - i + 1)) > 0) return 0;\n \n int x1 = 0;\n if ((pos + 1) != arrLen)\n {\n auto it = hash.find((100000*(pos + 1) + i + 1));\n if (it == hash.cend())\n {\n hash[(100000*(pos + 1) + i + 1)] = generate(arrLen, endPos, k, i + 1, pos + 1);\n }\n x1 = hash[(100000*(pos + 1) + i + 1)] % 1000000007;\n }\n int x2 = 0;\n if ((pos - 1) >= 0)\n {\n auto it = hash.find(100000*(pos - 1) + i + 1);\n if (it == hash.cend())\n {\n hash[(100000*(pos - 1) + i + 1)] = generate(arrLen, endPos, k, i + 1, pos - 1);\n }\n x2 = hash[(100000*(pos - 1) + i + 1)] % 1000000007;\n }\n auto it = hash.find(100000*(pos) + i + 1);\n if (it == hash.cend())\n {\n hash[(100000*(pos) + i + 1)] = generate(arrLen, endPos, k, i + 1, pos);\n }\n int x3 = hash[(100000*(pos) + i + 1)] % 1000000007; \n return (static_cast<long long>(x1) + x2 + x3) % 1000000007;\n }\n\n int numWays(int steps, int arrLen) {\n return generate(arrLen, 0, steps, 0, 0);\n }\nprivate:\n unordered_map<int, int> hash;\n};", "memory": "31613" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int generate(const int &arrLen, const int &endPos, const int &k, int i, int pos)\n {\n if (i == k)\n {\n return endPos == pos;\n }\n if ((pos - (k - i + 1)) > 0) return 0;\n \n int x1 = 0;\n if ((pos + 1) != arrLen)\n {\n auto it = hash.find((100000*(pos + 1) + i + 1));\n if (it == hash.cend())\n {\n hash[(100000*(pos + 1) + i + 1)] = generate(arrLen, endPos, k, i + 1, pos + 1);\n }\n x1 = hash[(100000*(pos + 1) + i + 1)] % 1000000007;\n }\n int x2 = 0;\n if ((pos - 1) >= 0)\n {\n auto it = hash.find(100000*(pos - 1) + i + 1);\n if (it == hash.cend())\n {\n hash[(100000*(pos - 1) + i + 1)] = generate(arrLen, endPos, k, i + 1, pos - 1);\n }\n x2 = hash[(100000*(pos - 1) + i + 1)] % 1000000007;\n }\n auto it = hash.find(100000*(pos) + i + 1);\n if (it == hash.cend())\n {\n hash[(100000*(pos) + i + 1)] = generate(arrLen, endPos, k, i + 1, pos);\n }\n int x3 = hash[(100000*(pos) + i + 1)] % 1000000007; \n return (static_cast<long long>(x1) + x2 + x3) % 1000000007;\n }\n\n int numWays(int steps, int arrLen) {\n return generate(arrLen, 0, steps, 0, 0);\n }\nprivate:\n unordered_map<int, int> hash;\n};", "memory": "31613" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n struct pair_hash\n {\n template <typename T, typename U>\n std::size_t operator () (const std::pair<T, U> &x) const\n {\n return std::hash<T>()(x.first) ^ std::hash<U>()(x.second);\n }\n };\n\n int numWays(int steps, int arrLen) \n {\n int MOD = 1000000007;\n std::unordered_map<std::pair<int, int>, int, pair_hash> ways;\n // <int, int> -- <steps, index>\n\n ways[{1,0}] = 1;\n ways[{1,1}] = 1;\n\n for (int step = 2; step <= steps; ++step)\n {\n //std::cout << \"STEP \" << step << std::endl;\n\n int max_ind = std::min(std::min(step, arrLen - 1), steps - step + 1);\n for (int ind = 0; ind <= max_ind; ++ind)\n {\n int w = ways[{step - 1, ind}];\n\n if (ind > 0)\n w = (w + ways[{step - 1, ind - 1}]) % MOD;\n\n if (ind < max_ind)\n w = (w + ways[{step - 1, ind + 1}]) % MOD;\n\n ways[{step, ind}] = w;\n\n //std::cout << \"{\" << step << \",\" << ind << \"}: \" << w << std::endl;\n }\n\n //std::cout << std::endl;\n }\n\n return ways[{steps, 0}];\n }\n};\n\n\n\n\n// class Solution {\n// public:\n// int MOD = 1000000007;\n\n// int numLR(int steps, int arrLen)\n// {\n// int l = steps / 2;\n// return 0;\n// }\n\n// int fact(int n)\n// {\n// int r = 1;\n// for (int i = 1; i <= n; ++i)\n// {\n// r = r * i;\n// }\n// return r;\n// }\n\n// int numZ(int zeroes, int len)\n// {\n// // 2 zeroes, <><> len = 4\n\n// len = len + zeroes;\n\n// int r = 1;\n// for (int i = 0; i < zeroes; ++i)\n// {\n// r = r * (len - i) % MOD;\n// }\n\n// r = r / fact(zeroes);\n\n// return r;\n// }\n\n\n// int f(int steps, int i, int open, string c)\n// {\n// //cout << \"s(\" << steps << \",\" << i << \",\" << open << \")\" << endl;\n\n// int r = 0;\n// if (i == steps - 1)\n// {\n// if (open == 1)\n// {\n// c += \")\";\n// cout << c << endl;\n// return 1;\n// }\n// else\n// {\n// return 0;\n// }\n// }\n\n// if (open > 0)\n// r += f(steps, i + 1, open - 1, c + \")\");\n \n// r += f(steps, i + 1, open + 1, c + \"(\");\n\n// return r;\n// }\n\n// int numWays(int steps, int arrLen) \n// {\n// f(10, 0, 0, \"\");\n\n// // cout << \"f(2): \" << f(2, 0, 0) << endl;\n// // cout << \"f(4): \" << f(4, 0, 0) << endl;\n// // cout << \"f(6): \" << f(6, 0, 0) << endl;\n// //cout << \"f(8): \" << f(8, 0, 0) << endl;\n// //cout << \"f(10): \" << f(10, 0, 0, \"\") << endl;\n// // cout << \"f(12): \" << f(12, 0, 0) << endl;\n\n// //f(4, 0, 0);\n// //cout << \"f(8): \" << f(8, 0, 0) << endl;\n\n// return 0;\n// }\n// };", "memory": "36318" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int numWays(int steps, int arrLen) {\n unordered_map<int, int> v;\n int mod = 1e9+7;\n v[0] = 1;\n if(arrLen>=2){\n v[1] = 1;\n }\n for(int i=2;i<=steps; i++){\n unordered_map<int, int> tv = v;\n for(auto it = v.begin(); it!=v.end(); it++){\n if(it->first+1 < arrLen){\n tv[it->first+1] += v[it->first]%mod;\n tv[it->first+1] %= mod;\n }\n if(it->first-1 >=0){\n tv[it->first-1] += v[it->first]%mod;\n tv[it->first-1] %= mod;\n }\n }\n v = tv;\n }\n return v[0]%mod;\n \n }\n};", "memory": "37494" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\nint n,st;\nlong long dp[501][10001];\nlong long mod=1e9+7;\nlong long f(long long i,long long j){\n if(i==st) return (j==0);\n if(j<0 || j==n) return 0;\n if(dp[i][j]!=-1) return dp[i][j];\n long long ans=(f(i+1,j-1)+f(i+1,j+1)+f(i+1,j))%mod;\n return dp[i][j]=ans;\n}\n int numWays(int steps, int arrLen) {\n n=arrLen; st=steps;\n memset(dp,-1,sizeof(dp));\n return f(0,0);\n }\n};", "memory": "38670" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int M = pow(10, 9) + 7;\n int ways(int steps, int pos, int len, vector<vector<int>> &dp){\n if(steps == 0 && pos == 0)\n return 1;\n if(steps == 0)\n return 0;\n\n if(dp[pos][steps] != -1)\n return dp[pos][steps];\n\n int res = 0;\n\n //stay at the same place\n res = ((res%M) + ways(steps -1, pos, len, dp) % M) %M;\n\n //move left if possible\n if(pos > 0)\n res = ((res % M) + ways(steps - 1, pos -1, len, dp) % M) % M;\n \n //move right if possible\n if(pos + 1 < len)\n res = ((res % M) + ways(steps - 1, pos + 1, len, dp) % M) % M;\n\n return dp[pos][steps] = res;\n }\n int numWays(int s, int a) {\n int pos = 0;\n int maxLen = pow(10, 6), maxSteps = 500;\n vector<vector<int>> dp(maxSteps + 1, vector<int> (maxSteps +1, -1));\n return ways(s, pos, a, dp);\n }\n};", "memory": "39846" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\n int N;\n int M=1e9+7;\n vector<vector<int>> dp;\n unordered_map<string, int> m;\n int check(int steps, int ind){\n if(steps==0){\n if(ind==0) return 1;\n return 0;\n }\n if(ind>=500) return 0;\n //string str=to_string(steps)+'_'+to_string(ind);\n if(dp[steps][ind]!=-1) return dp[steps][ind];\n //if(m.count(str)) return m[str];\n int n1=0, n2=0, n3=0;\n if(ind>0) n1=check(steps-1, ind-1);\n if(ind<N-1) n2=check(steps-1, ind+1);\n n3=check(steps-1, ind);\n return dp[steps][ind]=((n1+n2)%M+n3)%M;\n }\npublic:\n int numWays(int steps, int arrLen) {\n N=arrLen;\n dp.resize(501, vector<int>(501, -1));\n return check(steps, 0);\n }\n};", "memory": "41023" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\nint mod = 1000000007;\n\nint fn(int idx, int cnt, int len, int steps, int **dp) {\n\n if(idx<0 or idx>=len) {\n return 0;\n }\n if(cnt>=steps) {\n if(idx==0) {\n return 1;\n }\n return 0;\n }\n\n if(dp[idx][cnt]!=-1) return dp[idx][cnt];\n\n int val1 = fn(idx, cnt+1, len, steps, dp)%mod; // for 0\n\n\n int val2 = fn(idx+1, cnt+1, len, steps, dp)% mod; // for right\n\n int val3 = fn(idx-1, cnt+1, len, steps, dp)%mod; // for left\n\n\n int ans = val1%mod;\n ans = ((ans%mod) + (val2%mod))%mod;\n ans = ((ans%mod) + (val3%mod))%mod;\n\n cout<<idx<< \" \"<<cnt<<\" \"<<ans<<endl;\n\n return dp[idx][cnt]=ans;\n}\n\n int numWays(int steps, int arrLen) {\n\n vector<int>vv;\n int *dp[510];\n for(int i=0;i<510;++i) dp[i] = new int[510];\n\n for(int i=0;i<510;++i)\n for(int j=0;j<510;++j)\n dp[i][j] = -1;\n\n return fn(0, 0, arrLen, steps, dp);\n \n }\n};", "memory": "42199" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int mod=1e9+7;\n int check(vector<vector<int>>&dp,int index,int n,int k){\n if(index<0 || index==n){\n return 0;\n }\n if(k==0){\n return (index==0);\n }\n if(dp[index][k]!=-1){\n return dp[index][k];\n }\n int ways=0;\n ways=(ways+check(dp,index,n,k-1)%mod)%mod;\n ways=(ways+check(dp,index-1,n,k-1)%mod)%mod;\n ways=(ways+check(dp,index+1,n,k-1)%mod)%mod;\n return dp[index][k]=ways;\n }\n int numWays(int k, int n) {\n vector<vector<int>>dp(510,vector<int>(510,-1));\n return check(dp,0,n,k);\n }\n};", "memory": "43375" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\n \npublic:\n int mod=1e9+7;\n long long solve(int ind, vector<vector<int>> &dp, int n, int steps){\n if(steps==0) return (ind==0);\n if(dp[ind][steps]!=-1) return dp[ind][steps];\n long long res=0;\n\n if(ind-1>=0){\n res+=solve(ind-1, dp, n, steps-1);\n }\n if(ind+1<n){\n res+=solve(ind+1, dp, n, steps-1);\n }\n res+=solve(ind, dp,n,steps-1);\n\n return dp[ind][steps]=res%mod;\n }\n \n int numWays(int steps, int arrLen) {\n\n vector<vector<int>> dp(511, vector<int>(511, -1));\n\n return solve(0,dp,arrLen, steps);\n \n\n \n }\n};", "memory": "44551" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\nprivate:\n int getNumWays(int currInd, int stepsLeft, int &arrLen, unordered_map<int, unordered_map<int, int>> &countWays) {\n if (stepsLeft == 0) return (currInd == 0);\n\n if (countWays[currInd].count(stepsLeft) == 1) return countWays[currInd][stepsLeft];\n\n // go left\n int left = 0;\n if (currInd > 0) left = getNumWays(currInd - 1, stepsLeft - 1, arrLen, countWays);\n\n // stay at same place\n int stay = getNumWays(currInd, stepsLeft - 1, arrLen, countWays);\n\n // go right\n int right = 0;\n if (currInd < arrLen - 1) right = getNumWays(currInd + 1, stepsLeft - 1, arrLen, countWays);\n\n return countWays[currInd][stepsLeft] = ((long long)left + stay + right) % int(1e9 + 7);\n }\npublic:\n int numWays(int steps, int arrLen) {\n int currInd = 0, stepsLeft = steps;\n unordered_map<int, unordered_map<int, int>> countWays;\n return getNumWays(currInd, stepsLeft, arrLen, countWays);\n }\n};", "memory": "45728" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int md = 1e9+7;\n unordered_map<int, unordered_map<int, int>> dp;\n int solve(int k, int n, int i) {\n if (k==0) return (int)(i==0);\n if (dp.count(i) && dp[i].count(k)) return dp[i][k];\n int res=0;\n res = (res + solve(k-1, n, i))%md;\n if (i) res = (res + solve(k-1, n, i-1))%md; \n if (i<n-1) res = (res + solve(k-1, n, i+1))%md;\n return dp[i][k] = res;\n }\n\n int numWays(int k, int n) {\n return solve(k, n, 0);\n }\n};", "memory": "46904" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n unordered_map<int, unordered_map<int, int>> dp;\n int MOD = pow(10, 9) + 7;\n int solve(int curr, int len, int rem){\n if(rem == 0){ \n if(curr == 0) return 1;\n else return 0;\n }\n if(dp.find(curr) != dp.end() && dp[curr].find(rem) != dp[curr].end()) return dp[curr][rem];\n int ans = 0;\n if(curr > 0) ans = (ans + solve(curr-1, len, rem-1))%MOD;\n if(curr < len-1) ans = (ans + solve(curr+1, len, rem-1))%MOD;\n ans = (ans + solve(curr, len, rem-1))%MOD;\n\n return dp[curr][rem] = ans;\n }\n int numWays(int steps, int arrLen) {\n return solve(0, arrLen, steps);\n }\n};", "memory": "48080" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int generate(const int &arrLen, const int &endPos, const int &k, int i, int pos)\n {\n if (i == k)\n {\n return endPos == pos;\n }\n int x1 = 0;\n if ((pos + 1) != arrLen)\n {\n auto it = hash.find((100000*(pos + 1) + i + 1));\n if (it == hash.cend())\n {\n hash[(100000*(pos + 1) + i + 1)] = generate(arrLen, endPos, k, i + 1, pos + 1);\n }\n x1 = hash[(100000*(pos + 1) + i + 1)] % 1000000007;\n }\n int x2 = 0;\n if ((pos - 1) >= 0)\n {\n auto it = hash.find(100000*(pos - 1) + i + 1);\n if (it == hash.cend())\n {\n hash[(100000*(pos - 1) + i + 1)] = generate(arrLen, endPos, k, i + 1, pos - 1);\n }\n x2 = hash[(100000*(pos - 1) + i + 1)] % 1000000007;\n }\n auto it = hash.find(100000*(pos) + i + 1);\n if (it == hash.cend())\n {\n hash[(100000*(pos) + i + 1)] = generate(arrLen, endPos, k, i + 1, pos);\n }\n int x3 = hash[(100000*(pos) + i + 1)] % 1000000007; \n return (static_cast<long long>(x1) + x2 + x3) % 1000000007;\n }\n\n\n int numWays(int steps, int arrLen) {\n //hash.resize(arrLen + 1);\n //for (auto &e: hash)\n //{\n // e.resize(steps + 1, INT_MAX);\n //}\n return generate(arrLen, 0, steps, 0, 0);\n }\nprivate:\n unordered_map<int, int> hash;\n //vector<vector<int>> hash;\n};", "memory": "49256" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int generate(const int &arrLen, const int &endPos, const int &k, int i, int pos)\n {\n if (i == k)\n {\n //cout << \"In pos = \" << pos << endl;\n return endPos == pos;\n }\n int x1 = 0;\n if (pos < 0) return 0;\n //cout << \"arrLen = \" << arrLen << \", endPos = \" << endPos << \", k = \" << k << \", i = \" << i << \", pos = \" << pos << endl;\n //if (abs(pos + 1 - endPos) <= (k - i - 1))\n if ((pos + 1) != arrLen)\n {\n //cout << \"1.in\" << endl;\n //if (hash[(pos + 1)][i + 1] == INT_MAX)\n auto it = hash.find((100000*(pos + 1) + i + 1));\n if (it == hash.cend())\n {\n hash[(100000*(pos + 1) + i + 1)] = generate(arrLen, endPos, k, i + 1, pos + 1);\n }\n x1 = hash[(100000*(pos + 1) + i + 1)] % 1000000007;\n }\n int x2 = 0;\n //if (abs(pos - 1 - endPos) <= (k - i - 1))\n if ((pos - 1) >= 0)\n {\n //cout << \"2.in\" << endl;\n auto it = hash.find(100000*(pos - 1) + i + 1);\n if (it == hash.cend())\n {\n hash[(100000*(pos - 1) + i + 1)] = generate(arrLen, endPos, k, i + 1, pos - 1);\n }\n x2 = hash[(100000*(pos - 1) + i + 1)] % 1000000007;\n }\n auto it = hash.find(100000*(pos) + i + 1);\n if (it == hash.cend())\n {\n hash[(100000*(pos) + i + 1)] = generate(arrLen, endPos, k, i + 1, pos);\n }\n int x3 = hash[(100000*(pos) + i + 1)] % 1000000007;\n \n return (static_cast<long long>(x1) + x2 + x3) % 1000000007;\n }\n\n\n int numWays(int steps, int arrLen) {\n //hash.resize(arrLen + 1);\n //for (auto &e: hash)\n //{\n // e.resize(steps + 1, INT_MAX);\n //}\n return generate(arrLen, 0, steps, 0, 0);\n }\nprivate:\n unordered_map<int, int> hash;\n //vector<vector<int>> hash;\n};", "memory": "50433" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "const int MOD = 1e9 + 7;\n\nclass Solution {\npublic:\n void add(int &x, int val){\n x += val;\n if(x > MOD){\n x -= MOD;\n }\n }\n\n int numWays(int steps, int arrLen) {\n map<int, int> mp;\n mp[0] = 1;\n for(int i = 0; i < steps; ++i){\n map<int, int> nmp;\n for(auto [pos, cnt] : mp){\n for(int to_pos = pos - 1; to_pos <= pos + 1; ++to_pos){\n if(to_pos < 0 || to_pos >= arrLen) continue;\n add(nmp[to_pos], cnt);\n }\n }\n swap(mp, nmp);\n }\n return mp[0];\n }\n};", "memory": "51609" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n map<pair <int, int> , int > dp;\n long long int mod = 1e9 + 7;\n int dfs(int index, int steps, int n) {\n if(index < 0 || index >= n) return 0;\n if(dp.find({index,steps}) != dp.end()) return dp[{index, steps}];\n if(steps == 0){\n return (index == 0) ? 1 : 0;\n }\n int ways = 0;\n ways = ( ways + dfs(index-1, steps - 1 , n) ) % mod;\n ways = ( ways + dfs(index, steps - 1 , n) ) % mod;\n ways = ( ways + dfs(index+1, steps - 1 , n) ) % mod;\n dp[{index, steps}] = ways;\n return ways; \n }\n int numWays(int steps, int arrLen) {\n dp.clear();\n return dfs(0, steps, arrLen);\n }\n};", "memory": "52785" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long MOD = 1000000007;\n long dfs(int i, int s, int al, map<pair<int, int>, long> &m){\n if(s == 0 && i == 0) return 1;\n if(i < 0 || i >= al || s == 0) return 0;\n if(m.find({i, s}) != m.end()) return m[{i, s}];\n return m[{i, s}] = (dfs(i, s - 1, al, m) + dfs(i + 1, s - 1, al, m) + dfs(i - 1, s - 1, al, m))%MOD;\n }\n int numWays(int steps, int arrLen) {\n map<pair<int, int>, long> m;\n return dfs(0, steps, arrLen, m);\n }\n};", "memory": "53961" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "static const int mod = 1000000007;\nclass Solution {\npublic:\n map<long long,long long> mm;\n long long f(long long steps,const int len,int pos){\n if(pos <= 0 || pos > len){\n return 0;\n }\n if(steps == 0){\n if(pos == 1) return 1;\n else return 0;\n }\n long long mark = (steps << 32) | pos;\n if(mm.count(mark)){\n return mm[mark];\n }\n long long ret = 0;\n ret += f(steps-1,len,pos);\n ret %= mod;\n ret += f(steps-1,len,pos+1);\n ret %= mod;\n ret += f(steps-1,len,pos-1);\n ret %= mod;\n mm[mark] = ret % mod;\n return mm[mark];\n }\n int numWays(int steps, int arrLen) {\n return f(steps,arrLen,1);\n }\n};", "memory": "55138" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n map<pair<int,int>,int> dp;\n int modu=1e9+7;\n int solve(int i , int steps, int arrlen){\n\n if(i==0 && steps==0) return 1;\n if(i<0 || steps==0 || i>=arrlen) return 0;\n if(dp.find({i,steps})!=dp.end()) return dp[{i,steps}];\n int a=solve(i+1,steps-1,arrlen);\n int b=solve(i-1,steps-1,arrlen);\n int c=solve(i,steps-1,arrlen);\n return dp[{i,steps}]=((a+b)%modu+c)%modu;\n }\n int numWays(int steps, int arrLen) {\n return solve(0,steps,arrLen);\n }\n};", "memory": "56314" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int mod = 1e9+7;\n map<pair<int,int>,int> mp;\n\n int solve(int i,int steps,int n){\n if(i>=n || i<0) return 0;\n if(mp.find({i,steps})!=mp.end()) return mp[{i,steps}];\n if(steps==0) return i==0;\n int op1 = solve(i+1,steps-1,n) %mod;\n int op2 = solve(i,steps-1,n) %mod;\n int op3 = solve(i-1,steps-1,n) %mod;\n return mp[{i,steps}] = ((op1+op2)%mod+op3)%mod;\n }\n\n int numWays(int steps, int arrLen) {\n return solve(0,steps,arrLen);\n }\n};", "memory": "57490" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int mod = 1e9 + 7;\n map<pair<int, int>, int> mp;\n\n int solve(int i, int steps, int n) {\n if (i >= n || i < 0)\n return 0;\n\n if (mp.find({i, steps}) != mp.end())\n return mp[{i, steps}];\n\n if (steps == 0)\n return i == 0;\n\n int op1 = solve(i + 1, steps - 1, n) % mod;\n int op2 = solve(i, steps - 1, n) % mod;\n int op3 = solve(i - 1, steps - 1, n) % mod;\n\n return mp[{i, steps}] = ((op1 + op2) % mod + op3) % mod;\n }\n\n int numWays(int steps, int arrLen) {\n return solve(0, steps, arrLen);\n }\n};", "memory": "57490" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "int mod=1e9+7;\nclass Solution {\npublic:\n map<pair<int,int>,int>mp;\n int help(int level,int steps,int arrLen)\n {\n if(steps==0)\n {\n if(level==0)return 1;\n else return 0;\n }\n if(mp.find({level,steps})!=mp.end())return mp[{level,steps}];\n long long ans=0;\n if(level+1<arrLen)ans=(ans+help(level+1,steps-1,arrLen))%mod;\n if(level-1>=0)ans=(ans+help(level-1,steps-1,arrLen))%mod;\n if(level<arrLen && level>=0)ans=(ans+help(level,steps-1,arrLen))%mod;\n\n return mp[{level,steps}]=ans%mod;\n }\n int numWays(int steps, int arrLen) {\n return help(0,steps,arrLen);\n }\n};", "memory": "58666" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int mod = 1000000007;\n int numWaysUtil(int startPos,int steps,map<int,map<int,int>>&dp,int &arrLen){\n if(steps==0){\n if(startPos==0)return 1;\n return 0;\n }\n if(dp.find(startPos)!=dp.end() && dp[startPos].find(steps) != dp[startPos].end())return dp[startPos][steps];\n int ans = 0;\n if(startPos-1>=0){\n ans = (ans + numWaysUtil(startPos-1,steps-1,dp,arrLen)%mod)%mod;\n }\n if(startPos+1<arrLen){\n ans = (ans + numWaysUtil(startPos+1,steps-1,dp,arrLen)%mod)%mod;\n }\n ans = (ans + numWaysUtil(startPos,steps-1,dp,arrLen)%mod)%mod;\n return dp[startPos][steps]=ans%mod;\n }\n int numWays(int steps, int arrLen) {\n map<int,map<int,int>>dp;\n int startPos=0;\n return numWaysUtil(startPos,steps,dp,arrLen);\n \n }\n};", "memory": "59843" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int mod = 1000000007;\n int numWaysUtil(int startPos,int steps,map<int,map<int,int>>&dp,int &arrLen){\n if(steps==0){\n if(startPos==0)return 1;\n return 0;\n }\n if(dp.find(startPos)!=dp.end() && dp[startPos].find(steps) != dp[startPos].end())return dp[startPos][steps];\n int ans = 0;\n if(startPos-1>=0){\n ans = (ans + numWaysUtil(startPos-1,steps-1,dp,arrLen)%mod)%mod;\n }\n if(startPos+1<arrLen){\n ans = (ans + numWaysUtil(startPos+1,steps-1,dp,arrLen)%mod)%mod;\n }\n ans = (ans + numWaysUtil(startPos,steps-1,dp,arrLen)%mod)%mod;\n return dp[startPos][steps]=ans%mod;\n }\n int numWays(int steps, int arrLen) {\n map<int,map<int,int>>dp;\n int startPos=0;\n return numWaysUtil(startPos,steps,dp,arrLen);\n \n }\n};", "memory": "61019" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n struct pair_hash\n {\n template <typename T, typename U>\n std::size_t operator () (const std::pair<T, U> &x) const\n {\n return std::hash<T>()(x.first) ^ std::hash<U>()(x.second);\n }\n };\n\n int numWays(int steps, int arrLen) \n {\n int MOD = 1000000007;\n std::unordered_map<std::pair<int, int>, int, pair_hash> ways;\n // <int, int> -- <steps, index>\n\n ways[{1,0}] = 1;\n ways[{1,1}] = 1;\n\n for (int step = 2; step <= steps; ++step)\n {\n int max_ind = std::min(step, arrLen - 1);\n for (int ind = 0; ind <= max_ind; ++ind)\n {\n int w = ways[{step - 1, ind}];\n\n if (ind > 0)\n w = (w + ways[{step - 1, ind - 1}]) % MOD;\n\n if (ind < max_ind)\n w = (w + ways[{step - 1, ind + 1}]) % MOD;\n\n ways[{step, ind}] = w;\n }\n }\n\n return ways[{steps, 0}];\n }\n};\n\n\n\n\n// class Solution {\n// public:\n// int MOD = 1000000007;\n\n// int numLR(int steps, int arrLen)\n// {\n// int l = steps / 2;\n// return 0;\n// }\n\n// int fact(int n)\n// {\n// int r = 1;\n// for (int i = 1; i <= n; ++i)\n// {\n// r = r * i;\n// }\n// return r;\n// }\n\n// int numZ(int zeroes, int len)\n// {\n// // 2 zeroes, <><> len = 4\n\n// len = len + zeroes;\n\n// int r = 1;\n// for (int i = 0; i < zeroes; ++i)\n// {\n// r = r * (len - i) % MOD;\n// }\n\n// r = r / fact(zeroes);\n\n// return r;\n// }\n\n\n// int f(int steps, int i, int open, string c)\n// {\n// //cout << \"s(\" << steps << \",\" << i << \",\" << open << \")\" << endl;\n\n// int r = 0;\n// if (i == steps - 1)\n// {\n// if (open == 1)\n// {\n// c += \")\";\n// cout << c << endl;\n// return 1;\n// }\n// else\n// {\n// return 0;\n// }\n// }\n\n// if (open > 0)\n// r += f(steps, i + 1, open - 1, c + \")\");\n \n// r += f(steps, i + 1, open + 1, c + \"(\");\n\n// return r;\n// }\n\n// int numWays(int steps, int arrLen) \n// {\n// f(10, 0, 0, \"\");\n\n// // cout << \"f(2): \" << f(2, 0, 0) << endl;\n// // cout << \"f(4): \" << f(4, 0, 0) << endl;\n// // cout << \"f(6): \" << f(6, 0, 0) << endl;\n// //cout << \"f(8): \" << f(8, 0, 0) << endl;\n// //cout << \"f(10): \" << f(10, 0, 0, \"\") << endl;\n// // cout << \"f(12): \" << f(12, 0, 0) << endl;\n\n// //f(4, 0, 0);\n// //cout << \"f(8): \" << f(8, 0, 0) << endl;\n\n// return 0;\n// }\n// };", "memory": "62195" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n struct pair_hash\n {\n template <typename T, typename U>\n std::size_t operator () (const std::pair<T, U> &x) const\n {\n return std::hash<T>()(x.first) ^ std::hash<U>()(x.second);\n }\n };\n\n int numWays(int steps, int arrLen) \n {\n int MOD = 1000000007;\n std::unordered_map<std::pair<int, int>, int, pair_hash> ways;\n // <int, int> -- <steps, index>\n\n ways[{1,0}] = 1;\n ways[{1,1}] = 1;\n\n for (int step = 2; step <= steps; ++step)\n {\n int max_ind = std::min(steps - step + 1, arrLen - 1);\n for (int ind = 0; ind <= max_ind; ++ind)\n {\n int w = ways[{step - 1, ind}];\n\n if (ind > 0)\n w = (w + ways[{step - 1, ind - 1}]) % MOD;\n\n if (ind < max_ind)\n w = (w + ways[{step - 1, ind + 1}]) % MOD;\n\n ways[{step, ind}] = w;\n }\n }\n\n return ways[{steps, 0}];\n }\n};\n\n\n\n\n// class Solution {\n// public:\n// int MOD = 1000000007;\n\n// int numLR(int steps, int arrLen)\n// {\n// int l = steps / 2;\n// return 0;\n// }\n\n// int fact(int n)\n// {\n// int r = 1;\n// for (int i = 1; i <= n; ++i)\n// {\n// r = r * i;\n// }\n// return r;\n// }\n\n// int numZ(int zeroes, int len)\n// {\n// // 2 zeroes, <><> len = 4\n\n// len = len + zeroes;\n\n// int r = 1;\n// for (int i = 0; i < zeroes; ++i)\n// {\n// r = r * (len - i) % MOD;\n// }\n\n// r = r / fact(zeroes);\n\n// return r;\n// }\n\n\n// int f(int steps, int i, int open, string c)\n// {\n// //cout << \"s(\" << steps << \",\" << i << \",\" << open << \")\" << endl;\n\n// int r = 0;\n// if (i == steps - 1)\n// {\n// if (open == 1)\n// {\n// c += \")\";\n// cout << c << endl;\n// return 1;\n// }\n// else\n// {\n// return 0;\n// }\n// }\n\n// if (open > 0)\n// r += f(steps, i + 1, open - 1, c + \")\");\n \n// r += f(steps, i + 1, open + 1, c + \"(\");\n\n// return r;\n// }\n\n// int numWays(int steps, int arrLen) \n// {\n// f(10, 0, 0, \"\");\n\n// // cout << \"f(2): \" << f(2, 0, 0) << endl;\n// // cout << \"f(4): \" << f(4, 0, 0) << endl;\n// // cout << \"f(6): \" << f(6, 0, 0) << endl;\n// //cout << \"f(8): \" << f(8, 0, 0) << endl;\n// //cout << \"f(10): \" << f(10, 0, 0, \"\") << endl;\n// // cout << \"f(12): \" << f(12, 0, 0) << endl;\n\n// //f(4, 0, 0);\n// //cout << \"f(8): \" << f(8, 0, 0) << endl;\n\n// return 0;\n// }\n// };", "memory": "62195" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n struct pair_hash\n {\n template <typename T, typename U>\n std::size_t operator () (const std::pair<T, U> &x) const\n {\n return std::hash<T>()(x.first) ^ std::hash<U>()(x.second);\n }\n };\n\n int numWays(int steps, int arrLen) \n {\n int MOD = 1000000007;\n std::unordered_map<std::pair<int, int>, int, pair_hash> ways;\n // <int, int> -- <steps, index>\n\n ways[{1,0}] = 1;\n ways[{1,1}] = 1;\n\n for (int step = 2; step <= steps; ++step)\n {\n int max_ind = std::min(step, arrLen - 1);\n for (int ind = 0; ind <= max_ind; ++ind)\n {\n int w = ways[{step - 1, ind}];\n\n if (ind > 0)\n w = (w + ways[{step - 1, ind - 1}]) % MOD;\n\n if (ind < max_ind)\n w = (w + ways[{step - 1, ind + 1}]) % MOD;\n\n ways[{step, ind}] = w;\n }\n }\n\n return ways[{steps, 0}];\n }\n};\n\n\n\n\n// class Solution {\n// public:\n// int MOD = 1000000007;\n\n// int numLR(int steps, int arrLen)\n// {\n// int l = steps / 2;\n// return 0;\n// }\n\n// int fact(int n)\n// {\n// int r = 1;\n// for (int i = 1; i <= n; ++i)\n// {\n// r = r * i;\n// }\n// return r;\n// }\n\n// int numZ(int zeroes, int len)\n// {\n// // 2 zeroes, <><> len = 4\n\n// len = len + zeroes;\n\n// int r = 1;\n// for (int i = 0; i < zeroes; ++i)\n// {\n// r = r * (len - i) % MOD;\n// }\n\n// r = r / fact(zeroes);\n\n// return r;\n// }\n\n\n// int f(int steps, int i, int open, string c)\n// {\n// //cout << \"s(\" << steps << \",\" << i << \",\" << open << \")\" << endl;\n\n// int r = 0;\n// if (i == steps - 1)\n// {\n// if (open == 1)\n// {\n// c += \")\";\n// cout << c << endl;\n// return 1;\n// }\n// else\n// {\n// return 0;\n// }\n// }\n\n// if (open > 0)\n// r += f(steps, i + 1, open - 1, c + \")\");\n \n// r += f(steps, i + 1, open + 1, c + \"(\");\n\n// return r;\n// }\n\n// int numWays(int steps, int arrLen) \n// {\n// f(10, 0, 0, \"\");\n\n// // cout << \"f(2): \" << f(2, 0, 0) << endl;\n// // cout << \"f(4): \" << f(4, 0, 0) << endl;\n// // cout << \"f(6): \" << f(6, 0, 0) << endl;\n// //cout << \"f(8): \" << f(8, 0, 0) << endl;\n// //cout << \"f(10): \" << f(10, 0, 0, \"\") << endl;\n// // cout << \"f(12): \" << f(12, 0, 0) << endl;\n\n// //f(4, 0, 0);\n// //cout << \"f(8): \" << f(8, 0, 0) << endl;\n\n// return 0;\n// }\n// };", "memory": "63371" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\nprivate:\n int mod = pow(10, 9)+7;\n int solve(int index, int steps, int& arrLen, unordered_map<long long int, unordered_map<long long int,long long int>>& dp){\n if(index<0 || index>=arrLen || steps<0)\n return 0;\n if(steps==0 && index==0)\n return 1;\n // if(dp[index][steps] != -1)\n // return dp[index][steps];\n if(dp[index].count(steps))\n return dp[index][steps];\n long long int moveRight = solve(index+1, steps-1, arrLen, dp)%mod;\n long long int moveLeft = solve(index-1, steps-1, arrLen, dp)%mod;\n long long int stayHere = solve(index, steps-1, arrLen, dp)%mod;\n return dp[index][steps] = (moveRight+moveLeft+stayHere)%mod;\n }\npublic:\n int numWays(int steps, int arrLen) {\n // vector<vector<long long int>> dp(arrLen, vector<long long int>(steps+1, -1));\n unordered_map<long long int, unordered_map<long long int,long long int>> dp;\n return solve(0,steps,arrLen,dp);\n }\n};", "memory": "64548" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long int func(long long int ind, long long int steps, long long int len, map<pair<long long int, long long int>, long long int>& dp, long long int m) {\n if (ind == 0 && steps == 0) return 1;\n if (ind < 0 || ind >= len || steps < 0) return 0;\n \n \n if (dp.find({ind, steps}) != dp.end()) return dp[{ind, steps}];\n \n long long int x = 0, y = 0, z = 0;\n if (ind < len - 1) {\n x = func(ind + 1, steps - 1, len, dp, m) % m;\n }\n if (ind > 0) {\n y = func(ind - 1, steps - 1, len, dp, m) % m;\n }\n z = func(ind, steps - 1, len, dp, m) % m;\n\n \n return dp[{ind, steps}] = (x + y + z) % m;\n }\n\n int numWays(int steps, int len) {\n map<pair<long long int, long long int>, long long int> dp;\n long long int m = 1e9 + 7;\n return func(0, steps, len, dp, m);\n }\n};\n", "memory": "65724" }
1,398
<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p> <p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> steps = 3, arrLen = 2 <strong>Output:</strong> 4 <strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps. Right, Left, Stay Stay, Right, Left Right, Stay, Left Stay, Stay, Stay </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> steps = 2, arrLen = 4 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps Right, Left Stay, Stay </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> steps = 4, arrLen = 2 <strong>Output:</strong> 8 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= steps &lt;= 500</code></li> <li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\n const int MOD = 1000000007;\n\npublic:\n int numWays(int steps, int arrLen) {\n vector<unordered_map<int, long long>> vec(steps);\n unordered_map<int, long long> mp, tp;\n mp[0] = 1;\n if(arrLen > 1)\n mp[1] = 1;\n vec[0] = mp;\n for(int i = 1; i < steps; i++){\n mp.clear();\n tp = vec[i-1];\n for(auto it = tp.begin(); it != tp.end(); it++){\n int el = it->first;\n if(el + 1 < arrLen) mp[el+1] = (mp[el+1] + it->second) % MOD;\n if(el - 1 >= 0) mp[el-1] = (mp[el-1] + it->second) % MOD;\n mp[el] = (mp[el] + it->second) % MOD;\n }\n vec[i] = mp;\n }\n return (int)vec[steps - 1][0];\n }\n};", "memory": "66900" }
1,400
<p><strong>Tic-tac-toe</strong> is played by two players <code>A</code> and <code>B</code> on a <code>3 x 3</code> grid. The rules of Tic-Tac-Toe are:</p> <ul> <li>Players take turns placing characters into empty squares <code>&#39; &#39;</code>.</li> <li>The first player <code>A</code> always places <code>&#39;X&#39;</code> characters, while the second player <code>B</code> always places <code>&#39;O&#39;</code> characters.</li> <li><code>&#39;X&#39;</code> and <code>&#39;O&#39;</code> characters are always placed into empty squares, never on filled ones.</li> <li>The game ends when there are <strong>three</strong> of the same (non-empty) character filling any row, column, or diagonal.</li> <li>The game also ends if all squares are non-empty.</li> <li>No more moves can be played if the game is over.</li> </ul> <p>Given a 2D integer array <code>moves</code> where <code>moves[i] = [row<sub>i</sub>, col<sub>i</sub>]</code> indicates that the <code>i<sup>th</sup></code> move will be played on <code>grid[row<sub>i</sub>][col<sub>i</sub>]</code>. return <em>the winner of the game if it exists</em> (<code>A</code> or <code>B</code>). In case the game ends in a draw return <code>&quot;Draw&quot;</code>. If there are still movements to play return <code>&quot;Pending&quot;</code>.</p> <p>You can assume that <code>moves</code> is valid (i.e., it follows the rules of <strong>Tic-Tac-Toe</strong>), the grid is initially empty, and <code>A</code> will play first.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo1-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[2,0],[1,1],[2,1],[2,2]] <strong>Output:</strong> &quot;A&quot; <strong>Explanation:</strong> A wins, they always play first. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo2-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]] <strong>Output:</strong> &quot;B&quot; <strong>Explanation:</strong> B wins. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo3-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]] <strong>Output:</strong> &quot;Draw&quot; <strong>Explanation:</strong> The game ends in a draw since there are no moves to make. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= moves.length &lt;= 9</code></li> <li><code>moves[i].length == 2</code></li> <li><code>0 &lt;= row<sub>i</sub>, col<sub>i</sub> &lt;= 2</code></li> <li>There are no repeated elements on <code>moves</code>.</li> <li><code>moves</code> follow the rules of tic tac toe.</li> </ul>
0
{ "code": "class Solution {\npublic:\n string tictactoe(vector<vector<int>>& moves) {\n \n bool printOutput = false;\n char board[9];\n for(int i=0; i<9; i++)\n board[i]='_';\n\n for(int i=0; i<moves.size(); i++)\n {\n const int &currX = moves[i][0];\n const int &currY = moves[i][1];\n\n board[currY*3+currX] = (i%2==0)?'A':'B';\n if(printOutput)\n {\n for(int y=0; y<3; y++)\n {\n for(int x=0; x<3; x++)\n {\n cout<<board[y*3+x]<<\" \";\n }\n cout<<endl;\n }\n cout<<endl;\n }\n\n //horizontal\n if(board[currY*3+0] == board[currY*3+1] && board[currY*3+0] == board[currY*3+2])\n return string(1,(board[currY*3+currX]));\n\n //vertical\n if(board[0*3+currX] == board[1*3+currX] && board[0*3+currX] == board[2*3+currX])\n return string(1,(board[currY*3+currX]));\n\n //cross doing down\n if((currX == currY) && board[0*3+0] == board[1*3+1] && board[1*3+1] == board[2*3+2])\n return string(1,(board[currY*3+currX]));\n\n //cross doing up\n if(((currX == 2 && currY == 0) || (currX == 0 && currY == 2) || (currX == 1 && currY == 1)) && board[2*3+0] == board[1*3+1] && board[1*3+1] == board[0*3+2])\n return string(1,(board[currY*3+currX]));\n }\n\n if(moves.size()<9)\n return \"Pending\";\n return \"Draw\"; \n }\n};", "memory": "9800" }
1,400
<p><strong>Tic-tac-toe</strong> is played by two players <code>A</code> and <code>B</code> on a <code>3 x 3</code> grid. The rules of Tic-Tac-Toe are:</p> <ul> <li>Players take turns placing characters into empty squares <code>&#39; &#39;</code>.</li> <li>The first player <code>A</code> always places <code>&#39;X&#39;</code> characters, while the second player <code>B</code> always places <code>&#39;O&#39;</code> characters.</li> <li><code>&#39;X&#39;</code> and <code>&#39;O&#39;</code> characters are always placed into empty squares, never on filled ones.</li> <li>The game ends when there are <strong>three</strong> of the same (non-empty) character filling any row, column, or diagonal.</li> <li>The game also ends if all squares are non-empty.</li> <li>No more moves can be played if the game is over.</li> </ul> <p>Given a 2D integer array <code>moves</code> where <code>moves[i] = [row<sub>i</sub>, col<sub>i</sub>]</code> indicates that the <code>i<sup>th</sup></code> move will be played on <code>grid[row<sub>i</sub>][col<sub>i</sub>]</code>. return <em>the winner of the game if it exists</em> (<code>A</code> or <code>B</code>). In case the game ends in a draw return <code>&quot;Draw&quot;</code>. If there are still movements to play return <code>&quot;Pending&quot;</code>.</p> <p>You can assume that <code>moves</code> is valid (i.e., it follows the rules of <strong>Tic-Tac-Toe</strong>), the grid is initially empty, and <code>A</code> will play first.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo1-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[2,0],[1,1],[2,1],[2,2]] <strong>Output:</strong> &quot;A&quot; <strong>Explanation:</strong> A wins, they always play first. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo2-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]] <strong>Output:</strong> &quot;B&quot; <strong>Explanation:</strong> B wins. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo3-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]] <strong>Output:</strong> &quot;Draw&quot; <strong>Explanation:</strong> The game ends in a draw since there are no moves to make. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= moves.length &lt;= 9</code></li> <li><code>moves[i].length == 2</code></li> <li><code>0 &lt;= row<sub>i</sub>, col<sub>i</sub> &lt;= 2</code></li> <li>There are no repeated elements on <code>moves</code>.</li> <li><code>moves</code> follow the rules of tic tac toe.</li> </ul>
0
{ "code": "class Solution {\npublic:\n\n //T O(n) S(9)\n string tictactoe(vector<vector<int>>& moves) {\n \n bool printOutput = false;\n char board[9];\n for(int i=0; i<9; i++)\n board[i]='_';\n\n for(int i=0; i<moves.size(); i++)\n {\n const int &currX = moves[i][0];\n const int &currY = moves[i][1];\n\n board[currY*3+currX] = (i%2==0)?'A':'B';\n if(printOutput)\n {\n for(int y=0; y<3; y++)\n {\n for(int x=0; x<3; x++)\n {\n cout<<board[y*3+x]<<\" \";\n }\n cout<<endl;\n }\n cout<<endl;\n }\n\n //horizontal\n if(board[currY*3+0] == board[currY*3+1] && board[currY*3+0] == board[currY*3+2])\n return string(1,(board[currY*3+currX]));\n\n //vertical\n if(board[0*3+currX] == board[1*3+currX] && board[0*3+currX] == board[2*3+currX])\n return string(1,(board[currY*3+currX]));\n\n //cross doing down (diagonal)\n if((currX == currY) && board[0*3+0] == board[1*3+1] && board[1*3+1] == board[2*3+2])\n return string(1,(board[currY*3+currX]));\n\n //cross doing up (anit-diagonal)\n if(\n //((currX == 2 && currY == 0) || (currX == 0 && currY == 2) || (currX == 1 && currY == 1)) &&\n (currX+currY==2) && \n board[2*3+0] == board[1*3+1] && board[1*3+1] == board[0*3+2]\n )\n return string(1,(board[currY*3+currX]));\n }\n\n if(moves.size()<9)\n return \"Pending\";\n return \"Draw\"; \n }\n};", "memory": "9800" }
1,400
<p><strong>Tic-tac-toe</strong> is played by two players <code>A</code> and <code>B</code> on a <code>3 x 3</code> grid. The rules of Tic-Tac-Toe are:</p> <ul> <li>Players take turns placing characters into empty squares <code>&#39; &#39;</code>.</li> <li>The first player <code>A</code> always places <code>&#39;X&#39;</code> characters, while the second player <code>B</code> always places <code>&#39;O&#39;</code> characters.</li> <li><code>&#39;X&#39;</code> and <code>&#39;O&#39;</code> characters are always placed into empty squares, never on filled ones.</li> <li>The game ends when there are <strong>three</strong> of the same (non-empty) character filling any row, column, or diagonal.</li> <li>The game also ends if all squares are non-empty.</li> <li>No more moves can be played if the game is over.</li> </ul> <p>Given a 2D integer array <code>moves</code> where <code>moves[i] = [row<sub>i</sub>, col<sub>i</sub>]</code> indicates that the <code>i<sup>th</sup></code> move will be played on <code>grid[row<sub>i</sub>][col<sub>i</sub>]</code>. return <em>the winner of the game if it exists</em> (<code>A</code> or <code>B</code>). In case the game ends in a draw return <code>&quot;Draw&quot;</code>. If there are still movements to play return <code>&quot;Pending&quot;</code>.</p> <p>You can assume that <code>moves</code> is valid (i.e., it follows the rules of <strong>Tic-Tac-Toe</strong>), the grid is initially empty, and <code>A</code> will play first.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo1-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[2,0],[1,1],[2,1],[2,2]] <strong>Output:</strong> &quot;A&quot; <strong>Explanation:</strong> A wins, they always play first. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo2-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]] <strong>Output:</strong> &quot;B&quot; <strong>Explanation:</strong> B wins. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo3-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]] <strong>Output:</strong> &quot;Draw&quot; <strong>Explanation:</strong> The game ends in a draw since there are no moves to make. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= moves.length &lt;= 9</code></li> <li><code>moves[i].length == 2</code></li> <li><code>0 &lt;= row<sub>i</sub>, col<sub>i</sub> &lt;= 2</code></li> <li>There are no repeated elements on <code>moves</code>.</li> <li><code>moves</code> follow the rules of tic tac toe.</li> </ul>
0
{ "code": "class Solution {\npublic:\n\n string tictactoe(vector<vector<int>>& moves) {\n \n vector<int> A_wins(8, 0), B_wins(8, 0);\n\n for(int i = 0; i < moves.size(); i ++){\n\n int row = moves[i][0], col = moves[i][1];\n\n if(i % 2 == 0){\n A_wins[row] ++;\n A_wins[col + 3] ++;\n if(row == col) A_wins[6] ++;\n if(row == 2 - col) A_wins[7] ++;\n }else{\n B_wins[row] ++;\n B_wins[col + 3] ++;\n if(row == col) B_wins[6] ++;\n if(row == 2 - col) B_wins[7] ++;\n }\n\n \n }\n\n for(int i = 0; i < 8; i ++){\n if(A_wins[i] == 3) return \"A\";\n if(B_wins[i] == 3) return \"B\";\n }\n return moves.size() == 9 ? \"Draw\" : \"Pending\";\n }\n};", "memory": "9900" }
1,400
<p><strong>Tic-tac-toe</strong> is played by two players <code>A</code> and <code>B</code> on a <code>3 x 3</code> grid. The rules of Tic-Tac-Toe are:</p> <ul> <li>Players take turns placing characters into empty squares <code>&#39; &#39;</code>.</li> <li>The first player <code>A</code> always places <code>&#39;X&#39;</code> characters, while the second player <code>B</code> always places <code>&#39;O&#39;</code> characters.</li> <li><code>&#39;X&#39;</code> and <code>&#39;O&#39;</code> characters are always placed into empty squares, never on filled ones.</li> <li>The game ends when there are <strong>three</strong> of the same (non-empty) character filling any row, column, or diagonal.</li> <li>The game also ends if all squares are non-empty.</li> <li>No more moves can be played if the game is over.</li> </ul> <p>Given a 2D integer array <code>moves</code> where <code>moves[i] = [row<sub>i</sub>, col<sub>i</sub>]</code> indicates that the <code>i<sup>th</sup></code> move will be played on <code>grid[row<sub>i</sub>][col<sub>i</sub>]</code>. return <em>the winner of the game if it exists</em> (<code>A</code> or <code>B</code>). In case the game ends in a draw return <code>&quot;Draw&quot;</code>. If there are still movements to play return <code>&quot;Pending&quot;</code>.</p> <p>You can assume that <code>moves</code> is valid (i.e., it follows the rules of <strong>Tic-Tac-Toe</strong>), the grid is initially empty, and <code>A</code> will play first.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo1-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[2,0],[1,1],[2,1],[2,2]] <strong>Output:</strong> &quot;A&quot; <strong>Explanation:</strong> A wins, they always play first. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo2-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]] <strong>Output:</strong> &quot;B&quot; <strong>Explanation:</strong> B wins. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo3-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]] <strong>Output:</strong> &quot;Draw&quot; <strong>Explanation:</strong> The game ends in a draw since there are no moves to make. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= moves.length &lt;= 9</code></li> <li><code>moves[i].length == 2</code></li> <li><code>0 &lt;= row<sub>i</sub>, col<sub>i</sub> &lt;= 2</code></li> <li>There are no repeated elements on <code>moves</code>.</li> <li><code>moves</code> follow the rules of tic tac toe.</li> </ul>
0
{ "code": "class Solution {\npublic:\n string tictactoe(vector<vector<int>>& moves) {\n int n=3;\n vector<int> rows(n);\n vector<int> cols(n);\n int diag1 = 0;\n int diag2 = 0;\n int currPlayer = 1;\n\n for(vector<int> currMove : moves){\n \trows[currMove[0]] += currPlayer;\n \tcols[currMove[1]] += currPlayer;\n \tdiag1 = currMove[0] == currMove[1] ? diag1 + currPlayer : diag1;\n \tdiag2 = currMove[0] + currMove[1] == n - 1 ? diag2 + currPlayer : diag2;\n\n \tif(abs(rows[currMove[0]]) == n || abs(cols[currMove[1]]) == n || abs(diag1) == n || abs(diag2) == n){\n \t\treturn currPlayer == 1 ? \"A\" : \"B\";\n \t}\n\n \tcurrPlayer *= -1; \n }\n\n return moves.size() < 9 ? \"Pending\" : \"Draw\";\n }\n};", "memory": "9900" }
1,400
<p><strong>Tic-tac-toe</strong> is played by two players <code>A</code> and <code>B</code> on a <code>3 x 3</code> grid. The rules of Tic-Tac-Toe are:</p> <ul> <li>Players take turns placing characters into empty squares <code>&#39; &#39;</code>.</li> <li>The first player <code>A</code> always places <code>&#39;X&#39;</code> characters, while the second player <code>B</code> always places <code>&#39;O&#39;</code> characters.</li> <li><code>&#39;X&#39;</code> and <code>&#39;O&#39;</code> characters are always placed into empty squares, never on filled ones.</li> <li>The game ends when there are <strong>three</strong> of the same (non-empty) character filling any row, column, or diagonal.</li> <li>The game also ends if all squares are non-empty.</li> <li>No more moves can be played if the game is over.</li> </ul> <p>Given a 2D integer array <code>moves</code> where <code>moves[i] = [row<sub>i</sub>, col<sub>i</sub>]</code> indicates that the <code>i<sup>th</sup></code> move will be played on <code>grid[row<sub>i</sub>][col<sub>i</sub>]</code>. return <em>the winner of the game if it exists</em> (<code>A</code> or <code>B</code>). In case the game ends in a draw return <code>&quot;Draw&quot;</code>. If there are still movements to play return <code>&quot;Pending&quot;</code>.</p> <p>You can assume that <code>moves</code> is valid (i.e., it follows the rules of <strong>Tic-Tac-Toe</strong>), the grid is initially empty, and <code>A</code> will play first.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo1-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[2,0],[1,1],[2,1],[2,2]] <strong>Output:</strong> &quot;A&quot; <strong>Explanation:</strong> A wins, they always play first. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo2-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]] <strong>Output:</strong> &quot;B&quot; <strong>Explanation:</strong> B wins. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo3-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]] <strong>Output:</strong> &quot;Draw&quot; <strong>Explanation:</strong> The game ends in a draw since there are no moves to make. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= moves.length &lt;= 9</code></li> <li><code>moves[i].length == 2</code></li> <li><code>0 &lt;= row<sub>i</sub>, col<sub>i</sub> &lt;= 2</code></li> <li>There are no repeated elements on <code>moves</code>.</li> <li><code>moves</code> follow the rules of tic tac toe.</li> </ul>
0
{ "code": "class Solution {\npublic:\n string tictactoe(vector<vector<int>>& moves) {\n vector<vector<char>> grid(3, vector<char>(3, ' '));\n \n \n for (int i = 0; i < moves.size(); ++i) {\n int row = moves[i][0], col = moves[i][1];\n if (i % 2 == 0) {\n grid[row][col] = 'X'; // Player A's move\n } else {\n grid[row][col] = 'O'; // Player B's move\n }\n }\n \n for (int i = 0; i < 3; ++i) {\n if (grid[i][0] != ' ' && grid[i][0] == grid[i][1] && grid[i][1] == grid[i][2]) {\n return grid[i][0] == 'X' ? \"A\" : \"B\";\n }\n \n if (grid[0][i] != ' ' && grid[0][i] == grid[1][i] && grid[1][i] == grid[2][i]) {\n return grid[0][i] == 'X' ? \"A\" : \"B\";\n }\n }\n \n if (grid[0][0] != ' ' && grid[0][0] == grid[1][1] && grid[1][1] == grid[2][2]) {\n return grid[0][0] == 'X' ? \"A\" : \"B\";\n }\n if (grid[0][2] != ' ' && grid[0][2] == grid[1][1] && grid[1][1] == grid[2][0]) {\n return grid[0][2] == 'X' ? \"A\" : \"B\";\n }\n \n \n if (moves.size() == 9) {\n return \"Draw\";\n }\n \n return \"Pending\";\n }\n};", "memory": "10000" }
1,400
<p><strong>Tic-tac-toe</strong> is played by two players <code>A</code> and <code>B</code> on a <code>3 x 3</code> grid. The rules of Tic-Tac-Toe are:</p> <ul> <li>Players take turns placing characters into empty squares <code>&#39; &#39;</code>.</li> <li>The first player <code>A</code> always places <code>&#39;X&#39;</code> characters, while the second player <code>B</code> always places <code>&#39;O&#39;</code> characters.</li> <li><code>&#39;X&#39;</code> and <code>&#39;O&#39;</code> characters are always placed into empty squares, never on filled ones.</li> <li>The game ends when there are <strong>three</strong> of the same (non-empty) character filling any row, column, or diagonal.</li> <li>The game also ends if all squares are non-empty.</li> <li>No more moves can be played if the game is over.</li> </ul> <p>Given a 2D integer array <code>moves</code> where <code>moves[i] = [row<sub>i</sub>, col<sub>i</sub>]</code> indicates that the <code>i<sup>th</sup></code> move will be played on <code>grid[row<sub>i</sub>][col<sub>i</sub>]</code>. return <em>the winner of the game if it exists</em> (<code>A</code> or <code>B</code>). In case the game ends in a draw return <code>&quot;Draw&quot;</code>. If there are still movements to play return <code>&quot;Pending&quot;</code>.</p> <p>You can assume that <code>moves</code> is valid (i.e., it follows the rules of <strong>Tic-Tac-Toe</strong>), the grid is initially empty, and <code>A</code> will play first.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo1-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[2,0],[1,1],[2,1],[2,2]] <strong>Output:</strong> &quot;A&quot; <strong>Explanation:</strong> A wins, they always play first. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo2-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]] <strong>Output:</strong> &quot;B&quot; <strong>Explanation:</strong> B wins. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo3-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]] <strong>Output:</strong> &quot;Draw&quot; <strong>Explanation:</strong> The game ends in a draw since there are no moves to make. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= moves.length &lt;= 9</code></li> <li><code>moves[i].length == 2</code></li> <li><code>0 &lt;= row<sub>i</sub>, col<sub>i</sub> &lt;= 2</code></li> <li>There are no repeated elements on <code>moves</code>.</li> <li><code>moves</code> follow the rules of tic tac toe.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int n;\n string tictactoe(vector<vector<int>>& moves) {\n\n vector<vector<int>> board(3, vector<int>(3, 0));\n int player = 1;\n n = moves.size();\n\n // Fill the board\n for (int i = 0; i < moves.size(); i++) {\n int x = moves[i][0];\n int y = moves[i][1];\n\n board[x][y] = player;\n\n if (canWin(board, player)) {\n\n return player == 1 ? \"A\" : \"B\";\n }\n\n player *= -1; // change the player A <--> B\n }\n return moves.size() == 9 ? \"Draw\" : \"Pending\";\n }\n\n\n bool canWin( vector<vector<int>> &board, int &player )\n {\n for( int i=0 ;i<3; i++)\n {\n if( board[i][0] == player && board[i][1] == player && board[i][2] == player ) return true;\n\n if( board[0][i] == player && board[1][i] == player && board[2][i] == player ) return true;\n }\n\n if (board[0][0] == player && board[1][1] == player && board[2][2] == player) return true;\n if (board[0][2] == player && board[1][1] == player && board[2][0] == player) return true;\n \n return false;\n\n\n }\n};", "memory": "10100" }
1,400
<p><strong>Tic-tac-toe</strong> is played by two players <code>A</code> and <code>B</code> on a <code>3 x 3</code> grid. The rules of Tic-Tac-Toe are:</p> <ul> <li>Players take turns placing characters into empty squares <code>&#39; &#39;</code>.</li> <li>The first player <code>A</code> always places <code>&#39;X&#39;</code> characters, while the second player <code>B</code> always places <code>&#39;O&#39;</code> characters.</li> <li><code>&#39;X&#39;</code> and <code>&#39;O&#39;</code> characters are always placed into empty squares, never on filled ones.</li> <li>The game ends when there are <strong>three</strong> of the same (non-empty) character filling any row, column, or diagonal.</li> <li>The game also ends if all squares are non-empty.</li> <li>No more moves can be played if the game is over.</li> </ul> <p>Given a 2D integer array <code>moves</code> where <code>moves[i] = [row<sub>i</sub>, col<sub>i</sub>]</code> indicates that the <code>i<sup>th</sup></code> move will be played on <code>grid[row<sub>i</sub>][col<sub>i</sub>]</code>. return <em>the winner of the game if it exists</em> (<code>A</code> or <code>B</code>). In case the game ends in a draw return <code>&quot;Draw&quot;</code>. If there are still movements to play return <code>&quot;Pending&quot;</code>.</p> <p>You can assume that <code>moves</code> is valid (i.e., it follows the rules of <strong>Tic-Tac-Toe</strong>), the grid is initially empty, and <code>A</code> will play first.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo1-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[2,0],[1,1],[2,1],[2,2]] <strong>Output:</strong> &quot;A&quot; <strong>Explanation:</strong> A wins, they always play first. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo2-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]] <strong>Output:</strong> &quot;B&quot; <strong>Explanation:</strong> B wins. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo3-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]] <strong>Output:</strong> &quot;Draw&quot; <strong>Explanation:</strong> The game ends in a draw since there are no moves to make. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= moves.length &lt;= 9</code></li> <li><code>moves[i].length == 2</code></li> <li><code>0 &lt;= row<sub>i</sub>, col<sub>i</sub> &lt;= 2</code></li> <li>There are no repeated elements on <code>moves</code>.</li> <li><code>moves</code> follow the rules of tic tac toe.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool check(vector<vector<char>> &grid, char c) {\n for(int i=0; i<3; i++) {\n if(grid[i][0] == c && grid[i][1] == c && grid[i][2] == c) return true;\n if(grid[0][i] == c && grid[1][i] == c && grid[2][i] == c) return true;\n }\n\n if(grid[1][1] == c && ((grid[0][0] == c && grid[2][2] == c) || (grid[0][2] == c && grid[2][0] == c))) return true;\n return false;\n }\n\n string tictactoe(vector<vector<int>>& moves) {\n int n = moves.size();\n vector<vector<char>> grid(3, vector<char>(3, ' '));\n\n for(int i=0; i<n; i++) {\n if(i % 2 == 0) grid[moves[i][0]][moves[i][1]] = 'X';\n else grid[moves[i][0]][moves[i][1]] = 'O';\n }\n\n bool A = check(grid, 'X');\n bool B = check(grid, 'O');\n\n if(!A && !B && n == 9) return \"Draw\";\n else if(A) return \"A\";\n else if(B) return \"B\";\n else return \"Pending\";\n }\n};", "memory": "10200" }
1,400
<p><strong>Tic-tac-toe</strong> is played by two players <code>A</code> and <code>B</code> on a <code>3 x 3</code> grid. The rules of Tic-Tac-Toe are:</p> <ul> <li>Players take turns placing characters into empty squares <code>&#39; &#39;</code>.</li> <li>The first player <code>A</code> always places <code>&#39;X&#39;</code> characters, while the second player <code>B</code> always places <code>&#39;O&#39;</code> characters.</li> <li><code>&#39;X&#39;</code> and <code>&#39;O&#39;</code> characters are always placed into empty squares, never on filled ones.</li> <li>The game ends when there are <strong>three</strong> of the same (non-empty) character filling any row, column, or diagonal.</li> <li>The game also ends if all squares are non-empty.</li> <li>No more moves can be played if the game is over.</li> </ul> <p>Given a 2D integer array <code>moves</code> where <code>moves[i] = [row<sub>i</sub>, col<sub>i</sub>]</code> indicates that the <code>i<sup>th</sup></code> move will be played on <code>grid[row<sub>i</sub>][col<sub>i</sub>]</code>. return <em>the winner of the game if it exists</em> (<code>A</code> or <code>B</code>). In case the game ends in a draw return <code>&quot;Draw&quot;</code>. If there are still movements to play return <code>&quot;Pending&quot;</code>.</p> <p>You can assume that <code>moves</code> is valid (i.e., it follows the rules of <strong>Tic-Tac-Toe</strong>), the grid is initially empty, and <code>A</code> will play first.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo1-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[2,0],[1,1],[2,1],[2,2]] <strong>Output:</strong> &quot;A&quot; <strong>Explanation:</strong> A wins, they always play first. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo2-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]] <strong>Output:</strong> &quot;B&quot; <strong>Explanation:</strong> B wins. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo3-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]] <strong>Output:</strong> &quot;Draw&quot; <strong>Explanation:</strong> The game ends in a draw since there are no moves to make. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= moves.length &lt;= 9</code></li> <li><code>moves[i].length == 2</code></li> <li><code>0 &lt;= row<sub>i</sub>, col<sub>i</sub> &lt;= 2</code></li> <li>There are no repeated elements on <code>moves</code>.</li> <li><code>moves</code> follow the rules of tic tac toe.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string tictactoe(vector<vector<int>>& moves) {\n char grid[3][3], r, c;\n int m, n = 0, i;\n char ch;\n string a=\"A\", b=\"B\", draw=\"Draw\", pending=\"Pending\";\n for(i=0; i<moves.size(); i++){\n r = moves[i][0];\n c = moves[i][1];\n if(i%2 == 0){\n grid[r][c] = 'X';\n }\n else{\n grid[r][c] = 'O';\n }\n if(i>=4){\n if(i%2 == 0){\n ch = 'X';\n }\n else{\n ch = 'O';\n }\n if(grid[0][0] == ch && grid[1][1] == ch && grid[2][2] == ch ){\n n = 3;\n break;\n }\n if(grid[0][2] == ch && grid[1][1] == ch && grid[2][0] == ch ){\n n = 3;\n break;\n }\n for(m=0; m<3; m++){\n if(grid[m][0] == ch && grid[m][1] == ch && grid[m][2] == ch ){\n n = 3;\n break;\n }\n if(grid[0][m] == ch && grid[1][m] == ch && grid[2][m] == ch ){\n n = 3;\n break;\n }\n }\n }\n if(n==3) break;\n }\n if(n == 3 && ch == 'X') return a;\n else if(n == 3 && ch == 'O') return b;\n else if(i == 9) return draw;\n else return pending;\n }\n};", "memory": "10200" }
1,400
<p><strong>Tic-tac-toe</strong> is played by two players <code>A</code> and <code>B</code> on a <code>3 x 3</code> grid. The rules of Tic-Tac-Toe are:</p> <ul> <li>Players take turns placing characters into empty squares <code>&#39; &#39;</code>.</li> <li>The first player <code>A</code> always places <code>&#39;X&#39;</code> characters, while the second player <code>B</code> always places <code>&#39;O&#39;</code> characters.</li> <li><code>&#39;X&#39;</code> and <code>&#39;O&#39;</code> characters are always placed into empty squares, never on filled ones.</li> <li>The game ends when there are <strong>three</strong> of the same (non-empty) character filling any row, column, or diagonal.</li> <li>The game also ends if all squares are non-empty.</li> <li>No more moves can be played if the game is over.</li> </ul> <p>Given a 2D integer array <code>moves</code> where <code>moves[i] = [row<sub>i</sub>, col<sub>i</sub>]</code> indicates that the <code>i<sup>th</sup></code> move will be played on <code>grid[row<sub>i</sub>][col<sub>i</sub>]</code>. return <em>the winner of the game if it exists</em> (<code>A</code> or <code>B</code>). In case the game ends in a draw return <code>&quot;Draw&quot;</code>. If there are still movements to play return <code>&quot;Pending&quot;</code>.</p> <p>You can assume that <code>moves</code> is valid (i.e., it follows the rules of <strong>Tic-Tac-Toe</strong>), the grid is initially empty, and <code>A</code> will play first.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo1-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[2,0],[1,1],[2,1],[2,2]] <strong>Output:</strong> &quot;A&quot; <strong>Explanation:</strong> A wins, they always play first. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo2-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]] <strong>Output:</strong> &quot;B&quot; <strong>Explanation:</strong> B wins. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo3-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]] <strong>Output:</strong> &quot;Draw&quot; <strong>Explanation:</strong> The game ends in a draw since there are no moves to make. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= moves.length &lt;= 9</code></li> <li><code>moves[i].length == 2</code></li> <li><code>0 &lt;= row<sub>i</sub>, col<sub>i</sub> &lt;= 2</code></li> <li>There are no repeated elements on <code>moves</code>.</li> <li><code>moves</code> follow the rules of tic tac toe.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string tictactoe(vector<vector<int>>& moves) { \n int winner = checkWinner(moves);\n if (winner == 1 || winner == 2) { \n return winner == 1 ? \"A\" : \"B\";\n }\n\n if (moves.size() == 9) {\n return \"Draw\";\n }\n return \"Pending\"; \n }\n\n int checkWinner(vector<vector<int>>& moves) {\n vector<vector<char>> grid(3, vector<char> (3, ' '));\n for (int i = 0; i < moves.size(); ++i) {\n vector<int> pos = moves[i];\n if (i % 2 == 0)\n grid[pos[0]][pos[1]] = 'X';\n else \n grid[pos[0]][pos[1]] = 'o';\n }\n\n // check row:\n for (int row = 0; row < 3; row++) {\n if (grid[row][0] == ' ' || grid[row][1] == ' ' || grid[row][2] == ' ')\n continue;\n if (grid[row][0] == grid[row][1] && grid[row][1] == grid[row][2])\n return grid[row][0] == 'X' ? 1 : 2;\n }\n\n // check col:\n for (int col = 0; col < 3; col++) {\n if (grid[0][col] == ' ' || grid[1][col] == ' ' || grid[2][col] == ' ')\n continue;\n if (grid[0][col] == grid[1][col] && grid[1][col] == grid[2][col])\n return grid[0][col] == 'X' ? 1 : 2;\n }\n\n // check diagron:\n if (grid[0][2] == 'X' && grid[1][1] == 'X' && grid[2][0] == 'X')\n return 1;\n if (grid[0][2] == 'o' && grid[1][1] == 'o' && grid[2][0] == 'o')\n return 2;\n if (grid[0][0] == 'X' && grid[1][1] == 'X' && grid[2][2] == 'X')\n return 1;\n if (grid[0][0] == 'o' && grid[1][1] == 'o' && grid[2][2] == 'o')\n return 2;\n return 0;\n\n }\n\n\n};", "memory": "10300" }
1,400
<p><strong>Tic-tac-toe</strong> is played by two players <code>A</code> and <code>B</code> on a <code>3 x 3</code> grid. The rules of Tic-Tac-Toe are:</p> <ul> <li>Players take turns placing characters into empty squares <code>&#39; &#39;</code>.</li> <li>The first player <code>A</code> always places <code>&#39;X&#39;</code> characters, while the second player <code>B</code> always places <code>&#39;O&#39;</code> characters.</li> <li><code>&#39;X&#39;</code> and <code>&#39;O&#39;</code> characters are always placed into empty squares, never on filled ones.</li> <li>The game ends when there are <strong>three</strong> of the same (non-empty) character filling any row, column, or diagonal.</li> <li>The game also ends if all squares are non-empty.</li> <li>No more moves can be played if the game is over.</li> </ul> <p>Given a 2D integer array <code>moves</code> where <code>moves[i] = [row<sub>i</sub>, col<sub>i</sub>]</code> indicates that the <code>i<sup>th</sup></code> move will be played on <code>grid[row<sub>i</sub>][col<sub>i</sub>]</code>. return <em>the winner of the game if it exists</em> (<code>A</code> or <code>B</code>). In case the game ends in a draw return <code>&quot;Draw&quot;</code>. If there are still movements to play return <code>&quot;Pending&quot;</code>.</p> <p>You can assume that <code>moves</code> is valid (i.e., it follows the rules of <strong>Tic-Tac-Toe</strong>), the grid is initially empty, and <code>A</code> will play first.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo1-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[2,0],[1,1],[2,1],[2,2]] <strong>Output:</strong> &quot;A&quot; <strong>Explanation:</strong> A wins, they always play first. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo2-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]] <strong>Output:</strong> &quot;B&quot; <strong>Explanation:</strong> B wins. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo3-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]] <strong>Output:</strong> &quot;Draw&quot; <strong>Explanation:</strong> The game ends in a draw since there are no moves to make. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= moves.length &lt;= 9</code></li> <li><code>moves[i].length == 2</code></li> <li><code>0 &lt;= row<sub>i</sub>, col<sub>i</sub> &lt;= 2</code></li> <li>There are no repeated elements on <code>moves</code>.</li> <li><code>moves</code> follow the rules of tic tac toe.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string tictactoe(vector<vector<int>>& moves) {\n vector<vector<int>> board;\n for(int i = 0;i<3;i++){\n vector<int> vec;\n for(int j = 0;j<3;j++){\n vec.push_back(-1);\n }\n board.push_back(vec);\n }\n for(int i = 0;i<moves.size();i++){\n if(i % 2 == 0) board[moves[i][0]][moves[i][1]] = 1;\n else board[moves[i][0]][moves[i][1]] = 0;\n }\n bool xWin = ((board[0][0]==1 && board[0][1]==1 && board[0][2]==1) || (board[1][0]==1 && board[1][1]==1 && board[1][2]==1) || (board[2][0]==1 && board[2][1]==1 && board[2][2]==1) || (board[0][0]==1 && board[1][0]==1 && board[2][0]==1) || (board[0][1]==1 && board[1][1]==1 && board[2][1]==1) || (board[0][2]==1 && board[1][2]==1 && board[2][2]==1) || (board[0][0]==1 && board[1][1]==1 && board[2][2]==1) || (board[0][2]==1 && board[1][1]==1 && board[2][0]==1));\n bool oWin = ((board[0][0]==0 && board[0][1]==0 && board[0][2]==0) || (board[1][0]==0 && board[1][1]==0 && board[1][2]==0) || (board[2][0]==0 && board[2][1]==0 && board[2][2]==0) || (board[0][0]==0 && board[1][0]==0 && board[2][0]==0) || (board[0][1]==0 && board[1][1]==0 && board[2][1]==0) || (board[0][2]==0 && board[1][2]==0 && board[2][2]==0) || (board[0][0]==0 && board[1][1]==0 && board[2][2]==0) || (board[0][2]==0 && board[1][1]==0 && board[2][0]==0));\n if(xWin) return \"A\";\n else if(oWin) return \"B\";\n else if(moves.size() < 9) return \"Pending\";\n return \"Draw\";\n }\n};", "memory": "10400" }
1,400
<p><strong>Tic-tac-toe</strong> is played by two players <code>A</code> and <code>B</code> on a <code>3 x 3</code> grid. The rules of Tic-Tac-Toe are:</p> <ul> <li>Players take turns placing characters into empty squares <code>&#39; &#39;</code>.</li> <li>The first player <code>A</code> always places <code>&#39;X&#39;</code> characters, while the second player <code>B</code> always places <code>&#39;O&#39;</code> characters.</li> <li><code>&#39;X&#39;</code> and <code>&#39;O&#39;</code> characters are always placed into empty squares, never on filled ones.</li> <li>The game ends when there are <strong>three</strong> of the same (non-empty) character filling any row, column, or diagonal.</li> <li>The game also ends if all squares are non-empty.</li> <li>No more moves can be played if the game is over.</li> </ul> <p>Given a 2D integer array <code>moves</code> where <code>moves[i] = [row<sub>i</sub>, col<sub>i</sub>]</code> indicates that the <code>i<sup>th</sup></code> move will be played on <code>grid[row<sub>i</sub>][col<sub>i</sub>]</code>. return <em>the winner of the game if it exists</em> (<code>A</code> or <code>B</code>). In case the game ends in a draw return <code>&quot;Draw&quot;</code>. If there are still movements to play return <code>&quot;Pending&quot;</code>.</p> <p>You can assume that <code>moves</code> is valid (i.e., it follows the rules of <strong>Tic-Tac-Toe</strong>), the grid is initially empty, and <code>A</code> will play first.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo1-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[2,0],[1,1],[2,1],[2,2]] <strong>Output:</strong> &quot;A&quot; <strong>Explanation:</strong> A wins, they always play first. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo2-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]] <strong>Output:</strong> &quot;B&quot; <strong>Explanation:</strong> B wins. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo3-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]] <strong>Output:</strong> &quot;Draw&quot; <strong>Explanation:</strong> The game ends in a draw since there are no moves to make. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= moves.length &lt;= 9</code></li> <li><code>moves[i].length == 2</code></li> <li><code>0 &lt;= row<sub>i</sub>, col<sub>i</sub> &lt;= 2</code></li> <li>There are no repeated elements on <code>moves</code>.</li> <li><code>moves</code> follow the rules of tic tac toe.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string tictactoe(vector<vector<int>>& moves) {\n // Define all possible winning combinations\n vector<vector<int>> winner{{0,1,2},\n {3,4,5},\n {6,7,8},\n {0,3,6},\n {1,4,7},\n {2,5,8},\n {0,4,8},\n {2,4,6}};\n // Initialize the board as a 1D vector with 9 empty strings\n vector<string> pos(9, \"\");\n bool yesa = true;\n\n // Populate the board with the moves\n for(int i = 0; i < moves.size(); i++) {\n int idx = moves[i][0] * 3 + moves[i][1];\n if(yesa) {\n pos[idx] = \"A\";\n yesa = false;\n } else {\n pos[idx] = \"B\";\n yesa = true;\n }\n }\n\n // Check for a winner\n for(int i = 0; i < 8; i++) {\n if((pos[winner[i][0]] != \"\" || pos[winner[i][1]] != \"\" || pos[winner[i][2]] != \"\") \n && (pos[winner[i][0]] == pos[winner[i][1]]) \n && (pos[winner[i][1]] == pos[winner[i][2]])) {\n return pos[winner[i][0]];\n }\n }\n\n // Determine if the game is a draw or still pending\n return moves.size() == 9 ? \"Draw\" : \"Pending\";\n }\n};", "memory": "10500" }
1,400
<p><strong>Tic-tac-toe</strong> is played by two players <code>A</code> and <code>B</code> on a <code>3 x 3</code> grid. The rules of Tic-Tac-Toe are:</p> <ul> <li>Players take turns placing characters into empty squares <code>&#39; &#39;</code>.</li> <li>The first player <code>A</code> always places <code>&#39;X&#39;</code> characters, while the second player <code>B</code> always places <code>&#39;O&#39;</code> characters.</li> <li><code>&#39;X&#39;</code> and <code>&#39;O&#39;</code> characters are always placed into empty squares, never on filled ones.</li> <li>The game ends when there are <strong>three</strong> of the same (non-empty) character filling any row, column, or diagonal.</li> <li>The game also ends if all squares are non-empty.</li> <li>No more moves can be played if the game is over.</li> </ul> <p>Given a 2D integer array <code>moves</code> where <code>moves[i] = [row<sub>i</sub>, col<sub>i</sub>]</code> indicates that the <code>i<sup>th</sup></code> move will be played on <code>grid[row<sub>i</sub>][col<sub>i</sub>]</code>. return <em>the winner of the game if it exists</em> (<code>A</code> or <code>B</code>). In case the game ends in a draw return <code>&quot;Draw&quot;</code>. If there are still movements to play return <code>&quot;Pending&quot;</code>.</p> <p>You can assume that <code>moves</code> is valid (i.e., it follows the rules of <strong>Tic-Tac-Toe</strong>), the grid is initially empty, and <code>A</code> will play first.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo1-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[2,0],[1,1],[2,1],[2,2]] <strong>Output:</strong> &quot;A&quot; <strong>Explanation:</strong> A wins, they always play first. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo2-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]] <strong>Output:</strong> &quot;B&quot; <strong>Explanation:</strong> B wins. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo3-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]] <strong>Output:</strong> &quot;Draw&quot; <strong>Explanation:</strong> The game ends in a draw since there are no moves to make. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= moves.length &lt;= 9</code></li> <li><code>moves[i].length == 2</code></li> <li><code>0 &lt;= row<sub>i</sub>, col<sub>i</sub> &lt;= 2</code></li> <li>There are no repeated elements on <code>moves</code>.</li> <li><code>moves</code> follow the rules of tic tac toe.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string tictactoe(vector<vector<int>>& moves) {\n // Define all possible winning combinations\n vector<vector<int>> winner{{0,1,2},\n {3,4,5},\n {6,7,8},\n {0,3,6},\n {1,4,7},\n {2,5,8},\n {0,4,8},\n {2,4,6}};\n // Initialize the board as a 1D vector with 9 empty strings\n vector<string> pos(9, \"\");\n bool yesa = true;\n\n // Populate the board with the moves\n for(int i = 0; i < moves.size(); i++) {\n int idx = moves[i][0] * 3 + moves[i][1];\n if(yesa) {\n pos[idx] = \"A\";\n yesa = false;\n } else {\n pos[idx] = \"B\";\n yesa = true;\n }\n }\n\n // Check for a winner\n for(int i = 0; i < 8; i++) {\n if((pos[winner[i][0]] != \"\" || pos[winner[i][1]] != \"\" || pos[winner[i][2]] != \"\") \n && (pos[winner[i][0]] == pos[winner[i][1]]) \n && (pos[winner[i][1]] == pos[winner[i][2]])) {\n return pos[winner[i][0]];\n }\n }\n\n // Determine if the game is a draw or still pending\n return moves.size() == 9 ? \"Draw\" : \"Pending\";\n }\n};", "memory": "10500" }
1,400
<p><strong>Tic-tac-toe</strong> is played by two players <code>A</code> and <code>B</code> on a <code>3 x 3</code> grid. The rules of Tic-Tac-Toe are:</p> <ul> <li>Players take turns placing characters into empty squares <code>&#39; &#39;</code>.</li> <li>The first player <code>A</code> always places <code>&#39;X&#39;</code> characters, while the second player <code>B</code> always places <code>&#39;O&#39;</code> characters.</li> <li><code>&#39;X&#39;</code> and <code>&#39;O&#39;</code> characters are always placed into empty squares, never on filled ones.</li> <li>The game ends when there are <strong>three</strong> of the same (non-empty) character filling any row, column, or diagonal.</li> <li>The game also ends if all squares are non-empty.</li> <li>No more moves can be played if the game is over.</li> </ul> <p>Given a 2D integer array <code>moves</code> where <code>moves[i] = [row<sub>i</sub>, col<sub>i</sub>]</code> indicates that the <code>i<sup>th</sup></code> move will be played on <code>grid[row<sub>i</sub>][col<sub>i</sub>]</code>. return <em>the winner of the game if it exists</em> (<code>A</code> or <code>B</code>). In case the game ends in a draw return <code>&quot;Draw&quot;</code>. If there are still movements to play return <code>&quot;Pending&quot;</code>.</p> <p>You can assume that <code>moves</code> is valid (i.e., it follows the rules of <strong>Tic-Tac-Toe</strong>), the grid is initially empty, and <code>A</code> will play first.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo1-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[2,0],[1,1],[2,1],[2,2]] <strong>Output:</strong> &quot;A&quot; <strong>Explanation:</strong> A wins, they always play first. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo2-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]] <strong>Output:</strong> &quot;B&quot; <strong>Explanation:</strong> B wins. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo3-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]] <strong>Output:</strong> &quot;Draw&quot; <strong>Explanation:</strong> The game ends in a draw since there are no moves to make. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= moves.length &lt;= 9</code></li> <li><code>moves[i].length == 2</code></li> <li><code>0 &lt;= row<sub>i</sub>, col<sub>i</sub> &lt;= 2</code></li> <li>There are no repeated elements on <code>moves</code>.</li> <li><code>moves</code> follow the rules of tic tac toe.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool hasWinner(string p1, string p2, string p3)\n {\n if(p1 == \"P\")\n return false;\n return p1 == p2 && p2 == p3;\n }\n\n string tictactoe(vector<vector<int>>& moves) {\n // create a matrix 3x3 with all elements equal to \"0\"\n // -1 will be the \"empty\" spots\n vector<vector<string>> matrix(3, vector<string>(3, \"P\"));\n // fill matrix with A moves (even)\n for(auto i = 0; i < moves.size(); i = i + 2)\n {\n const auto& v = moves[i];\n matrix[v[0]][v[1]] = \"A\";\n }\n //fill matrix with B moves (odd)\n for(int i = 1; i < moves.size(); i = i + 2)\n {\n const auto& v = moves[i];\n matrix[v[0]][v[1]] = \"B\";\n }\n // check for winner\n for(auto i = 0; i < matrix.size(); ++i)\n {\n //check for row\n if(hasWinner(matrix[i][0],matrix[i][1], matrix[i][2]))\n return matrix[i][0];\n //check for column\n if(hasWinner(matrix[0][i], matrix[1][i], matrix[2][i]))\n return matrix[0][i];\n //diagonals\n if(hasWinner(matrix[0][0], matrix[1][1], matrix[2][2]))\n return matrix[0][0]; \n if(hasWinner(matrix[0][2], matrix[1][1], matrix[2][0]))\n return matrix[0][2]; \n }\n if(moves.size() == 9)\n return \"Draw\";\n return \"Pending\";\n }\n};", "memory": "10600" }
1,400
<p><strong>Tic-tac-toe</strong> is played by two players <code>A</code> and <code>B</code> on a <code>3 x 3</code> grid. The rules of Tic-Tac-Toe are:</p> <ul> <li>Players take turns placing characters into empty squares <code>&#39; &#39;</code>.</li> <li>The first player <code>A</code> always places <code>&#39;X&#39;</code> characters, while the second player <code>B</code> always places <code>&#39;O&#39;</code> characters.</li> <li><code>&#39;X&#39;</code> and <code>&#39;O&#39;</code> characters are always placed into empty squares, never on filled ones.</li> <li>The game ends when there are <strong>three</strong> of the same (non-empty) character filling any row, column, or diagonal.</li> <li>The game also ends if all squares are non-empty.</li> <li>No more moves can be played if the game is over.</li> </ul> <p>Given a 2D integer array <code>moves</code> where <code>moves[i] = [row<sub>i</sub>, col<sub>i</sub>]</code> indicates that the <code>i<sup>th</sup></code> move will be played on <code>grid[row<sub>i</sub>][col<sub>i</sub>]</code>. return <em>the winner of the game if it exists</em> (<code>A</code> or <code>B</code>). In case the game ends in a draw return <code>&quot;Draw&quot;</code>. If there are still movements to play return <code>&quot;Pending&quot;</code>.</p> <p>You can assume that <code>moves</code> is valid (i.e., it follows the rules of <strong>Tic-Tac-Toe</strong>), the grid is initially empty, and <code>A</code> will play first.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo1-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[2,0],[1,1],[2,1],[2,2]] <strong>Output:</strong> &quot;A&quot; <strong>Explanation:</strong> A wins, they always play first. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo2-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]] <strong>Output:</strong> &quot;B&quot; <strong>Explanation:</strong> B wins. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo3-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]] <strong>Output:</strong> &quot;Draw&quot; <strong>Explanation:</strong> The game ends in a draw since there are no moves to make. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= moves.length &lt;= 9</code></li> <li><code>moves[i].length == 2</code></li> <li><code>0 &lt;= row<sub>i</sub>, col<sub>i</sub> &lt;= 2</code></li> <li>There are no repeated elements on <code>moves</code>.</li> <li><code>moves</code> follow the rules of tic tac toe.</li> </ul>
3
{ "code": "class Board {\nprivate:\nunordered_map<int, int> rows;\nunordered_map<int, int> cols;\nunordered_map<int, int> diags;\n\n/*\n0: left diag\n1: right diag\n2: both diags\n-1: not a diag\n*/\nint getDiag(int row, int col) {\n if (row == 1 && col == 1) return 2;\n if ((row == 0 && col == 0) || (row == 2 && col == 2)) return 0;\n if ((row == 0 && col == 2) || (row == 2 && col == 0)) return 1;\n return -1;\n}\n\npublic:\nBoard() {}\n\nvoid addMove(int row, int col, bool isPlayerA) {\n if (isPlayerA) {\n rows[row]++;\n cols[col]++;\n int diagNum = getDiag(row, col);\n if (diagNum == 2) {\n diags[0]++;\n diags[1]++;\n } else if (diagNum != -1){\n diags[diagNum]++;\n }\n } else {\n rows[row]--;\n cols[col]--;\n int diagNum = getDiag(row, col);\n if (diagNum == 2) {\n diags[0]--;\n diags[1]--;\n } else if (diagNum != -1){\n diags[diagNum]--;\n }\n }\n}\n\nstring checkState() {\n for (auto& r : rows) {\n if (r.second == 3) return \"A\";\n if (r.second == -3) return \"B\";\n }\n for (auto& c : cols) {\n if (c.second == 3) return \"A\";\n if (c.second == -3) return \"B\";\n }\n for (auto& d : diags) {\n if (d.second == 3) return \"A\";\n if (d.second == -3) return \"B\";\n }\n return \"Pending\";\n}\n\n};\n\nclass Solution {\npublic:\n string tictactoe(vector<vector<int>>& moves) {\n Board board;\n bool isA = true;\n for (const auto& move : moves) {\n int row = move[0];\n int col = move[1];\n board.addMove(row, col, isA);\n isA = !isA;\n }\n string state = board.checkState();\n if (state == \"Pending\" && moves.size() == 9) return \"Draw\";\n return state;\n }\n};", "memory": "10700" }
1,400
<p><strong>Tic-tac-toe</strong> is played by two players <code>A</code> and <code>B</code> on a <code>3 x 3</code> grid. The rules of Tic-Tac-Toe are:</p> <ul> <li>Players take turns placing characters into empty squares <code>&#39; &#39;</code>.</li> <li>The first player <code>A</code> always places <code>&#39;X&#39;</code> characters, while the second player <code>B</code> always places <code>&#39;O&#39;</code> characters.</li> <li><code>&#39;X&#39;</code> and <code>&#39;O&#39;</code> characters are always placed into empty squares, never on filled ones.</li> <li>The game ends when there are <strong>three</strong> of the same (non-empty) character filling any row, column, or diagonal.</li> <li>The game also ends if all squares are non-empty.</li> <li>No more moves can be played if the game is over.</li> </ul> <p>Given a 2D integer array <code>moves</code> where <code>moves[i] = [row<sub>i</sub>, col<sub>i</sub>]</code> indicates that the <code>i<sup>th</sup></code> move will be played on <code>grid[row<sub>i</sub>][col<sub>i</sub>]</code>. return <em>the winner of the game if it exists</em> (<code>A</code> or <code>B</code>). In case the game ends in a draw return <code>&quot;Draw&quot;</code>. If there are still movements to play return <code>&quot;Pending&quot;</code>.</p> <p>You can assume that <code>moves</code> is valid (i.e., it follows the rules of <strong>Tic-Tac-Toe</strong>), the grid is initially empty, and <code>A</code> will play first.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo1-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[2,0],[1,1],[2,1],[2,2]] <strong>Output:</strong> &quot;A&quot; <strong>Explanation:</strong> A wins, they always play first. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo2-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]] <strong>Output:</strong> &quot;B&quot; <strong>Explanation:</strong> B wins. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo3-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]] <strong>Output:</strong> &quot;Draw&quot; <strong>Explanation:</strong> The game ends in a draw since there are no moves to make. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= moves.length &lt;= 9</code></li> <li><code>moves[i].length == 2</code></li> <li><code>0 &lt;= row<sub>i</sub>, col<sub>i</sub> &lt;= 2</code></li> <li>There are no repeated elements on <code>moves</code>.</li> <li><code>moves</code> follow the rules of tic tac toe.</li> </ul>
3
{ "code": "class Board {\nprivate:\nunordered_map<int, int> rows;\nunordered_map<int, int> cols;\nunordered_map<int, int> diags;\n\n/*\n0: left diag\n1: right diag\n2: both diags\n-1: not a diag\n*/\nint getDiag(int row, int col) {\n if (row == 1 && col == 1) return 2;\n if ((row == 0 && col == 0) || (row == 2 && col == 2)) return 0;\n if ((row == 0 && col == 2) || (row == 2 && col == 0)) return 1;\n return -1;\n}\n\npublic:\nBoard() {}\n\nvoid addMove(int row, int col, bool isPlayerA) {\n if (isPlayerA) {\n rows[row]++;\n cols[col]++;\n int diagNum = getDiag(row, col);\n if (diagNum == 2) {\n diags[0]++;\n diags[1]++;\n } else if (diagNum != -1){\n diags[diagNum]++;\n }\n } else {\n rows[row]--;\n cols[col]--;\n int diagNum = getDiag(row, col);\n if (diagNum == 2) {\n diags[0]--;\n diags[1]--;\n } else if (diagNum != -1){\n diags[diagNum]--;\n }\n }\n}\n\nstring checkState() {\n for (auto& r : rows) {\n if (r.second == 3) return \"A\";\n if (r.second == -3) return \"B\";\n }\n for (auto& c : cols) {\n if (c.second == 3) return \"A\";\n if (c.second == -3) return \"B\";\n }\n for (auto& d : diags) {\n if (d.second == 3) return \"A\";\n if (d.second == -3) return \"B\";\n }\n return \"Pending\";\n}\n\n};\n\nclass Solution {\npublic:\n string tictactoe(vector<vector<int>>& moves) {\n Board board;\n bool isA = true;\n for (const auto& move : moves) {\n int row = move[0];\n int col = move[1];\n board.addMove(row, col, isA);\n isA = !isA;\n }\n string state = board.checkState();\n if (state == \"Pending\" && moves.size() == 9) return \"Draw\";\n return state;\n }\n};", "memory": "10700" }
1,400
<p><strong>Tic-tac-toe</strong> is played by two players <code>A</code> and <code>B</code> on a <code>3 x 3</code> grid. The rules of Tic-Tac-Toe are:</p> <ul> <li>Players take turns placing characters into empty squares <code>&#39; &#39;</code>.</li> <li>The first player <code>A</code> always places <code>&#39;X&#39;</code> characters, while the second player <code>B</code> always places <code>&#39;O&#39;</code> characters.</li> <li><code>&#39;X&#39;</code> and <code>&#39;O&#39;</code> characters are always placed into empty squares, never on filled ones.</li> <li>The game ends when there are <strong>three</strong> of the same (non-empty) character filling any row, column, or diagonal.</li> <li>The game also ends if all squares are non-empty.</li> <li>No more moves can be played if the game is over.</li> </ul> <p>Given a 2D integer array <code>moves</code> where <code>moves[i] = [row<sub>i</sub>, col<sub>i</sub>]</code> indicates that the <code>i<sup>th</sup></code> move will be played on <code>grid[row<sub>i</sub>][col<sub>i</sub>]</code>. return <em>the winner of the game if it exists</em> (<code>A</code> or <code>B</code>). In case the game ends in a draw return <code>&quot;Draw&quot;</code>. If there are still movements to play return <code>&quot;Pending&quot;</code>.</p> <p>You can assume that <code>moves</code> is valid (i.e., it follows the rules of <strong>Tic-Tac-Toe</strong>), the grid is initially empty, and <code>A</code> will play first.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo1-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[2,0],[1,1],[2,1],[2,2]] <strong>Output:</strong> &quot;A&quot; <strong>Explanation:</strong> A wins, they always play first. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo2-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]] <strong>Output:</strong> &quot;B&quot; <strong>Explanation:</strong> B wins. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/09/22/xo3-grid.jpg" style="width: 244px; height: 245px;" /> <pre> <strong>Input:</strong> moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]] <strong>Output:</strong> &quot;Draw&quot; <strong>Explanation:</strong> The game ends in a draw since there are no moves to make. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= moves.length &lt;= 9</code></li> <li><code>moves[i].length == 2</code></li> <li><code>0 &lt;= row<sub>i</sub>, col<sub>i</sub> &lt;= 2</code></li> <li>There are no repeated elements on <code>moves</code>.</li> <li><code>moves</code> follow the rules of tic tac toe.</li> </ul>
3
{ "code": "class Solution {\npublic:\n enum Type {\n Row0,\n Row1,\n Row2,\n Col0,\n Col1,\n Col2,\n Diag0,\n Diag1,\n };\n\n std::unordered_map<Type, int> playerA;\n std::unordered_map<Type, int> playerB;\n \n std::unordered_map<Type, int>* player = &playerA;\n string tictactoe(vector<vector<int>>& moves) {\n for (const auto& move : moves) {\n const auto row = move[0];\n const auto col = move[1];\n (*player)[static_cast<Type>(Row0 + row)]++;\n (*player)[static_cast<Type>(Col0 + col)]++;\n if (row == col) {\n (*player)[Diag0]++;\n }\n if (row + col == 2) {\n (*player)[Diag1]++;\n }\n\n player = (player == &playerA) ? &playerB : &playerA;\n }\n \n if (playerA[Row0] == 3 || playerA[Row1] == 3 || playerA[Row2] == 3 ||\n playerA[Col0] == 3 || playerA[Col1] == 3 || playerA[Col2] == 3 ||\n playerA[Diag0] == 3 || playerA[Diag1] == 3) {\n return \"A\";\n } else if (playerB[Row0] == 3 || playerB[Row1] == 3 || playerB[Row2] == 3 ||\n playerB[Col0] == 3 || playerB[Col1] == 3 || playerB[Col2] == 3 ||\n playerB[Diag0] == 3 || playerB[Diag1] == 3) {\n return \"B\";\n } else if (moves.size() == 9) {\n return \"Draw\";\n } else {\n return \"Pending\";\n }\n }\n};\n\n\n// 0 1 2\n// 1 2 3\n// 2 3 4\n\n// 0 -1 -2\n// 1 0 -1\n// 2 1 0", "memory": "10800" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool isVowel(char s){\n return s=='A' || s=='E' || s=='I' || s=='O' || s=='U' || s=='a' || s=='e' || s=='i' || s=='o' ||s=='u';\n }\n\n int find(vector<int>& a){\n for(int i=0;i<a.size();i++){\n if(a[i]>0){\n return i;\n }\n }\n\n return -1;\n }\n string sortVowels(string s) {\n vector<int> f(10);\n\n for(int i=0;i<s.length();i++){\n if(s[i]=='A'){\n f[0]++;\n }\n if(s[i]=='E'){\n f[1]++;\n }\n if(s[i]=='I'){\n f[2]++;\n }\n if(s[i]=='O'){\n f[3]++;\n }\n if(s[i]=='U'){\n f[4]++;\n }\n if(s[i]=='a'){\n f[5]++;\n }\n if(s[i]=='e'){\n f[6]++;\n }\n if(s[i]=='i'){\n f[7]++;\n }\n if(s[i]=='o'){\n f[8]++;\n }\n if(s[i]=='u'){\n f[9]++;\n }\n }\n\n int rowIndex = -1;\n\n for(int i=0;i<s.length();i++){\n if(isVowel(s[i])){\n int rowIndex = find(f);\n if(rowIndex == 0){\n s[i] = 'A';\n }\n if(rowIndex == 1){\n s[i] = 'E';\n }\n if(rowIndex == 2){\n s[i] = 'I';\n } \n if(rowIndex == 3){\n s[i] = 'O';\n }\n if(rowIndex == 4){\n s[i] = 'U';\n }\n if(rowIndex == 5){\n s[i] = 'a';\n }\n if(rowIndex == 6){\n s[i] = 'e';\n }\n if(rowIndex == 7){\n s[i] = 'i';\n }\n if(rowIndex == 8){\n s[i] = 'o';\n }\n if(rowIndex == 9){\n s[i] = 'u';\n }\n \n \n f[rowIndex]--;\n }\n }\n\n return s;\n }\n};", "memory": "13500" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n string sortVowels(string s) {\n const auto is_vowel = [](auto c) {\n return c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'\n || c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';\n };\n int freq[10];\n for (auto c: s)\n switch (c) {\n case 'A' : ++freq[0]; break;\n case 'E' : ++freq[1]; break;\n case 'I' : ++freq[2]; break;\n case 'O' : ++freq[3]; break;\n case 'U' : ++freq[4]; break;\n case 'a' : ++freq[5]; break;\n case 'e' : ++freq[6]; break;\n case 'i' : ++freq[7]; break;\n case 'o' : ++freq[8]; break;\n case 'u' : ++freq[9]; break;\n }\n int i = 0;\n for (auto& c: s) {\n while (i < 10 && !freq[i])\n ++i;\n if (i == 10)\n break;\n if (is_vowel(c)) {\n switch (i) {\n case 0: c = 'A'; break;\n case 1: c = 'E'; break;\n case 2: c = 'I'; break;\n case 3: c = 'O'; break;\n case 4: c = 'U'; break;\n case 5: c = 'a'; break;\n case 6: c = 'e'; break;\n case 7: c = 'i'; break;\n case 8: c = 'o'; break;\n case 9: c = 'u'; break;\n }\n --freq[i];\n }\n }\n return s;\n }\n};", "memory": "13700" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
0
{ "code": "class Solution{\n string sortedVowels = \"AEIOUaeiou\";\n\n inline int sortedVowelsIdx(const char& ch){\n return sortedVowels.find(ch);\n }\n\npublic:\n string sortVowels(string str){\n vector<int> vowelsCount(10);\n for(char& ch:str){\n int idx = sortedVowelsIdx(ch);\n if(idx != -1)\n ++vowelsCount[idx];\n }\n\n int k = 0;\n for(char& ch:str){\n int idx = sortedVowelsIdx(ch);\n\n if(idx != -1){\n while(vowelsCount[k] == 0)\n ++k;\n \n ch = sortedVowels[k];\n --vowelsCount[k];\n }\n }\n\n return str;\n }\n};", "memory": "13800" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool isVowel(char letter) {\n return letter == 'a' ||\n letter == 'e' ||\n letter == 'i' ||\n letter == 'o' ||\n letter == 'u' ||\n letter == 'A' ||\n letter == 'E' ||\n letter == 'I' ||\n letter == 'O' ||\n letter == 'U';\n }\n std::string sortVowels(const std::string& s) {\n std::string vowels;\n vowels.reserve(s.size());\n\n for (char letter : s) {\n if (isVowel(letter)) {\n vowels.push_back(letter);\n }\n }\n\n sort(vowels.begin(), vowels.end());\n\n std::string result;\n result.reserve(s.size());\n\n int vowelIndex = 0;\n for (char letter : s) {\n if (isVowel(letter)) {\n result.push_back(vowels[vowelIndex]);\n ++vowelIndex;\n } else {\n result.push_back(letter);\n }\n }\n\n return result;\n }\n};", "memory": "14000" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n string sortVowels(string s) {\n vector<int> arr(10, 0);\n vector<char> v = {'A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u'};\n for(int i = 0; i < s.size(); i++) {\n if(s[i] == 65) {\n arr[0]++;\n s[i] = '.';\n }\n else if(s[i] == 69) {\n arr[1]++;\n s[i] = '.';\n }\n else if(s[i] == 73){\n arr[2]++;\n s[i] = '.';\n }\n else if(s[i] == 79) {\n arr[3]++;\n s[i] = '.';\n }\n else if(s[i] == 85) {\n arr[4]++;\n s[i] = '.';\n }\n else if(s[i] == 97){\n arr[5]++;\n s[i] = '.';\n }\n else if(s[i] == 101) {\n arr[6]++;\n s[i] = '.';\n }\n else if(s[i] == 105){\n arr[7]++;\n s[i] = '.';\n }\n else if(s[i] == 111) {\n arr[8]++;\n s[i] = '.';\n }\n else if(s[i] == 117) {\n arr[9]++;\n s[i] = '.';\n }\n }\n int k = 0;\n for(int i = 0; i < s.size(); i++) {\n if(s[i] == '.') {\n for(int j = k; j < 10; j++) {\n if(arr[j] > 0) {\n s[i] = v[j];\n arr[j]--;\n break;\n }\n }\n }\n }\n return s;\n }\n};", "memory": "14100" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
0
{ "code": "class Solution {\n inline bool isvowel(char c) {\n c = tolower(c);\n return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';\n }\n\npublic:\n string sortVowels(string s) {\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n int daa[10] = {0};\n string order = \"AEIOUaeiou\";\n int curr = 0;\n\n for (auto c : s) {\n if (isvowel(c)) {\n int index = order.find(c);\n daa[index]++;\n }\n }\n\n for(int i = 0; i < s.size(); i++) {\n if (isvowel(s[i])) {\n while(daa[curr] <= 0) {\n curr++;\n }\n\n s[i] = order[curr];\n daa[curr]--;\n }\n }\n\n return s;\n }\n};", "memory": "14200" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool isVowel(char c) {\n return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || \n c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U';\n }\n string sortVowels(string s) {\n string t;\n t.reserve(s.size());\n for (const char c : s) {\n if (isVowel(c)) {\n t.push_back(c);\n }\n }\n sort(t.begin(), t.end());\n int i = 0;\n for (char& c : s) {\n if (isVowel(c)) {\n c = t[i];\n ++i;\n }\n }\n return s;\n }\n};", "memory": "14300" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n string sortVowels(string s) {\n int n=s.size();\n vector<int> hash(58,0);\n for(int i=0;i<n;i++){\n int ch=s[i];\n if(ch == 'a' ||ch == 'e' ||ch == 'i' ||ch == 'o' ||ch == 'u' ||ch == 'A' ||ch == 'E' ||ch == 'I' ||ch == 'O' ||ch == 'U'){\n hash[s[i]-'A']++;\n }\n }\n\n int ind=0;\n for(int i=0;i<n;i++){\n int ch=s[i];\n if(ch == 'a' ||ch == 'e' ||ch == 'i' ||ch == 'o' ||ch == 'u' ||ch == 'A' ||ch == 'E' ||ch == 'I' ||ch == 'O' ||ch == 'U'){\n while(hash[ind] == 0) ind++;\n s[i] = ind+'A';\n hash[ind]--;\n }\n }\n return s;\n }\n};", "memory": "14400" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool isvowel(char &ch){\n return (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U' || ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');\n }\n string sortVowels(string s) {\n map<char, int> m;\n for(int i = 0; i < s.size(); i++){\n if(isvowel(s[i])){\n m[s[i]]++;\n }\n }\n auto iterator = m.begin();\n for(int i = 0; i < s.size(); i++){\n if(isvowel(s[i])){\n if(iterator->second == 0){\n iterator++;\n }\n s[i] = iterator->first;\n iterator->second--;\n }\n }\n \n return s;\n // multiset<int> str;\n // for(int i = 0; i < s.size(); i++){\n // if(isvowel(s[i])){\n // int ascii = s[i];\n // str.insert(ascii);\n // }\n // }\n // auto iterator = str.begin();\n // for(int i = 0; i < s.size(); i++){\n // if(isvowel(s[i])){\n // int ascii = *iterator;\n // s[i] = ascii;\n // iterator++;\n // }\n // }\n\n return s;\n }\n};", "memory": "14500" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n string sortVowels(string s) {\n string vow;\n for(int i=0;i<s.size();i++){\n if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U')\n vow+=s[i];\n }\n sort(vow.begin(),vow.end());\n int j=0;\n for(int i=0;i<s.size();i++){\n if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U')\n s[i]=vow[j++];\n }\n return s;\n }\n};", "memory": "14600" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
0
{ "code": "#include <algorithm>\n#include <cctype> // for tolower\n#include <string>\n\nclass Solution {\npublic:\n bool isVowel(char ch) { // Changed function name from isvovel to isVowel\n char temp = tolower(ch);\n return temp == 'a' || temp == 'e' || temp == 'i' || temp == 'o' || temp == 'u';\n }\n\n string sortVowels(string s) {\n string ans;\n\n int n = s.size();\n \n if (n == 1) return s;\n\n // Collect all vowels\n for (int i = 0; i < n; i++) {\n if (isVowel(s[i])) {\n ans.push_back(s[i]); // Changed psuh_back to push_back\n }\n }\n\n // Sort the collected vowels\n sort(ans.begin(), ans.end());\n\n int j = 0;\n // Replace vowels in the original string with sorted vowels\n for (int i = 0; i < n; i++) {\n if (isVowel(s[i])) {\n s[i] = ans[j++];\n }\n }\n\n return s;\n }\n};\n", "memory": "14700" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n string sortVowels(string s) {\n vector<char> vec;\n for(int i = 0; i < s.length(); i++) {\n if(s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u' ||\n s[i] == 'A' || s[i] == 'E' || s[i] == 'I' || s[i] == 'O' || s[i] == 'U') {\n vec.push_back(s[i]);\n }\n }\n sort(vec.begin(),vec.end());\n int j = 0;\n for(int i = 0; i < s.length(); i++) {\n if(s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u' ||\n s[i] == 'A' || s[i] == 'E' || s[i] == 'I' || s[i] == 'O' || s[i] == 'U') {\n s[i] = vec[j];\n j++;\n }\n }\n \n return s;\n }\n};\n", "memory": "14800" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
0
{ "code": "class Solution {\npublic:\nbool isVowel(char ch){\n if(ch=='A' || ch=='E'|| ch=='I' ||ch=='O'||ch=='U')\n return true;\n if(ch=='a' || ch=='e'|| ch=='i' ||ch=='o'||ch=='u')\n return true;\n return false;\n}\n string sortVowels(string s) {\n vector<char> vowels;\n for(int i=0;i<s.length();i++){\n if(isVowel(s[i])){\n vowels.push_back(s[i]);\n }\n }\n sort(vowels.begin(),vowels.end());\n int index=0;\n for(int i=0;i<s.length();i++){\n if(isVowel(s[i])){\n s[i]=vowels[index++];\n }\n }\n return s;\n }\n};", "memory": "14900" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n string sortVowels(string s) {\n vector<char>v;\n for(auto x:s){\n if(x=='A' || x=='E' || x=='I' || x=='O' || x=='U' || x=='a'|| x=='e' || x=='i'||x=='o' || x=='u'){\n v.push_back(x);\n }\n }\n sort(v.begin(),v.end());\n int j=0;\n for(int i=0;i<s.size();i++){\n if(s[i]=='A' || s[i]=='E' || s[i]=='I' || s[i]=='O' || \n s[i]=='U' || s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || \n s[i]=='u'){\n s[i]=v[j++];\n }\n }\n return s;\n }\n};", "memory": "15000" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n string sortVowels(string s) {\n vector<char> ans;\n for (int i=0;i<s.size();i++){\n if(s[i]=='A'|| s[i]=='E'|| s[i]=='I'|| s[i]=='O'|| s[i]=='U'|| s[i]=='a'|| s[i]=='e'|| s[i]=='i'|| s[i]=='o'|| s[i]=='u'){\n ans.push_back(s[i]);\n }\n }\n int j=0;\n sort(ans.begin(),ans.end());\n\n for (int i=0;i<s.size();i++){\n if(s[i]=='A'|| s[i]=='E'|| s[i]=='I'|| s[i]=='O'|| s[i]=='U'|| s[i]=='a'|| s[i]=='e'|| s[i]=='i'|| s[i]=='o'|| s[i]=='u'){\n s[i]=ans[j];\n j++;\n\n }\n }\n return s;\n \n }\n};", "memory": "15000" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
0
{ "code": "class Solution\n{\npublic:\n string sortVowels(string s)\n {\n vector<char> vowels;\n for (int i = 0; i < s.size(); i++)\n {\n if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u' || s[i] == 'A' || s[i] == 'E' || s[i] == 'I' || s[i] == 'O' || s[i] == 'U')\n {\n vowels.push_back(s[i]);\n }\n }\n sort(vowels.begin(), vowels.end());\n int index = 0;\n for (int i = 0; i < s.size(); i++)\n {\n if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u' || s[i] == 'A' || s[i] == 'E' || s[i] == 'I' || s[i] == 'O' || s[i] == 'U')\n {\n s[i] = vowels[index];\n index++;\n }\n }\n return s;\n }\n};", "memory": "15100" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool isVowel(char ch){\n if(ch=='a'|| ch=='e'|| ch=='i' || ch=='o' || ch=='u'\n || ch=='A'|| ch=='E'|| ch=='I' || ch=='O' || ch=='U'){\n return true;\n }\n\n return false;\n }\n string sortVowels(string s) {\n string t ;\n vector<char>vowels;\n\n for(int i=0;i<s.length();i++){\n if(isVowel(s[i])){\n vowels.push_back(s[i]);\n }\n }\n \n sort(vowels.begin(),vowels.end());\n int ind=0;\n\n for(int i=0;i<s.length();i++){\n if(isVowel(s[i])){\n s[i]= vowels[ind];\n ind++;\n }\n } \n\n \n return s;\n }\n};", "memory": "15200" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool isVowel(char ch) {\n ch = tolower(ch);\n return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');\n }\n \n string sortVowels(string s) {\n unordered_map<char, int> mp;\n \n for(char &ch : s) {\n if(isVowel(ch)) {\n mp[ch]++;\n }\n }\n \n string temp = \"AEIOUaeiou\";\n \n int j = 0; //pointing to temp (current vowel)\n \n for(int i = 0; i<s.length(); i++) {\n \n if(isVowel(s[i])) {\n while(mp[temp[j]] == 0) {\n j++;\n }\n \n s[i] = temp[j];\n mp[temp[j]]--;\n }\n \n }\n \n return s;\n \n }\n};\n", "memory": "15200" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string sortVowels(string s) {\n vector<char> vowels{'a', 'e', 'i', 'o', 'u'};\n vector<char> temp;\n for(const char& c :s){\n char c2 = tolower(c);\n\n for(const char& v: vowels){\n if(c2 == v){\n temp.push_back(c);\n }\n }\n }\n sort(temp.begin(), temp.end());\n int j = 0;\n for(int i =0;i<s.length();i++){\n char c2 = tolower(s[i]);\n for(const char& v: vowels){\n if(c2 == v){\n s[i] = temp[j++];\n break;\n }\n }\n }\n\n return s;\n }\n};", "memory": "15300" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string sortVowels(string s) {\n vector<char>v;\n for(int i=0;i<s.size();i++)\n {\n char p=s[i];\n if(p=='a' || p=='e' || p=='i' || p=='o' || p=='u' || p=='A' || p=='E' || p=='I' || p=='O' || p=='U')\n {\n v.push_back(p);\n }\n }\n sort(v.begin(),v.end());\n string q=\"\";\n int j=0;\n for(int i=0;i<s.size();i++)\n {\n char p=s[i];\n if(p=='a' || p=='e' || p=='i' || p=='o' || p=='u' || p=='A' || p=='E' || p=='I' || p=='O' || p=='U')\n {\n s[i]=v[j];\n j++;\n }\n // else\n // {\n // q=q+s[i];\n // }\n } \n return s;\n }\n};", "memory": "15400" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string sortVowels(string s) {\n string small=\"aeiou\";\n string cap=\"AEIOU\";\n int ind=0;\n string temp=\"\";\n for(int i=0;i<s.size();i++){\n for(auto x:small){\n if(s[i]==x)\n temp.push_back(s[i]); \n }\n for(auto x:cap){\n if(s[i]==x)\n temp.push_back(s[i]);\n }\n \n }\n sort(temp.begin(),temp.end());\n for(int i=0;i<s.size();i++)\n {\n for(int j=0;j<5;j++){\n if(s[i]==small[j])\n {\n s[i]=temp[ind];\n ind++;\n break;\n }\n else if(s[i]==cap[j])\n {\n s[i]=temp[ind];\n ind++;\n break;\n }\n }\n }\n \n return s;\n }\n};", "memory": "15500" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string sortVowels(string s) {\n string ans=\"\";\n vector<char>temp;\n for(auto i : s){\n if(i=='a'|| i=='e'||i=='o' ||i=='u'||i=='i'||i=='A'||i=='E'||i=='O'||i=='I'||i=='U'){\n temp.push_back(i);\n }\n }\n sort(temp.begin(),temp.end());\n for(auto i : temp)\n cout<<i;\n int j=0;\n for(int i=0;i<s.size();i++){\n if(s[i]=='a'|| s[i]=='e'||s[i]=='o' ||s[i]=='u'||s[i]=='i'||s[i]=='A'||s[i]=='E'||s[i]=='O'||s[i]=='I'||s[i]=='U'){\n s[i]=temp[j];\n j++;\n }\n }\n return s;\n }\n};", "memory": "15500" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string sortVowels(string s) {\n string vowel;\n int lower[26];\n int upper[26];\n for(int i = 0;i<26;i++){\n lower[i] = upper[i] = 0;\n }\n for(int i = 0;i<s.size();i++){\n if(s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u'){\n lower[s[i] - 'a']++;\n s[i] = '#';\n }else if(s[i] == 'A' || s[i] == 'E' || s[i] == 'I' || s[i] == 'O' || s[i] == 'U'){\n upper[s[i] - 'A']++;\n s[i] = '#';\n }\n }\n for(int i = 0;i<26;i++){\n if(upper[i] != 0){\n char ch = i+'A';\n while(upper[i]--){\n vowel += ch;\n }\n }\n }\n for(int i = 0;i<26;i++){\n if(lower[i] != 0){\n char ch = i+'a';\n while(lower[i]--){\n vowel += ch;\n }\n }\n }\n int j = 0;\n for(int i = 0;i<s.size();i++){\n if(s[i] == '#'){\n s[i] = vowel[j];\n j++;\n }\n }\n return s;\n }\n};", "memory": "15600" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string sortVowels(string s) {\n int lower[26];\n int upper[26];\n for(int i = 0;i<26;i++){\n lower[i] = upper[i] = 0;\n }\n for(int i = 0;i<s.size();i++){\n if(s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u'){\n lower[s[i]-'a']++;\n s[i] = '#';\n }else if(s[i] == 'A' || s[i] == 'E' || s[i] == 'I' || s[i] == 'O' || s[i] == 'U'){\n upper[s[i]-'A']++;\n s[i] = '#';\n }\n }\n\n string vowels;\n for(int i = 0;i<26;i++){\n char ch = i +'A';\n while(upper[i] != 0){\n vowels += ch;\n upper[i]--;\n }\n }\n for(int i = 0;i<26;i++){\n char ch = i +'a';\n while(lower[i] != 0){\n vowels += ch;\n lower[i]--;\n }\n }\n int idx = 0;\n for(int i = 0;i<s.size();i++){\n if(s[i] == '#'){\n s[i] = vowels[idx];\n idx++;\n }\n }\n return s;\n }\n};", "memory": "15700" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string sortVowels(string s) {\n int arr[58] = {0};\n\n int n = s.size();\n for (int i = 0; i < n; i++) {\n if (s[i] == 'A' || s[i] == 'E' || s[i] == 'I' || s[i] == 'O' ||\n s[i] == 'U' || s[i] == 'a' || s[i] == 'e' || s[i] == 'i' ||\n s[i] == 'o' || s[i] == 'u') {\n arr[s[i] - 'A']++;\n s[i] = '0';\n }\n }\n\n string temp = \"\";\n\n for (int i = 0; i < 58; i++) {\n if (arr[i] > 0) {\n while(arr[i]--){\n temp += 'A' + i;\n }\n }\n }\n\n int index=0;\n for (int i = 0; i < n; i++) {\n if(s[i] == '0'){\n s[i] = temp[index];\n index++;\n }\n }\n\n return s;\n }\n};", "memory": "15800" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string sortVowels(string s) {\n \n\n int lower[26] = {0}; \n int upper[26] = {0};\n for(int i=0;i<s.size();i++){\n if(s[i]=='a' || s[i] =='e'|| s[i]=='i' || s[i]=='o' || s[i]=='u'){\n lower[s[i]-'a']++;\n s[i]='*';\n }\n if(s[i]=='A' || s[i] =='E'|| s[i]=='I' || s[i]=='O' || s[i]=='U'){\n upper[s[i]-'A']++;\n s[i]='*';\n }\n }\n string vowels =\"\";\n for(int i=0;i<26;i++){\n char ch = i+'A';\n while(upper[i]){\n vowels+=ch;\n upper[i]--;\n }\n }\n for(int i=0;i<26;i++){\n char ch = i+'a';\n while(lower[i]){\n vowels+=ch;\n lower[i]--;\n }\n }\n int j=0;\n int i =0;\n while(j<vowels.size()&& i<s.size()){\n if(s[i]=='*'){\n s[i]= vowels[j];\n j++;\n }\n i++;\n }\n return s;\n\n\n \n\n }\n};", "memory": "15800" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string sortVowels(string s) {\n string check =\"AEIOU\";\n\n int lower[26] = {0}; \n int upper[26] = {0};\n for(int i=0;i<s.size();i++){\n if(s[i]=='a' || s[i] =='e'|| s[i]=='i' || s[i]=='o' || s[i]=='u'){\n lower[s[i]-'a']++;\n s[i]='*';\n }\n if(s[i]=='A' || s[i] =='E'|| s[i]=='I' || s[i]=='O' || s[i]=='U'){\n upper[s[i]-'A']++;\n s[i]='*';\n }\n }\n string vowels =\"\";\n for(int i=0;i<26;i++){\n char ch = i+'A';\n while(upper[i]){\n vowels+=ch;\n upper[i]--;\n }\n }\n for(int i=0;i<26;i++){\n char ch = i+'a';\n while(lower[i]){\n vowels+=ch;\n lower[i]--;\n }\n }\n int j=0;\n int i =0;\n while(j<vowels.size()&& i<s.size()){\n if(s[i]=='*'){\n s[i]= vowels[j];\n j++;\n }\n i++;\n }\n return s;\n\n\n \n\n }\n};", "memory": "15900" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string sortVowels(string s) {\n string check =\"AEIOU\";\n\n int lower[26] = {0}; \n int upper[26] = {0};\n for(int i=0;i<s.size();i++){\n if(s[i]=='a' || s[i] =='e'|| s[i]=='i' || s[i]=='o' || s[i]=='u'){\n lower[s[i]-'a']++;\n s[i]='*';\n }\n if(s[i]=='A' || s[i] =='E'|| s[i]=='I' || s[i]=='O' || s[i]=='U'){\n upper[s[i]-'A']++;\n s[i]='*';\n }\n }\n string vowels =\"\";\n for(int i=0;i<26;i++){\n char ch = i+'A';\n while(upper[i]){\n vowels+=ch;\n upper[i]--;\n }\n }\n for(int i=0;i<26;i++){\n char ch = i+'a';\n while(lower[i]){\n vowels+=ch;\n lower[i]--;\n }\n }\n int j=0;\n int i =0;\n while(j<vowels.size()&& i<s.size()){\n if(s[i]=='*'){\n s[i]= vowels[j];\n j++;\n }\n i++;\n }\n return s;\n\n\n \n\n }\n};", "memory": "16000" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string sortVowels(string s) {\n vector<int>lower(26,0);\n vector<int>upper(26,0);\n int vowelcount=0;\n string temp=\"\";\n int j=0;\n for(int i=0;i<s.size();i++){\n if(s[i]=='a'||s[i]=='e'||s[i]=='o'||s[i]=='u'||s[i]=='i'){\n lower[s[i]-'a']++;\n s[i]='*';\n vowelcount++;\n }\n else if(s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U'){\n upper[s[i]-'A']++;\n s[i]='*'; \n vowelcount++;\n }\n }\n temp.reserve(vowelcount);\n \n for(int i=0;i<26;i++)\n {\n if(upper[i]>0){\n \n temp.append(upper[i], 'A' + i);\n }\n }\n for(int i=0;i<26;i++){\n if(lower[i]>0){\n temp.append(lower[i], 'a' + i);\n }\n }\n for(int i=0;i<s.size();i++){\n if(s[i]=='*'){\n s[i]=temp[j++];\n }\n \n }\n return s;\n }\n};", "memory": "16100" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n // Returns true if the character is a vowel.\n bool isVowel(char c) {\n return c == 'a' || c == 'e' || c == 'o'|| c == 'u'|| c == 'i' \n || c == 'A' || c == 'E' || c == 'O'|| c == 'U'|| c == 'I';\n }\n\n string sortVowels(string s) {\n string temp;\n\n // Store the vowels in the temporary string.\n for (char c : s) {\n if (isVowel(c)) {\n temp += c;\n }\n }\n \n // Sort the temporary string characters in ascending order.\n sort(temp.begin(), temp.end());\n\n int j = 0;\n string ans;\n for (int i = 0; i < s.size(); i++) {\n // If the character is a vowel, replace it with the character in the string temp.\n if (isVowel(s[i])) {\n ans += temp[j];\n j++;\n } else {\n ans += s[i];\n }\n }\n \n return ans;\n }\n};", "memory": "16200" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string sortVowels(string s) {\n\n vector<int> lower(26, 0);\n vector<int> upper(26, 0);\n\n for (int i = 0; i < s.size(); i++) {\n if (s[i] == 'a' || s[i] =='e' ||s[i]=='i' || s[i]=='o' || s[i]=='u')\n {\n lower[s[i] -'a']++;\n s[i]='#';\n }\n else if (s[i] == 'A' || s[i] =='E' ||s[i]=='I' || s[i]=='O' || s[i]=='U')\n {\n upper[s[i] -'A']++;\n s[i]='#';\n }\n }\n string ans;\n for(int i=0;i<26;i++)\n {\n char c ='A'+i;\n while(upper[i])\n {\n ans+=c;\n upper[i]--;\n }\n }\n for(int i=0;i<26;i++)\n {\n char c ='a'+i;\n while(lower[i])\n {\n ans+=c;\n lower[i]--;\n }\n }\n\n int first =0,second =0;\n while(second < ans.size())\n {\n if(s[first] == '#')\n {\n s[first] = ans[second];\n second++;\n }\n first++;\n }\n return s;\n\n }\n};\n", "memory": "16300" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string sortVowels(string s) {\n vector<int> lower(26, 0);\n vector<int> upper(26, 0);\n for(int i = 0; i < s.size(); i++)\n {\n if(s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u')\n {\n lower[s[i] - 'a']++;\n s[i] = '#';\n }\n else if(s[i] == 'A' || s[i] == 'E' || s[i] == 'I' || s[i] == 'O' || s[i] == 'U')\n {\n upper[s[i] - 'A']++;\n s[i] = '#';\n }\n }\n string ans;\n for(int i = 0; i < 26; i++)\n {\n char c = 'A' + i;\n while(upper[i])\n {\n ans += c;\n upper[i]--;\n }\n }\n for(int i = 0; i < 26; i++)\n {\n char c = 'a' + i;\n while(lower[i])\n {\n ans += c;\n lower[i]--;\n }\n }\n int first = 0, second = 0;\n while(second < ans.size())\n {\n if(s[first] == '#')\n {\n s[first] = ans[second];\n second++;\n }\n first++;\n }\n return s;\n }\n};", "memory": "16300" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string sortVowels(string s) {\n vector<int>lower(26,0);\n vector<int>upper(26,0);\n string ans;\n\n for(int i=0;i<s.size();i++){\n // wasey first upper then lower\n if(s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U'){\n upper[s[i]-'A']++;\n s[i]='*';\n }\n else if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'){\n lower[s[i]-'a']++;\n s[i]='*';\n }\n }\n for(int i=0;i<26;i++){\n char c='A'+i;\n while(upper[i]){\n ans+=c;\n upper[i]--;\n }\n }\n for(int i=0;i<26;i++){\n char c='a'+i;\n while(lower[i]){\n ans+=c;\n lower[i]--;\n }\n }\n int i=0,j=0;\n while(j<ans.size()){\n if(s[i]=='*'){\n s[i]=ans[j];\n j++;\n }\n i++;\n }\n return s;\n \n }\n};", "memory": "16400" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string sortVowels(string s) {\n vector<int>lower(26,0);\n vector<int>upper(26,0);\n string ans;\n\n for(int i=0;i<s.size();i++)\n {\n if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u')\n {\n lower[s[i]-'a']++;\n s[i]='#';\n }\n else if(s[i]=='A' || s[i]=='E' || s[i]=='I' || s[i]=='O' || s[i]=='U')\n {\n upper[s[i]-'A']++;\n s[i]='#';\n }\n }\n \n for(int i=0;i<26;i++)\n {\n char c='A'+i;\n while(upper[i])\n {\n ans+=c;\n upper[i]--;\n }\n }\n for(int i=0;i<26;i++)\n {\n char c='a'+i;\n while(lower[i])\n {\n ans+=c;\n lower[i]--;\n }\n }\nint k=0;\n for(int i=0;i<s.size();i++)\n {\n if(s[i]=='#')\n {\n s[i]=ans[k];\n k++;\n }\n }\nreturn s;\n\n\n \n }\n};", "memory": "16500" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string sortVowels(string s) {\n vector<int> upper(26, 0);\n vector<int> lower(26, 0);\n\n for(int i=0; i<s.size(); i++){\n char c = s[i];\n //uppercase walo ko vector me dalo\n if(c=='A' || c=='E' || c=='I' || c=='O' || c=='U'){\n upper[c-'A']++;\n s[i] = '#';\n }\n //lowercase walo ko vector me dalo\n else if(c=='a' || c=='e' || c=='i' || c=='o' || c=='u'){\n lower[c-'a']++;\n s[i] = '#';\n }\n }\n string temp;\n //Pahle uppercase walo ko temp me add kardo(ASCII value jyada hai isliye)\n for(int i=0; i<26; i++){\n char c = 'A'+i;\n while(upper[i]){ //jis index par value>0 (means jis vowel ke liye) hai usi ke liye run hoga,\n temp += c;\n upper[i]--;\n }\n }\n //ab lowercase walo ko temp me add karo\n for(int i=0; i<26; i++){\n char c = 'a'+i;\n while(lower[i]){ //jis index par value>0 (means jis vowel ke liye) hai usi ke liye run hoga,\n temp += c;\n lower[i]--;\n }\n }\n //update original string\n int it = 0;\n while(it < temp.size()){\n for(int i=0; i<s.size(); i++){\n if(s[i]=='#'){\n s[i] = temp[it];\n it++;\n }\n }\n }\n return s;\n }\n};", "memory": "16500" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string sortVowels(string s) {\n vector<int> lower(26,0);\n vector<int> upper(26,0);\n for(int i=0;i<s.size();i++){\n if(s[i]=='a'||s[i]=='e'|| s[i]=='i'|| s[i]=='o'|| s[i]=='u'){\n lower[s[i]-'a']++;\n s[i]='#';\n }\n else if(s[i]=='A'||s[i]=='E'|| s[i]=='I'|| s[i]=='O'|| s[i]=='U'){\n upper[s[i]-'A']++;\n s[i]='#';\n }\n }\n string vowel;\n for(int i=0;i<26;i++){\n while(upper[i]){\n char c= i+'A';\n vowel+=c;\n upper[i]--;\n }\n }\n for(int i=0;i<26;i++){\n while(lower[i]){\n char c= i+'a';\n vowel+=c;\n lower[i]--;\n }\n }\n int t=0;\n for(int i=0;i<s.size();i++){\n if(s[i]=='#'){\n s[i]=vowel[t];\n t++;\n }\n }\n return s;\n }\n};", "memory": "16600" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string sortVowels(string s) {\n vector<int>lower(26,0);\n vector<int>upper(26,0);\n for(int i=0;i<s.size();i++){\n if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'){\n lower[s[i]-'a']++;\n s[i]='#';\n }\n else if(s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U'){\n upper[s[i]-'A']++;\n s[i]='#';\n }\n }\n string ans=\"\";\n for(int i=0;i<26;i++){\n char c='A'+i;\n while(upper[i]){\n ans+=c;\n upper[i]--;\n }\n }\n\n for(int i=0;i<26;i++){\n char c='a'+i;\n while(lower[i]){\n ans+=c;\n lower[i]--;\n }\n }\n\n int first=0;\n int second =0;\n\n while(second<ans.size()){\n if(s[first]=='#'){\n s[first]=ans[second];\n second++;\n }\n first++;\n }\n\n\n\n return s;\n\n \n }\n};", "memory": "16600" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string sortVowels(string s) {\n vector<int>lower(26,0);\n vector<int>upper(26,0);\n for(int i=0;i<s.size();i++){\n if(s[i]=='a'|| s[i]=='e' || s[i]=='i' ||s[i]=='o' ||s[i]=='u'){\n lower[s[i]-'a']++;\n s[i]='#';\n }\n else if(s[i]=='A'|| s[i]=='E' || s[i]=='I' ||s[i]=='O' ||s[i]=='U'){\n upper[s[i]-'A']++;\n s[i]='#';\n }\n\n \n }\n string vowel=\"\";\n for(int i=0;i<26;i++){\n char c='A'+i;\n while(upper[i]){\n vowel+=c;\n upper[i]--;\n }\n }\n for(int i=0;i<26;i++){\n char c='a'+i;\n while(lower[i]){\n vowel+=c;\n lower[i]--;\n }\n }\n cout<<vowel;\n int first=0,second=0;\n while(second<vowel.size()){\n if(s[first]=='#'){\n s[first]=vowel[second];\n second++;\n }\n first++;\n }\n return s;\n\n\n \n }\n};", "memory": "16700" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string sortVowels(string s) {\n vector<int> lower(26,0);\n vector<int> upper(26,0);\n\n for(int i=0;i<s.size();i++){\n if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'){\n lower[s[i]-'a']++;\n s[i]='#';\n }\n else if(s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U'){\n upper[s[i]-'A']++;\n s[i]='#';\n }\n\n }\n string vowel;\n for(int i=0;i<26;i++){\n char c='A'+i;\n while(upper[i]){\n vowel+=c;\n upper[i]--;\n }\n }\n for(int i=0;i<26;i++){\n char c='a'+i;\n while(lower[i]){\n vowel+=c;\n lower[i]--;\n }\n }\n\n int first=0;\n int second=0;\n while(second<vowel.size()){\n if(s[first]=='#'){\n s[first]=vowel[second];\n second++;\n }\n first++;\n }\n return s;\n }\n};", "memory": "16700" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string sortVowels(string s) {\n string vowels;\n for (auto it : s) {\n if (it == 'a' || it == 'e' || it == 'i' || it == 'o' || it == 'u' ||\n it == 'A' || it == 'E' || it == 'I' || it == 'O' || it == 'U') {\n vowels.push_back(it);\n }\n }\n sort(vowels.rbegin(), vowels.rend());\n for (int i = 0; i <= s.size() - 1; i++) {\n if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' ||\n s[i] == 'u' || s[i] == 'A' || s[i] == 'E' || s[i] == 'I' ||\n s[i] == 'O' || s[i] == 'U') {\n s[i] = vowels.back();\n vowels.pop_back();\n }\n }\n return s;\n }\n};", "memory": "16800" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string sortVowels(string s) \n {\n vector<int>lower(26,0),upper(26,0);\n string temp=\"\";\n for(int i=0;i<s.size();i++)\n {\n if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u')\n lower[s[i]-'a']++;\n else if( s[i]=='A' || s[i]=='E' || s[i]=='I' || s[i]=='O' || s[i]=='U')\n upper[s[i]-'A']++;\n }\n\n for(int i=0;i<26;i++)\n {\n char c='A'+i;\n while(upper[i]--)\n temp+=c;\n }\n\n for(int i=0;i<26;i++)\n {\n char c='a'+i;\n while(lower[i]--)\n temp+=c;\n }\n \n int j=0;\n for(int i=0;i<s.size();i++)\n {\n if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u' || s[i]=='A' || s[i]=='E' || s[i]=='I' || s[i]=='O' || s[i]=='U')\n s[i]=temp[j++];\n }\n return s;\n }\n};", "memory": "16800" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string sortVowels(string s) {\n string help=\"\";\n string ans=\"\";\n vector<int> lower(26,0);\n vector<int> upper(26,0);\n for(int i=0;i<s.length();i++)\n {\n if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u')\n {\n lower[s[i]-'a']++;\n s[i]='#';\n }\n else if(s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U')\n {\n upper[s[i]-'A']++;\n s[i]='#';\n }\n }\n for(int i=0;i<26;i++)\n {\n char c='A'+i;\n while(upper[i])\n {\n ans+=c;\n upper[i]--;\n }\n }\n for(int i=0;i<26;i++)\n {\n char c='a'+i;\n while(lower[i])\n {\n ans+=c;\n lower[i]--;\n }\n }\n int second=0;\n int first=0;\n while(second<ans.length())\n {\n if(s[first]=='#')\n {\n s[first]=ans[second];\n second++;\n }\n first++;\n }\n return s;\n }\n};", "memory": "16900" }
2,887
<p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p> <ul> <li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 &lt;= i &lt; s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li> <li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 &lt;= i &lt; j &lt; s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li> </ul> <p>Return <em>the resulting string</em>.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;lEetcOde&quot; <strong>Output:</strong> &quot;lEOtcede&quot; <strong>Explanation:</strong> &#39;E&#39;, &#39;O&#39;, and &#39;e&#39; are the vowels in s; &#39;l&#39;, &#39;t&#39;, &#39;c&#39;, and &#39;d&#39; are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;lYmpH&quot; <strong>Output:</strong> &quot;lYmpH&quot; <strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return &quot;lYmpH&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists only of letters of the&nbsp;English alphabet&nbsp;in <strong>uppercase and lowercase</strong>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string sortVowels(string s) {\n string help=\"\";\n string ans=\"\";\n vector<int> lower(26,0);\n vector<int> upper(26,0);\n for(int i=0;i<s.length();i++)\n {\n if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u')\n {\n ans+=s[i];\n s[i]='#';\n }\n else if(s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U')\n {\n ans+=s[i]; \n s[i]='#';\n }\n }\n int second=0;\n int first=0;\n sort(ans.begin(),ans.end());\n while(second<ans.length())\n {\n if(s[first]=='#')\n {\n s[first]=ans[second];\n second++;\n }\n first++;\n }\n return s;\n }\n};", "memory": "16900" }