id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices... | 3 | {
"code": "class Solution {\n int findMiniDel(vector<int> &a_count, vector<int> &b_sum, string &reduced_s, int a_pos, int b_pos) {\n if(a_pos<0 || b_pos<0) {\n return 0;\n }\n // findMiniDel(a_count, b_sum, string &reduced_s, pos-1)\n int delete_a = a_count[a_pos] + findMiniD... |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices... | 3 | {
"code": "class Solution {\n int findMiniDel(vector<int> &a_count, vector<int> &b_sum, string &reduced_s, int a_pos, int b_pos) {\n if(a_pos<0 || b_pos<0) {\n return 0;\n }\n // findMiniDel(a_count, b_sum, string &reduced_s, pos-1)\n int delete_a = a_count[a_pos] + findMiniD... |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices... | 3 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n vector<int> compressed;\n int cnt = 0;\n int i = 0;\n while(i<s.size()){\n int left = i;\n ++i;\n while(i<s.size()&&s[i]==s[i-1]){\n ++i;\n }\n ... |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices... | 3 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n vector<int> v;\n char c = s.front();\n int cnt = 0;\n for (char ch : s) {\n if (ch == c) ++cnt;\n else {\n c = ch;\n v.push_back(cnt);\n cnt = 1;\... |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices... | 3 | {
"code": "class Solution \n{\n vector<pair<int,int>> preprocess(string const & s)\n {\n vector<pair<int,int>> excludes;\n int idx = 0;\n while (idx<s.size() && s[idx]=='a')\n {\n ++idx;\n }\n pair<int,int> exclude;\n while(idx<s.size())\n {\n ... |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices... | 3 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n vector<int> cost;\n\n int bCount = s[0] == 'b';\n bool seenA = false;\n for (int i = 1; i < s.size() - 1; ++i)\n {\n seenA = seenA || s[i] == 'a';\n\n if (s[i] != s[i + 1] || s[i] != s... |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices... | 3 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n // count (aa..a...bb..b) set\n vector<int> num_a, num_b;\n vector<pair<int, int>> dp;\n int i = 0;\n int n = s.length();\n\n while(i < n){\n int a = 0;\n int b = 0;\n ... |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices... | 3 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n int n = s.size();\n vector<pair<int, int>> vec(n);\n vec[0].first = 0;\n for(int i = 1; i < n; i++) {\n if(s[i-1] == 'b') vec[i].first = vec[i-1].first + 1;\n else vec[i].first = vec[i-1].fir... |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices... | 3 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n int n = s.size();\n vector<pair<int, int>> counts(n + 1, {0, 0});\n int count = 0, ans = INT_MAX;\n\n for(int i = 0; i < n; ++i) {\n if(s[i] == 'b') {\n ++count;\n }\n\n ... |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices... | 3 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n int n = s.size();\n if(n==1) {\n return 0;\n }\n\n vector<pair<int,int>>dp(n, pair<int,int>{0,0});\n\n int countA = 0, countB = 0;\n char a = 'a', b = 'b';\n\n for(int i=0;i<n;i++) ... |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices... | 3 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string a) {\n int n=a.size(),i,ans=n,x,y;\n vector<int> pre(n),post(n);\n pre[0]=(a[0]=='b'?1:0);\n post[n-1]=(a[n-1]=='a'?1:0);\n for(i=1;i<n;i++)\n pre[i]=pre[i-1]+(a[i]=='b'?1:0);\n for(i=n-2;i>=0;i--)\n... |
1,756 | <p>You are given a string <code>s</code> consisting only of characters <code>'a'</code> and <code>'b'</code>ββββ.</p>
<p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices... | 3 | {
"code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n vector<int> prefix_b(s.size() + 2, 0), suffix_a(s.size() + 2, 0);\n prefix_b[0] = 0;\n suffix_a[suffix_a.size() - 1] = 0;\n\n for(int i = 1; i < prefix_b.size() - 1; i++) {\n if(s[i - 1] == 'b') {\n ... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 0 | {
"code": "class Solution {\npublic:\n\tstd::array<int, 26> getLetterCount(const string &word)\n\t{\n\t\tstd::array<int, 26> arr = { 0 };\n\n\t\tfor (std::size_t i = 0; i < word.size(); ++i)\n\t\t{\n\t\t\tarr[static_cast<char>(word[i]) - 97] ++;\n\t\t}\n\n\t\treturn arr;\n\t}\n\n\tbool closeStrings(const string &word... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 0 | {
"code": "class Solution {\npublic:\n\tstd::array<int, 26> getLetterCount(const string &word)\n\t{\n\t\tstd::array<int, 26> arr = { 0 };\n\n\t\tfor (std::size_t i = 0; i < word.size(); ++i)\n\t\t{\n\t\t\tarr[static_cast<char>(word[i]) - 97] ++;\n\t\t}\n\n\t\treturn arr;\n\t}\n\n\tbool closeStrings(const string &word... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 0 | {
"code": "class Solution {\npublic:\n Solution() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n }\n\n bool closeStrings(const std::string& word1, const std::string& word2) {\n if (word1.size() != word2.size())\n return false;\n\n\n std::vector<int> s1... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 0 | {
"code": "class Solution {\npublic:\n bool closeStrings(string& word1, string& word2) \n {\n if (word1.size() != word2.size())\n return false;\n \n vector<int> map1(26, 0);\n vector<int> map2(26, 0);\n\n unordered_set<char> word1_set;\n unordered_set<char> w... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 0 | {
"code": "class Solution {\npublic:\n\n unordered_map<int, int> get_histo(const string& s) const\n {\n unordered_map<int, int> m;\n\n for (const char c : s)\n {\n ++m[c];\n }\n\n return m;\n }\n bool closeStrings(string& word1, const string& word2) {\n ... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 0 | {
"code": "#include <vector>\n#include <unordered_map>\n#include <algorithm>\n\nclass Solution {\npublic:\n bool closeStrings(const std::string& word1, const std::string& word2) {\n if (word1.length() != word2.length()) {\n return false;\n }\n\n std::unordered_map<char, int> freq1, ... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 0 | {
"code": "#include <vector>\n#include <algorithm>\n#include <string>\n#include <unordered_map>\n\nclass Solution {\npublic:\n bool closeStrings(const std::string& word1, const std::string& word2) {\n // If lengths are different, they can't be close\n if (word1.size() != word2.size()) return false;\n... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 0 | {
"code": "class Solution {\npublic:\n\n map<int, int> get_histo(const string& s) const\n {\n map<int, int> m;\n\n for (const char c : s)\n {\n ++m[c];\n }\n\n return m;\n }\n bool closeStrings(string& word1, const string& word2) {\n // we can only work... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 0 | {
"code": "class Solution {\npublic:\n bool closeStrings(string& word1, string& word2) {\n if(word1.size() != word2.size())\n return false;\n sort(word1.begin(), word1.end());\n sort(word2.begin(), word2.end());\n if(word1 == word2) return true;\n unordered_map<char, i... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 0 | {
"code": "class Solution {\npublic:\n bool closeStrings(const std::string& word1, const std::string& word2) {\n if (word1.size() != word2.size()) {\n return false;\n }\n\n std::unordered_map<char, int> map1, map2;\n std::unordered_set<char> set1, set2;\n\n // Populate... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 0 | {
"code": "class Solution {\npublic:\n bool closeStrings(const std::string& word1, const std::string& word2) {\n if (word1.size() != word2.size()) {\n return false;\n }\n\n std::unordered_map<char, int> map1, map2;\n std::unordered_set<char> set1, set2;\n\n // Populate... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 0 | {
"code": "class Solution {\npublic:\n bool closeStrings(string &word1, string &word2) {\n unordered_set<char> w1(word1.begin(), word1.end()), w2(word2.begin(), word2.end());\n if (w1 != w2) return false;\n unordered_map<char, int> mp1, mp2; // {char -> frequency}\n for (char c : word1)... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 0 | {
"code": "class Solution {\npublic:\n bool closeStrings(string &word1, string &word2) {\n unordered_map<char, int> mp1, mp2; // {char -> frequency}\n for (char c : word1) ++mp1[c];\n for (char c : word2) ++mp2[c];\n unordered_multiset<int> freq1, freq2;\n unordered_set<char> w1,... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 0 | {
"code": "class Solution {\npublic:\n bool closeStrings(const std::string& word1, const std::string& word2) {\n // If the lengths are different, they cannot be close\n if (word1.size() != word2.size()) {\n return false;\n }\n\n std::map<char, int> word1Map;\n std::map... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 0 | {
"code": "class Solution {\npublic:\n \n void mergeSort(int low, int mid, int high, vector<int>& vec){\n if(low == high){\n return;\n }\n mergeSort(low, (low+mid)/2, mid, vec);\n mergeSort(mid+1, (mid+1+high)/2, high, vec);\n vector<int> v1(mid-low+1), v2(high - mid);\n int i = 0, j = ... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 0 | {
"code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n // if(word1.length()!=word2.length()) return false;\n // unordered_map<char,char>m1,m2;\n // for(int i=0;i<word1.length();i++){\n // m1[word1[i]]++;\n // m2[word2[i]]++;\n\n\n ... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 0 | {
"code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n int m = word1.length(); \n int n = word2.length(); \n\n \n if(m != n)\n return false;\n\n vector<int> frequency1(26); \n vector<int> frequency2(26); \n\n for(int i = ... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 0 | {
"code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n int n = word1.length();\n int m = word2.length();\n\n vector<int> freq1(26, 0);\n vector<int> freq2(26, 0);\n\n for(auto c: word1){\n freq1[c-'a']++;\n }\n for(auto c... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 0 | {
"code": "class Solution {\npublic:\n bool closeStrings(std::string word1, std::string word2) {\n std::vector<int> freq1(26, 0);\n std::vector<int> freq2(26, 0);\n\n for (char ch : word1) {\n freq1[ch - 'a']++;\n }\n\n for (char ch : word2) {\n freq2[ch - '... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 0 | {
"code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n vector<int> w1(26,0), w2(26,0);\n if(word1.size()!=word2.size()) return false;\n for(char ch: word1)\n w1[ch-'a']++;\n for(char ch: word2)\n w2[ch-'a']++;\n for(int i=0;... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 0 | {
"code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n vector<int> v1(26, 0);\n vector<int> v2(26, 0);\n for(int i=0; i<word1.length(); i++) v1[word1[i]-'a']++;\n for(int i=0; i<word2.length(); i++) v2[word2[i]-'a']++;\n for(int i=0; i<26; i++){\... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 0 | {
"code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n int m = word1.length(); // length of 1st string word1...\n int n = word2.length(); // length of 2nd string word2...\n\n //if both strings aren't of same length then, returns false...\n if(m != n)\n ... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 1 | {
"code": "class Solution {\npublic:\n bool closeStrings(string s1, string s2) {\n int n=s1.size(),m=s2.size();\n vector<char>a(26,0),b(26,0);\n for(int i=0;i<n;i++)\n {\n int x=s1[i]-'a';\n a[x]++;\n }\n for(int i=0;i<m;i++)\n b[s2[i]-'a']++;\n so... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 1 | {
"code": "class Solution {\npublic:\n int a[256];\n int b[256];\n bool closeStrings(string w1, string w2) {\n if (w1.size() != w2.size())\n return false;\n \n for (int i = 0; i < w1.size(); i++) {\n a[(int)w1[i]]++;\n b[(int)w2[i]]++;\n }\n\n f... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 1 | {
"code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n if(word1.length() != word2.length()) return false;\n\n vector<int> cnts1(26);\n vector<int> cnts2(26);\n for(char c : word1) cnts1[c - 'a']++;\n for(char c : word2) cnts2[c - 'a']++;\n\n ... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 1 | {
"code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n if(word1.size()==word2.size()){\n int mp1[256]={0};\n int mp2[256]={0};\n\n set<char>st1;\n set<char>st2;\n\n for(int i=0;i<word1.size();i++){\n mp1[... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 1 | {
"code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n vector<int> f1(26,0),f2=f1;\n unordered_set<int> hs;\n for(char c:word1){\n hs.insert(c);\n f1[c-'a']++;\n }\n for(char c:word2){\n if(hs.find(c)==hs.end()){\... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 1 | {
"code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n int a1[26],a2[26];\n for(int i=0;i<word1.size();i++) a1[word1[i]-'a']++;\n for(int i=0;i<word2.size();i++) a2[word2[i]-'a']++;\n map<int,int> m;\n for(int i=0;i<26;i++) m[a1[i]]++;\n f... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 2 | {
"code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n \n if(word1.size() != word2.size()) return false;\n\n unordered_map<int,int> freq1,freq2;\n for(char c: word1){\n freq1[c]++;\n }\n\n for(char c: word2){\n freq2[c... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 2 | {
"code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2)\n {\n unordered_map<char, int> map1;\n unordered_map<char, int> map2;\n unordered_map<int, int> freq;\n\n //alog:\n //len should be the same\n //both string should have same letters (no... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 2 | {
"code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n if(word1.size()!= word2.size())return 0; \n unordered_map<char, int>map1;\n unordered_map<char, int>map2;\n multiset<int> multiSet1;\n multiset<int> multiSet2;\n for(int i=0; i<word1.size();... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 2 | {
"code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n const int n1 = word1.size();\n const int n2 = word2.size();\n if (n1 != n2)\n return false;\n sort(word1.begin(), word1.end());\n sort(word2.begin(), word2.end());\n if (wor... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 2 | {
"code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n int n1 = word1.size();\n int n2 = word2.size();\n\n if(n1 != n2) return false;\n\n unordered_map<char, int> mp1;\n unordered_map<char, int> mp2;\n\n set<char> st1;\n set<char> s... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 2 | {
"code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n\n if(word1.length()!=word2.length()){\n return false;\n }\n unordered_map<char , int>freq1;\n unordered_map<char , int>freq2;\n for(char ch : word1){\n freq1[ch]++;\n ... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 2 | {
"code": "class Solution {\npublic:\n\n bool closeStrings(string word1, string word2) {\n int n=word1.length();\n int m=word2.length();\n if(n!=m) return false;\n map<char,int> m1,m2;\n vector<int> v1,v2;\n for(int i=0;i<n;i++){\n m1[word1[i]]++;\n }\n ... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 2 | {
"code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n if(word1.size()!=word2.size()) \n return false;\n unordered_map<char,int>mp1;\n unordered_map<char,int>mp2;\n for(auto x:word1)\n {\n mp1[x]++;\n } \n for(auto x:word2)\... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 3 | {
"code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n if(word1.size()!= word2.size())return 0; \n unordered_map<char, int>map1;\n unordered_map<char, int>map2;\n multiset<int> multiSet1;\n multiset<int> multiSet2;\n for(int i=0; i<word1.size();... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 3 | {
"code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n unordered_set<char> s;\n unordered_map<char, int> mp;\n for(int i=0; i<word1.length(); i++){\n s.insert(word1[i]);\n mp[word1[i]]++;\n }\n unordered_map<char, int> mp2;\... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 3 | {
"code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n int n1 = word1.size();\n int n2 = word2.size();\n\n if(n1 != n2) return false;\n\n unordered_map<char, int> mp1;\n unordered_map<char, int> mp2;\n\n set<char> st1;\n set<char> s... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 3 | {
"code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n map<char,int>m;\n map<char,int>m2;\n vector<int>v;\n vector<int>v2;\n set<char>ss;\n for(auto i:word1) ss.insert(i);\n for(auto i:word2) if(ss.count(i)==0) return false;\n\n ... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 3 | {
"code": "#include <ranges>\n\nclass Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n unordered_map<char, int> hist1, hist2;\n for(const char e: word1)\n hist1[e]++;\n\n for(const char e: word2)\n hist2[e]++;\n\n\n auto vit1 = views::values(... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 3 | {
"code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n int n=word1.size();\n int m=word2.size();\n\n if(n!=m){\n return false;\n }\n\n map<char,int>m1;\n map<char,int>m2;\n\n for(int i=0;i<word1.length();i++){\n ... |
1,777 | <p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>
<ul>
<li>Operation 1: Swap any two <strong>existing</strong> characters.
<ul>
<li>For example, <code>a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u></code></li>
</ul>
</li>
<li>Operation ... | 3 | {
"code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n if(word1.size()!=word2.size()){\n return false;\n }\n set<char> words1,words2;\n for(auto c:word1){\n words1.insert(c);\n }\n for(auto c:word2){\n word... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n \n int n = nums.size();\n vector<int> pre(n);\n vector<int> suf(n);\n pre[0] = nums[0];\n suf[n-1] = nums[n-1];\n for(int i = 1;i<n;i++){\n pre[i] = pre[i-1] + nums[i];... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size();\n vector<int> prefix(n, 0), suffix(n, 0);\n prefix[0] = nums[0];\n suffix[n - 1] = nums[n - 1];\n for (int i = 1; i < n; i++)\n prefix[i] = prefix[i - 1] + nums[... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n=nums.size(),sum=0,mini=INT_MAX,i;\n vector<int> prfx(n);\n vector<int> sufx(n);\n for(i=0;i<n;i++){\n sum+=nums[i];prfx[i]=sum;\n }\n sum=0;\n for(i=n-1;i>=0;i--)... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n \n \n int n=nums.size()-1;\n if(x<min(nums[0],nums[n]))return -1;\n \n vector<int>leftr(n+1),rightr(n+1);\n leftr[0]=nums[0],rightr[0]=nums[n];\n for(int i=1;i<=n;i++)leftr... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size();\n vector<int> pstart(n,0),pend(n,0);\n long long sum=0;\n for(int i=0;i<n;i++)\n {\n sum=sum+nums[i];\n pstart[i]=sum;\n }\n sum=0;\n ... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n=nums.size();\n vector<int>suff;\n int sm=0;\n for(int i=n-1;i>=0;i--)\n {\n int add=0;\n if(!suff.empty())add=suff.back();\n suff.push_back(add+nums[i]);\... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n vector<int>ans;\n ans=nums;\n int i,j,l=0,r=0,chk=0,t=2*ans.size()+1;\n for(i=0;i<nums.size();i++){ans.push_back(nums[i]);chk+=nums[i];}\n if(chk<x)return -1;\n chk=0;\n // for(i=... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n vector<int>ans;\n ans=nums;\n int i,j,l=0,r=0,chk=0,t=2*ans.size()+1;\n for(i=0;i<nums.size();i++){ans.push_back(nums[i]);chk+=nums[i];}\n if(chk<x)return -1;\n chk=0;\n // for(i=... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n vector<int>ans;\n ans=nums;\n int i,j,l=0,r=0,chk=0,t=2*ans.size()+1;\n for(i=0;i<nums.size();i++){ans.push_back(nums[i]);chk+=nums[i];}\n if(chk<x)return -1;\n chk=0;\n // for(i=... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size();\n unsigned int cnt = UINT_MAX;\n vector<int> lSum;\n vector<int> rSum;\n\n lSum.push_back(0);\n if (nums[0] <= x) {\n for (int i = 0; i < n; i++) {\n ... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n vector<int>pref;\n int n=nums.size();\n vector<int>suff(n);\n int ans=1e9;\n int sum=0;\n for(int i=0;i<nums.size();i++){\n sum+=nums[i];\n pref.push_back(sum);\n ... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n\n vector<int> nums2 ;\n for (int n: nums) {\n nums2.push_back(n);\n }\n for (int n: nums) {\n nums2.push_back(n);\n }\n int n = nums2.size();\n\n int l = 0;\... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n vector<int>pref;\n vector<int>suff;\n int sum=0;\n for(int i=0;i<nums.size();i++){\n sum=sum+nums[i];\n pref.push_back(sum);\n }\n sum=0;\n int n=nums.size()... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 2 | {
"code": "class Solution {\nprivate:\n vector<int> psum = vector<int>(1e5);\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size(), sum = 0, n_left, minmoves = n;\n\n psum[0] = nums[0];\n\n for(int i = 1; i < n; i++) {\n psum[i] = psum[i-1] + nums[i];\n ... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 2 | {
"code": "class Solution {\nprivate:\n vector<int> psum = vector<int>(1e5);\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size(), sum = 0, n_left, minmoves = n;\n\n psum[0] = nums[0];\n\n for(int i = 1; i < n; i++) {\n psum[i] = psum[i-1] + nums[i];\n ... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 2 | {
"code": "class Solution {\nprivate:\n vector<int> psum = vector<int>(1e5);\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size(), sum = 0, n_left, minmoves = n;\n\n psum[0] = nums[0];\n\n for(int i = 1; i < n; i++) {\n psum[i] = psum[i-1] + nums[i];\n ... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n unordered_map<int, int> prefix;\n int v = 0, retv = INT_MAX;\n for (int i = 0; i < nums.size(); i++) {\n v += nums[i];\n if (v > x) break;\n if (v == x) retv = min(retv, i + ... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n unordered_map<int,int> mpp;\n int n=nums.size(), sum=0;\n for(int r=n-1; r>=0; r--){\n sum+=nums[r];\n if(sum<=x)mpp[sum]=n-r;\n else break;\n if(r==0 && sum<x)ret... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 2 | {
"code": "class Solution {\n public:\n int minOperations(vector<int>& nums, int x) {\n unordered_map<int, int> left;\n int res = INT_MAX;\n for (auto l = 0, sum = 0; l <= nums.size() && sum <= x; ++l) {\n left[sum] = l;\n if (l < nums.size())\n sum += nu... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size();\n // remain -> idx from left \n map<int, int> loc;\n int cur = x;\n int result = INT_MAX;\n for (int i = 0; i < nums.size(); i++) {\n cur -= nums[i];\n ... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n=nums.size();\n vector<int> prefixsum(n,0);\n int sum=0;\n for (int i=0;i<n;i++){\n sum+=nums[i];\n prefixsum[i]=sum;\n }\n int ans=INT_MAX;\n unordered... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 2 | {
"code": "/*\n\nhttps://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/discuss/935935/Java-Detailed-Explanation-O(N)-Prefix-SumMap-Longest-Target-Sub-Array\n\nhttps://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/discuss/936074/JavaPython-3-Sliding-window%3A-Longest-subarray-sum-to-the-t... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int total_sum = 0;\n for (int num : nums) {\n total_sum += num;\n }\n \n int target = total_sum - x;\n if (target < 0) return -1;\n if (target == 0) return nums.size();\n \n unordered_map<int... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size();\n int total_sum = 0;\n for (int num : nums) {\n total_sum += num;\n }\n \n int target = total_sum - x;\n if (target < 0) {\n return -1;\... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size();\n int total_sum = 0;\n\n for(int num : nums) {\n total_sum += num;\n }\n\n int target = total_sum - x;\n if(target == 0) return n; // entire array sum equ... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size();\n unordered_map<int,int> m;\n m[0] = -1;\n int sum = 0;\n for(auto a : nums)\n sum+=a;\n if(sum == x) return n;\n if(sum < x) return -1;\n i... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 2 | {
"code": "class Solution {\n\n int helper (vector <int> &nums, int &target){\n int n = nums.size();\n unordered_map <int, int> mp;\n mp[0] = -1;\n int sum = 0, maxlen = 0;\n for (int i = 0; i < n; i++){\n sum += nums[i];\n\n mp[sum] = i;\n\n if ... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 2 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n const int sum = accumulate(nums.begin(), nums.end(), 0); \n const int target = sum - x;\n if (target == 0) {\n return nums.size();\n }\n // find the longest subarray whose sum is target.\n c... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 3 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int goal) {\n //largest subarray whose sum is x\n int n = nums.size();\n int presum=0;\n int larg=0;\n int x=0;\n for(auto it:nums)\n x+=it;\n x-=goal;\n if(x==0)\n ... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 3 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size();\n vector<int> preSum=nums;\n int ans=INT_MAX;\n int target= -x;\n \n for(int i=0;i<n;i++){\n target += nums[i];\n }\n if(target==0) return n... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 3 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size();\n vector<int> preSum=nums;\n int ans=INT_MAX;\n int target= -x;\n \n for(int i=0;i<n;i++){\n target += nums[i];\n }\n if(target==0) return n... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 3 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size();\n vector<int> preSum=nums;\n int ans=INT_MAX;\n int target= -x;\n \n for(int i=0;i<n;i++){\n target += nums[i];\n }\n if(target==0) return n... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 3 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int tot = accumulate(nums.begin(),nums.end(),0);\n int n = nums.size();\n int target = tot - x;\n if(target == 0)return n;\n map<int,int> mp;\n mp[0] = -1;\n int maxi = 0;\n ... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 3 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& a, int x) {\n long long s = 0;\n for(auto i:a)\n s += i;\n \n s = s - x;\n if(s==0)\n return (int)a.size();\n \n map<long long, int> d;\n\n d[0] = -1;\n int... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 3 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int sum = 0;\n for(auto n:nums){\n sum += n;\n }\n int find = sum - x;\n if(find == 0){\n return nums.size();\n }\n map<int,int> m;\n m[0] = -1;\n ... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 3 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size();\n int len = INT_MIN;\n int s = 0;\n for(int i = 0; i < n; i++){\n s += nums[i];\n }\n if(s == x){\n return n;\n }\n int sum = s-x... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 3 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size();\n int total = accumulate(nums.begin(),nums.end(),0);\n int target = total - x;\n if(target < 0) return -1;\n if(target == 0) return n;\n int maxLen = -1;\n un... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 3 | {
"code": "#include <vector>\n#include <unordered_map>\n#include <algorithm>\n\nclass Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size();\n int res = n + 1;\n int t_sum = 0;\n for (int i : nums) {\n t_sum += i;\n }\n int k ... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 3 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int totalSum = accumulate(nums.begin(), nums.end(), 0);\n int target = totalSum - x;\n \n if (target < 0) return -1;\n \n unordered_map<int, int> prefixSum;\n prefixSum[0] = -1;\n... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 3 | {
"code": "class Solution {\npublic:\n int maxSubArrayLen(vector<int>& nums, int k) {\n\n //[1,-1,5,-2,3]. k=3\n //[1,0, 5, 3,6]\n\n // -2,-1,2,1 k=1\n // -2, -3,-1,0\n // -2 -4, -2,-1\n unordered_map<long long, int>mmap;\n mmap[0]=-1;\n int res=0;\n ... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 3 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size();\n unordered_map<int, int> hmap;\n int minOps = INT_MAX;\n int curSum = 0;\n\n for(int i=0; i<n; ++i) {\n curSum += nums[i];\n if(curSum == x) {\n ... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 3 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n x = accumulate(nums.begin(), nums.end(), 0) - x;\n unordered_map<int, int> vis{ {0, -1} };\n int n = nums.size();\n int ans = 1 << 30;\n for (int i = 0, s = 0; i < n; ++i) {\n s += n... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 3 | {
"code": "#include <vector>\n#include <unordered_map>\n#include <algorithm>\n\nclass Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size();\n int res = n + 1;\n int t_sum = 0;\n unordered_map<int, int> ps_ar;\n int pre_sum = 0;\n ps_ar[... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 3 | {
"code": "class Solution {\npublic:\n #define ll long long\n int minOperations(vector<int>& nums, int x) {\n // longest subarray with sum = total-x\n int n = nums.size();\n ll totalSum = 0;\n for(int num: nums) totalSum += num;\n int maxLength = 0;\n ll target = totalS... |
1,776 | <p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p>
<p>... | 3 | {
"code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) { \n int n = nums.size();\n int sum = 0;\n unordered_map<int, int> mp;\n mp[0] = -1;\n for(int i = 0; i<n; i++) {\n sum += nums[i];\n mp[sum] = i;\n }\n ... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.