id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
1,207 | <p>Given the <code>root</code> of a binary tree, each node in the tree has a distinct value.</p>
<p>After deleting all nodes with a value in <code>to_delete</code>, we are left with a forest (a disjoint union of trees).</p>
<p>Return the roots of the trees in the remaining forest. You may return the result in any ord... | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod... |
1,207 | <p>Given the <code>root</code> of a binary tree, each node in the tree has a distinct value.</p>
<p>After deleting all nodes with a value in <code>to_delete</code>, we are left with a forest (a disjoint union of trees).</p>
<p>Return the roots of the trees in the remaining forest. You may return the result in any ord... | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod... |
1,207 | <p>Given the <code>root</code> of a binary tree, each node in the tree has a distinct value.</p>
<p>After deleting all nodes with a value in <code>to_delete</code>, we are left with a forest (a disjoint union of trees).</p>
<p>Return the roots of the trees in the remaining forest. You may return the result in any ord... | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod... |
1,207 | <p>Given the <code>root</code> of a binary tree, each node in the tree has a distinct value.</p>
<p>After deleting all nodes with a value in <code>to_delete</code>, we are left with a forest (a disjoint union of trees).</p>
<p>Return the roots of the trees in the remaining forest. You may return the result in any ord... | 3 | {
"code": "class Solution {\nprivate:\n void traverse(TreeNode* root, queue<TreeNode*>* nodes, unordered_set<int> delete_set) {\n if(root == nullptr)\n return;\n\n if(root->left) {\n if(find(delete_set.begin(), delete_set.end(), root->left->val) != delete_set.end()) {\n ... |
1,207 | <p>Given the <code>root</code> of a binary tree, each node in the tree has a distinct value.</p>
<p>After deleting all nodes with a value in <code>to_delete</code>, we are left with a forest (a disjoint union of trees).</p>
<p>Return the roots of the trees in the remaining forest. You may return the result in any ord... | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod... |
1,207 | <p>Given the <code>root</code> of a binary tree, each node in the tree has a distinct value.</p>
<p>After deleting all nodes with a value in <code>to_delete</code>, we are left with a forest (a disjoint union of trees).</p>
<p>Return the roots of the trees in the remaining forest. You may return the result in any ord... | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod... |
1,207 | <p>Given the <code>root</code> of a binary tree, each node in the tree has a distinct value.</p>
<p>After deleting all nodes with a value in <code>to_delete</code>, we are left with a forest (a disjoint union of trees).</p>
<p>Return the roots of the trees in the remaining forest. You may return the result in any ord... | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod... |
1,207 | <p>Given the <code>root</code> of a binary tree, each node in the tree has a distinct value.</p>
<p>After deleting all nodes with a value in <code>to_delete</code>, we are left with a forest (a disjoint union of trees).</p>
<p>Return the roots of the trees in the remaining forest. You may return the result in any ord... | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod... |
1,207 | <p>Given the <code>root</code> of a binary tree, each node in the tree has a distinct value.</p>
<p>After deleting all nodes with a value in <code>to_delete</code>, we are left with a forest (a disjoint union of trees).</p>
<p>Return the roots of the trees in the remaining forest. You may return the result in any ord... | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod... |
1,207 | <p>Given the <code>root</code> of a binary tree, each node in the tree has a distinct value.</p>
<p>After deleting all nodes with a value in <code>to_delete</code>, we are left with a forest (a disjoint union of trees).</p>
<p>Return the roots of the trees in the remaining forest. You may return the result in any ord... | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod... |
1,207 | <p>Given the <code>root</code> of a binary tree, each node in the tree has a distinct value.</p>
<p>After deleting all nodes with a value in <code>to_delete</code>, we are left with a forest (a disjoint union of trees).</p>
<p>Return the roots of the trees in the remaining forest. You may return the result in any ord... | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod... |
1,207 | <p>Given the <code>root</code> of a binary tree, each node in the tree has a distinct value.</p>
<p>After deleting all nodes with a value in <code>to_delete</code>, we are left with a forest (a disjoint union of trees).</p>
<p>Return the roots of the trees in the remaining forest. You may return the result in any ord... | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n// vector<TreeNode*> ans;\n// void delNodeUtil(TreeNode* root, Tree... |
1,207 | <p>Given the <code>root</code> of a binary tree, each node in the tree has a distinct value.</p>
<p>After deleting all nodes with a value in <code>to_delete</code>, we are left with a forest (a disjoint union of trees).</p>
<p>Return the roots of the trees in the remaining forest. You may return the result in any ord... | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod... |
1,207 | <p>Given the <code>root</code> of a binary tree, each node in the tree has a distinct value.</p>
<p>After deleting all nodes with a value in <code>to_delete</code>, we are left with a forest (a disjoint union of trees).</p>
<p>Return the roots of the trees in the remaining forest. You may return the result in any ord... | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod... |
1,207 | <p>Given the <code>root</code> of a binary tree, each node in the tree has a distinct value.</p>
<p>After deleting all nodes with a value in <code>to_delete</code>, we are left with a forest (a disjoint union of trees).</p>
<p>Return the roots of the trees in the remaining forest. You may return the result in any ord... | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 0 | {
"code": "#pragma GCC optimize(\"O3,unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n\nstatic const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return true;\n}();\n\ninline bool isdigit(char c) {\n return c >= '... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 0 | {
"code": "#pragma GCC optimize(\"O3,unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n\nstatic const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return true;\n}();\n\ninline bool isdigit(char c) {\n return c >= '... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 0 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n reverse(s.begin(),s.end());\n int n = s.size();\n int left = 0,right = 0;\n int i =0;\n while(i<n){\n while(i<n && s[i]==' '){\n i++;\n }\n if(i==n){\n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 0 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n reverse(s.begin(), s.end());\n\n int i = 0, m = s.length(), top = 0, end = 0;\n while (i < m)\n {\n while (i < m && s[i] == ' ')\n i++;\n if (i == m) break;\n\n while... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 0 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n reverse(s.begin(),s.end());\n int i =0;\n int l=0,r=0;\n int n = s.length();\n \n while(i<n){\n while(i<n && s[i] !=' '){\n s[r++]=s[i++];\n }\n if(l<r){... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 0 | {
"code": "class Solution {\npublic:\n // My approach - using three pointer i, j and k\n // Time Complexity: O(n) to traverse string s\n // + O(2m) => O(m) to traverse string str and reverse each word\n // + O(m) to reverse string str\n // note: m is the length of string str - str is formed by removing... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 1 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n string ans=\"\";\n int n=s.size();\n int prev=n;\n int fl=-1;\n for(int i=n-1;i>0;i--){\n if(s[i]==' ') {\n fl=1;\n prev--;\n continue;\n }\... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 1 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n string result=\"\";\n int end= s.length();\n for (int i=end-1;i>=0;i--){\n if (s[i]==' '){\n if (i+1<end){\n result+=s.substr(i+1,end-(i+1)) + \" \";\n }\n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 1 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n string ans;\n string temp = \"\";\n\n stack<string>st;\n\n for( int i = 0; i < s.length(); i++)\n {\n if( s[i] == ' ')\n {\n if( temp.size() > 0)\n st.push... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 1 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n std::stack<string> words;\n string result;\n for(int i = 0; i < s.size(); i++)\n {\n string word {};\n // Skip any spaces\n if(s[i] == ' ')\n {\n continue;... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 1 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n string word=\"\";\n int n=s.length();\n stack<string> st;\n int i=0;\n while(s[i]==' ' && i<n) i++;\n\n while(i<n){\n string word=\"\";\n while(s[i]!=' ' && i<n){\n wo... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 1 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n stack<string>stk;\n string t=\"\";\n for(auto i:s){\n if(isalnum(i)){\n t+=i;\n }\n else{\n if(t.length())stk.push(t);\n t=\"\";\n }\n... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n int i = 0;\n while(s[i]==' ')i++;\n s.erase(0, i);\n i = s.size()-1;\n while(s[i]==' ')i--;\n s.erase(i+1);\n vector<string>answer;\n string current = \"\";\n for(auto it : s){\n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n stack<string> st;\n s.push_back(' ');\n int n=s.size();\n for(int i=0;i<n;i++){\n if(s[i]==' ') continue;\n else{\n int temp=i;\n while(i<n&&s[i]!=' ') i++;\n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\npublic:\n\n stack<string> separatewords(string& s)\n {\n stack<string> separatewords;\n\n uint left = 0;\n uint right = 0;\n uint length = s.length();\n bool skipspaces = false;\n\n /*\n 1 - space after word\n 2 - multi... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n vector<string>words;\n int index = 0;\n words.push_back(\"\");\n for (int i = 0; i < s.length(); i++) {\n if (s[i] == ' ') {\n continue;\n } else if (i > 0 && s[i-1] == ' ') {\n... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n std::map<int, std::string> mmap;\n std::string ss(\"\");\n int count =0;\n for (auto c : s) {\n if (c == ' ') {\n if (ss.size() > 0) {\n mmap[count] = ss;\n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n std::map<int, std::string> mmap;\n std::string ss(\"\");\n int count =0;\n for (auto c : s) {\n if (c == ' ') {\n if (ss.size() > 0) {\n mmap[count] = ss;\n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\npublic:\n /*\n std::vector<std::string> splitString(const std::string& str, char delimiter) {\n std::vector<std::string> tokens;\n std::stringstream ss(str);\n std::string token;\n\n while (std::getline(ss, token, delimiter)) {\n if (!token.emp... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "#include <stack>\nclass Solution {\npublic:\n string reverseWords(string s) {\n int max_len = s.size();\n int idx = 0;\n vector<string> ret;\n string ret_;\n string space = \" \";\n bool is_first_space = true;\n while (idx < max_len && s[idx] == char(' ')) {\n idx += 1;\n }\... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n vector<string>v;\n string to=\"\";\n // cout<<s[2]<<endl;\n for(int i=0;i<s.size();i++){\n // cout<<s[i]<<endl;\n if(s[i]==' '){\n // cout<<to<<endl;\n v.push_back(to);\n... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\npublic:\n vector<string> splitWord(string s){\n vector<string> ans;\n\n int start = 0;\n for(int i=0; i<s.size(); i++){\n if(s[i]==' '){\n ans.push_back(s.substr(start, i-start));\n start = i+1;\n }\n }\n\n... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n int i,j,k,l;\n int n=s.size();\n i=n-1;\n string ans=\"\";\n if(n==1) return s;\n while(i>0)\n {\n while(s[i]==' ' && i>0)\n i--;\n if(i<0) break;\n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n vector<string> words;\n string word=\"\";\n for(int i=0;i<s.size();i++){\n if(s[i]==' '){\n words.push_back(word);\n word=\"\";\n }\n else{\n w... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\npublic:\n auto split(const string &str, char target) const -> vector<string> {\n int n = str.size();\n int start = 0;\n vector<string> ans;\n for (int i = 0; i < n; ++i) {\n if (str[i] == target) {\n ans.push_back(str.substr(start, ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "#include <string.h>\n\nclass Solution {\npublic:\n string reverseWords(string s) {\n std::vector<string> vec {};\n int head = 0;\n int tail = s.size()-1;\n char space = ' '; \n while (head < tail){\n if(s[head] == ' '){\n head ++;\n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\npublic:\n void solve(string& s,string& ans,string x,int i){\n if(i<0){\n reverse(x.begin(), x.end());\n ans+=x;\n return;\n }\n if(s[i]==' '){\n if((i>0 && s[i-1]==' ') || i==0){\n solve(s,ans,x,i-1);\n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\npublic:\n void solve(string& s,string& ans,string x,int i){\n if(i<0){\n reverse(x.begin(), x.end());\n ans+=x;\n return;\n }\n if(s[i]==' '){\n if((i>0 && s[i-1]==' ') || i==0){\n solve(s,ans,x,i-1);\n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\npublic:\n vector<string> remove_spaces(vector<string> str){\n int n=str.size();\n vector<string> ans;\n for(int i=0;i<n;i++){\n if(str[i]!=\" \" && str[i]!=\"\"){\n ans.push_back(str[i]);\n }\n }\n return ans;\n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\npublic:\n vector<string> remove_spaces(vector<string> str){\n int n=str.size();\n vector<string> ans;\n for(int i=0;i<n;i++){\n if(str[i]!=\" \" && str[i]!=\"\"){\n ans.push_back(str[i]);\n }\n }\n return ans;\n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\nstd::vector<std::string> putStringInVector( std::string s) {\n std::vector<std::string> s_vec;\n std::string current_word;\n for (int i = 0; i < s.size(); ++i) {\n if ( s[i] == ' ' && !current_word.empty() ) {\n s_vec.push_back( current_word ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n std::stack<std::queue <char>*> stackOfQueue;\n int index=0;\n while(index<s.length()){\n if(s[index]!=' '){\n std::queue<char>* tmp_queue=new std::queue<char>;\n int tmp_index=0;\n... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\npublic:\n string reverseWords(const string& s) {\n // Trim leading and trailing spaces\n string trimmed = regex_replace(s, regex(\"^\\\\s+|\\\\s+$\"), \"\");\n\n // Split string by whitespace\n vector<string> words;\n stringstream ss(trimmed);\n string word;\n whil... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\npublic:\nstring cleanString(string& s)\n{\n std::string trimmed=std::regex_replace(s,std::regex(\"^ +| +$\"),\"\");\n std::string cleaned=std::regex_replace(trimmed,std::regex(\" +\"),\" \");\n return cleaned;\n}\n\n\n\n string reverseWords(string s) {\nint st=0;\nint e=s.length(... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n reverse(s.begin(),s.end());\n int j=0;\n for(int i=0;i<s.length();i++)\n {\n if(s[i] == ' ')\n {\n reverse(s.begin()+j, s.begin()+i);\n j=i+1;\n }\n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n vector<int> vec;\n regex pattern(\"\\\\s+\");\n string result = regex_replace(s, pattern, \" \");\n size_t firstNonSpace = result.find_first_not_of(\" \");\n size_t lastNonSpace = result.find_last_not_of(\" ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n regex pattern(\"\\\\s+\");\n string result = regex_replace(s, pattern, \" \");\n size_t firstNonSpace = result.find_first_not_of(\" \");\n size_t lastNonSpace = result.find_last_not_of(\" \");\n if (firstNon... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "#include<regex>\nclass Solution {\npublic:\n string reverseWords(string s) {\n int front=0;\n int i=0;\n while(s[i]==' ')\n {\n front++;\n i++;\n }\n\n s.erase(0,front);\n int j=s.size()-1;\n int back=0;\n while(s[j]==' ')\n {\n back++;\n j--;\n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "#include<regex>\nclass Solution {\npublic:\n string reverseWords(string s) {\n int front=0;\n int i=0;\n //first trim the whitespaces out in front of the string\n while(s[i]==' ')\n {\n front++;\n i++;\n }\n\n s.erase(0,front);\n int j=s.size()-1;\n int back=0;\n... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n s = std::regex_replace(s, regex(\"\\\\s+\"), \" \");\n vector<string>wordList;\n stringstream ss(s);\n \n while (ss.good()) {\n string substr;\n getline(ss, substr, ' ');\n \n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\npublic:\n std::vector<std::string> split(const std::string& str) {\n std::vector<std::string> words;\n std::regex word_regex(\"(\\\\S+)\");\n auto words_begin = std::sregex_iterator(str.begin(), str.end(), word_regex);\n auto words_end = std::sregex_iterator();\n\n for (... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n string str,ans;\n reverse(s.begin(),s.end());\n s = std::regex_replace(s, std::regex(\"^ +| +$|( ) +\"), \"$1\");\n for(int i=0;i<s.size();i++)\n {\n if(s[i]==' ' )\n {\n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n std::regex pattern(\"\\\\s+\");\n std::string str = std::regex_replace(s, pattern, \" \");\n size_t firstNonSpace = str.find_first_not_of(\" \");\n size_t lastNonSpace = str.find_last_not_of(\" \");\n\n if (... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n if(s.size() == 1) return s;\n \n regex pat(\"[a-zA-Z0-9]+\");\n auto words_begin = sregex_iterator(s.begin(), s.end(), pat);\n auto words_end = sregex_iterator();\n \n string out;\n out.... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n if(s.size() == 1) return s;\n \n regex pat(\"[a-zA-Z0-9]+\");\n auto words_begin = sregex_iterator(s.begin(), s.end(), pat);\n auto words_end = sregex_iterator();\n \n string out;\n out.... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n std::vector<std::string> word_vec {};\n for (int i = 0; i < s.size(); i++) {\n if ((s[i] != ' ' && s[i + 1] == ' ') || (i == s.size() - 1 && s[i] != ' ')) {\n std::deque<char> temp;\n int... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "#include <regex>\nclass Solution {\npublic:\n string reverseWords(string s) {\n // Remove leading and trailing spaces\n s = std::regex_replace(s, std::regex(\"^\\\\s+|\\\\s+$\"), \"\");\n \n // Replace multiple spaces with a single space\n s = std::regex_replace(s, std... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "#include <regex>\nclass Solution {\npublic:\n string reverseWords(string s) {\n // Remove leading and trailing spaces\n s = std::regex_replace(s, std::regex(\"^\\\\s+|\\\\s+$\"), \"\");\n \n // Replace multiple spaces with a single space\n s = std::regex_replace(s, std... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "#include <sstream>\n#include <deque>\n#include <vector>\nclass Solution {\npublic:\n std::string reverseWords(std::string s) {\n std::stringstream senbuf {};\n std::vector<std::string> sentence {};\n std::deque<char> wordbuf {};\n for(auto rit = s.rbegin(); rit < s.rend(); ri... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n int i=0;\n string left=\"\", right=\"\", ans=\"\";\n while(i<=s.size()){\n if(s[i]==' ' && left==\"\"){\n i+=1;\n }else if((s[i]==' '||i==s.size()) && left!=\"\"){\n if(... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n string n = \"\";\n stringstream ss(s);\n string word;\n while (ss >> word) {\n if (!n.empty()){\n word += \" \";\n }\n n = word + n ;\n }\n return n;\n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n s+=\" \";\n int n=s.size();\n int cnt=0;\n string temp=\"\";\n string result=\"\";\n for(int i=0;i<n;i++)\n {\n if(s[i]!=' ')\n {\n temp+=s[i];\n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n int n=s.size();\n int l=0,r=n-1;\n string word=\"\";\n string ans=\"\";\n while(l<=r){\n char ch=s[l];\n if(ch!=' ')word+=ch;\n else{\n if(ans!=\"\"){\n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 2 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n string temp = \"\";\n string ans = \"\";\n for(char ch:s){\n if(ch == ' '){\n if(ans.empty()){\n ans = temp;\n }else{\n if(!temp.empty()) ans = temp + \" \" + ans;\n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 3 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n stringstream ss;\n ss<<s;\n string t;\n s=\"\";\n while(ss>>t)\n {\n s=t+\" \"+s;\n }\n return s.substr(0,s.size()-1);\n }\n};",
"memory": "23880"
} |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 3 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n\n stack<string> st;\n\n string temp = \"\";\n\n string ans = \"\";\n\n for(int i=0; i<s.size(); i++){\n if(s[i] == ' '){\n if(temp.size()){\n ans = \" \" + temp + ans;\n... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 3 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n\n\n vector<string>vec;\n int n=s.length();\n int i=0;\n while(i<n){\n\n string temp=\"\";\n while(i<n && s[i]!=' '){\n temp +=s[i];\n i++;\n }\n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 3 | {
"code": "#include <vector>\n#include <iostream>\n#include <sstream>\n\nclass Solution {\npublic:\n string reverseWords(string s) {\n\n string revertedWords;\n\n stringstream ss(s);\n string word;\n vector<string> words;\n\n while (ss >> word) {\n words.push_back(word... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 3 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n string ans;\n int start=0,i=0;\n stack<string> temp;\n\n while(s[i]==' ') i++;\n start=i;\n\n for(;i<s.size();){\n if(s[i]==' '){\n temp.push(s.substr(start,i-start));\n start=i... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 3 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n string ans;\n int start=0,i=0;\n stack<string> temp;\n\n while(s[i]==' ') i++;\n start=i;\n\n for(;i<s.size();){\n if(s[i]==' '){\n temp.push(s.substr(start,i-start));\n start=i... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 3 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n map<int,string> mp;\n string str=\"\";\n int count=1;\n for(int i=0;i<s.size();i++){\n if(s[i]==' '){\n if(i ==0 || s[i-1]==' ')\n continue;\n mp[count]=str;\... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 3 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n std::stringstream ss(s);\n const auto words = vector<string>(istream_iterator<string>(ss), istream_iterator<string>{});\n\n return reduce(rbegin(words), rend(words), string{}, [](const string lhs, const string rhs){\n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 3 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n vector<string> v;\n int start=0;\n for(int i=0;i<s.size();i++){\n if(s[i]==' '){\n string ss=s.substr(start,i-start);\n v.push_back(ss);\n start=i+1;\n }\... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 3 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n stringstream ss(s);\n vector<string> tokens;\n string token;\n string ans=\"\";\n while (getline(ss, token,' ')) {\n tokens.push_back(token);\n }\n for (auto it : tokens) {\n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 3 | {
"code": "#pragma GCC optimize (\"Ofast\")\n#pragma GCC optimize (\"O3\", \"unroll-loops\", \"-ffloat-store\")\n#pragma GCC target(\"avx,mmx,sse2,sse3,sse4\")\n\n// \"_\": A lambda function to enable faster I/O operations\nauto _=[]()noexcept{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);return 0;}();\n\n#include <... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 3 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n vector<string> ans;\n string word=\"\";\n for(int i=0;i<s.size();i++){\n if(s[i]==' '){\n \n ans.push_back(word);\n word=\"\";\n }\n else{\n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 3 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n vector<string> splited = splitString(s,\" \");\n string ans = \"\";\n for(int i = splited.size()-1;i>=0;i--){\n ans+=splited[i];\n ans+=\" \";\n }\n while(ans[ans.size()-1]==' ')\n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 3 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n int n_s = s.size();\n int i = n_s - 1, j;\n string r_s = \"\";\n\n while(i >= 0) {\n //cout <<\"s[\" << i << \"]-\" << s[i] << \"|\"; \n if(s[i] == ' ') {\n j = i+1;\n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 3 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n string my_string;\n int start, end;\n s=s.substr(s.find_first_not_of(\" \"),s.find_last_not_of(\" \")+1);\n end= s.find_last_not_of(\" \");\n start= s.find_last_of(\" \",end);\n while(start!=-1){\n \n my_string+= s.subst... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 3 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n regex re(\"\\\\w+\");\n smatch result;\n regex_search(s, result, re);\n string output;\n for (sregex_iterator it(s.begin(), s.end(), re), it_end; it != it_end; ++it ) {\n //cout << (*it)[0] << end... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 3 | {
"code": "class Solution {\npublic:\n\n string readWord(string s) {\n string result = \"\";\n int i = 0;\n while(i < s.length() && s[i] != ' ') {\n result += s[i++];\n }\n return result;\n }\n\n string reverseWords(string s) {\n vector<string> words;\n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 3 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n regex r(\"[^ ]+\");\n sregex_iterator it(s.begin(), s.end(), r);\n\n string res = it->str();\n while (++it != sregex_iterator()) {\n res = it->str() + ' ' + res;\n }\n\n return res;\n }\... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 3 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n stack<queue<char>> stk;\n for(int i=0;i<s.size();i++) {\n if(s[i] == ' ')\n continue;\n queue<char> q;\n while(i<s.size() && s[i]!= ' '){\n q.push(s[i]);\n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 3 | {
"code": "class Solution {\npublic:\n\n void ltrim(std::string &s) {\n s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {\n return !std::isspace(ch);\n }));\n }\n\n void rtrim(std::string &s) {\n s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned c... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 3 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n if (s.empty()) return s;\n\n const char* start = s.c_str();\n while (*start == ' ') start++;\n const char* end = start;\n while (*end != ' ' && *end != '\\0') end++;\n auto word = string(start, end-st... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 3 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n string word;\n int i =0;\n while(s[i]==' ' && i<s.length()){\n i++;\n }\n\n while(i<s.length()){\n if(s[i]==' '){\n break;\n }\n word = word + s[i];... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 3 | {
"code": "class Solution {\npublic:\n\n void ltrim(std::string &s) {\n s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {\n return !std::isspace(ch);\n }));\n }\n\n void rtrim(std::string &s) {\n s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned c... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 3 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n int n=s.size();\n const int N=1e4+10;\n vector<string>v(N,\"\");\n int j=0;\n for(int i=0;i<n;i++)\n { if(v[j]==\"\"&&s[i]==' ')\n {continue;}\n if(s[i]!=' ')\n {\n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 3 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n string ret = \"\";\n int length = s.size()-1;\n\n while(length >= 0){\n stack<char> c;\n while(length >= 0 && s[length] != ' '){\n c.push(s[length]);\n length--;\n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 3 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n string r = \"\";\n vector<queue<char>> vqueues;\n queue<char>q;\n for (int i = 0; i < s.size(); i++){\n if (s[i] != ' ')\n q.push(s[i]);\n if (s[i] == ' ' && !q.empty()){\n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 3 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n string res;\n vector<stack<char>> wordStacks = {{}}; // vector of stacks that will hold each word\n int wordIdx = 0; // will keep track of stack index\n\n for(int i = s.size() - 1; i >=0; i--)\n {\n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 3 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n string res;\n vector<stack<char>> wordStacks = {{}}; // vector of stacks that will hold each word\n int wordIdx = 0; // will keep track of stack index\n\n for(int i = s.size() - 1; i >=0; i--)\n {\n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 3 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n string res;\n vector<stack<char>> wordStacks = {{}}; // vector of stacks that will hold each word\n int wordIdx = 0; // will keep track of stack index\n\n for(int i = s.size() - 1; i >=0; i--)\n {\n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 3 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n string res;\n vector<stack<char>> wordStacks = {{}};\n int wordIdx = 0;\n\n for(int i = s.size() - 1; i >=0; i--)\n {\n if(s[i] != ' ')\n wordStacks[wordIdx].push(s[i]);\n ... |
151 | <p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p>
<p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p>
<p>Return <em>a string of the words in reverse order conc... | 3 | {
"code": "class Solution {\npublic:\n string reverseWords(string s) {\n int length = s.length();\n \n // removes leading spaces\n int index = 0;\n while(index < length && s[index] == ' ') index++;\n \n int avail = 0;\n for(int i = index; i < length; i++)\n ... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.