id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n static bool compare(const string& a, const string& b) {\n const auto len1 = a.length(), len2 = b.length(), mx = lcm(len1, len2);\n for (size_t i = 0; i < mx; i++)\n if (const char c1 = a[i % len1], c2 = b[i % len2]; c1 != c2)\n return c1 > c2;\n return false;\n }\n\n string largestNumber(const vector<int>& nums) {\n vector<string> s; s.reserve(nums.size());\n for (const auto& x : nums)\n s.push_back(to_string(x));\n ranges::sort(s, compare);\n return s[0][0] != '0' ? accumulate(s.begin(), s.end(), string{}) : \"0\";\n }\n};",
"memory": "15400"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "bool compare(int a,int b)\n{\n return to_string(a)+to_string(b)>to_string(b)+to_string(a);\n}\nclass Solution {\npublic:\n string largestNumber(vector<int>& arr) {\n sort(arr.begin(),arr.end(),compare);\n string ans = \"\";\n for(int i = 0;i<arr.size();i++)\n ans+=to_string(arr[i]);\n if(ans[0]=='0') return \"0\";\n return ans; \n }\n};",
"memory": "15400"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n // bool compare(int &a,int &b)\n // {\n // string aa=to_string(a);\n // string bb=to_string(b);\n // return (aa+bb)>(bb+aa);\n // };\n string largestNumber(vector<int>& nums) {\n\n auto compare=[](int &a,int &b)\n {\n string aa=to_string(a);\n string bb=to_string(b);\n return (aa+bb)>(bb+aa);\n };\n\n sort(nums.begin(),nums.end(),compare);\n string ans=\"\";\n for(auto x:nums)\n ans+=to_string(x);\n if(nums[0]==0)return \"0\";\n return ans;\n }\n};",
"memory": "15500"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string largestNumber(vector<int>& nums) {\n sort(nums.begin(), nums.end(), [](int const& x, int const& y) {\n string sx = to_string(x);\n string sy = to_string(y);\n return (sx+sy > sy+sx);\n });\n string s;\n if (nums[0] == 0) return \"0\";\n bool non_zero=false;\n for(auto i: nums) {\n // cout << s;\n s += to_string(i);\n if (i>0) non_zero=true;\n }\n if (!non_zero) return \"0\";\n return s;\n }\n};",
"memory": "15500"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string largestNumber(vector<int>& nums) {\n sort(nums.begin(), nums.end(), [](int const& x, int const& y) {\n string sx = to_string(x);\n string sy = to_string(y);\n return (sx+sy > sy+sx);\n });\n string s;\n bool non_zero=false;\n for(auto i: nums) {\n // cout << s;\n s += to_string(i);\n if (i>0) non_zero=true;\n }\n if (!non_zero) return \"0\";\n return s;\n }\n};",
"memory": "15600"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n bool static compare(int num1,int num2){\n string str1=to_string(num1);\n string str2=to_string(num2);\n return str1+str2>str2+str1;\n }\n string largestNumber(vector<int>& nums) {\n sort(nums.begin(),nums.end(),compare);\n string ans=\"\";\n for(int i=0;i<nums.size();i++){\n ans+=to_string(nums[i]);\n }\n if(ans[0]=='0')return \"0\";\n return ans;\n }\n};",
"memory": "15600"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool static check(int a, int b) {\n string s1 = to_string(a);\n string s2 = to_string(b);\n\n return s2 + s1 < s1 + s2;\n }\n\n string largestNumber(vector<int>& nums) {\n string ans;\n sort(nums.begin(), nums.end(), check);\n\n\n if (nums[0] == 0)\n return \"0\";\n\n for (int i = 0; i < nums.size(); i++) {\n ans += to_string(nums[i]);\n }\n\n return ans;\n }\n};\n\nconst int ZERO = []() {\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n return 0;\n}();",
"memory": "15700"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\n \n\npublic:\n string largestNumber(vector<int>& nums) {\n string ans = \"\";\n\n auto cmp = [](int& x, int& y) {\n string s = to_string(x) + to_string(y);\n string t = to_string(y) + to_string(x);\n return s > t;\n };\n sort(nums.begin(), nums.end(), cmp);\n\n for (auto num : nums) ans += to_string(num);\n\n return ans[0] == '0' ? \"0\" : ans;\n }\n};",
"memory": "15700"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n string largestNumber(vector<int>& nums) {\n std::sort(nums.begin(), nums.end(), [](int a, int b) {\n return std::to_string(a) + std::to_string(b) > std::to_string(b) + std::to_string(a);\n });\n \n if (nums[0] == 0) {\n return \"0\";\n }\n\n string result;\n std::for_each(nums.begin(), nums.end(), [&result](int num) {\n result += std::to_string(num);\n });\n return result;\n }\n};\n",
"memory": "15800"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n string largestNumber(vector<int>& nums) {\n vector<string>num;\n for(int i=0;i<nums.size();i++){\n num.push_back(to_string(nums[i]));\n }\n for(int i=0;i<num.size()-1;i++){\n for(int j=0;j<num.size()-1;j++){\n string s1=num[j]+num[j+1];\n string s2=num[j+1]+num[j];\n if(s2>s1){\n string temp=num[j];\n num[j]=num[j+1];\n num[j+1]=temp;\n }\n\n }\n }\n string s=\"\";\n for(int i=0;i<num.size();i++){\n s+=num[i];\n }\n if(num[0]==\"0\"){\n return \"0\";\n }\n \n return s;\n \n }\n};",
"memory": "15900"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n string largestNumber(vector<int>& nums) \n {\n vector<string> str;\n for(int i = 0; i < nums.size(); i++)\n {\n str.push_back(to_string(nums[i]));\n }\n for(int i = 0; i < nums.size() - 1; i++)\n {\n for(int j = 1; j < nums.size() - i; j++)\n {\n if(str[j] + str[j - 1] > str[j - 1] + str[j])\n {\n swap(str[j], str[j - 1]);\n }\n }\n }\n if(str[0] == \"0\") return \"0\";\n string ans;\n for(int i = 0; i < str.size(); i++)\n {\n ans += str[i];\n }\n return ans;\n }\n};",
"memory": "15900"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool compare(string& s1, string& s2){\n return s1+s2 > s2+s1;\n }\n\n string largestNumber(vector<int>& nums) {\n list<string> ordered;\n for(int n : nums){\n string s = to_string(n);\n auto it = ordered.begin();\n while(it!=ordered.end()){\n if(compare(*it,s)){it++;}\n else{\n ordered.insert(it, s);\n break;\n }\n }\n if(it == ordered.end()) ordered.push_back(s);\n }\n if (ordered.front() == \"0\") return \"0\";\n string f;\n for(string s : ordered) f += s;\n return f;\n }\n};\n\n// class Solution {\n// public:\n// string largestNumber(vector<int>& nums) {\n// vector<string> strs;\n// for(int n : nums){\n// strs.push_back(to_string(n) + 'a');\n// }\n// sort(strs.begin(), strs.end());\n// string f;\n// while(strs.size()){\n// string s = strs.back();\n// s.pop_back();\n// f += s;\n// }\n// return f;\n// }\n// };",
"memory": "16600"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n string largestNumber(vector<int>& nums) {\n std::sort(nums.begin(), nums.end(), [](int a, int b) {\n return std::to_string(a) + std::to_string(b) > std::to_string(b) + std::to_string(a);\n });\n \n if (nums[0] == 0) {\n return \"0\";\n }\n\n std::string result;\n return std::accumulate(nums.begin(), nums.end(), \n std::move(result), \n [](const std::string& acc, int num) {\n return acc + std::to_string(num);\n }\n );\n }\n};\n",
"memory": "16700"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool static comp(int &nums1, int &nums2) {\n string s1;\n string s2;\n s1 = to_string(nums1) + to_string(nums2);\n s2 = to_string(nums2) + to_string(nums1);\n return s1>s2;\n }\n string largestNumber(vector<int>& nums) {\n sort(nums.begin(), nums.end(), comp);\n string res = \"\";\n for(int i=0; i<nums.size(); i++) {\n res += to_string(nums[i]);\n if(res == \"0\" || res == \"00\") {\n res = \"0\";\n }\n }\n return res;\n }\n};",
"memory": "16700"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n string largestNumber(vector<int>& nums) {\n sort(begin(nums), end(nums), [](auto &a, auto &b) {\n string aa = std::to_string(a);\n string bb = std::to_string(b);\n return aa + bb > bb + aa;\n });\n string ans = std::accumulate(begin(nums), end(nums), string(\"\"), [](auto a, auto &b) {\n return a + std::to_string(b);\n });\n return ans[0] == '0' ? \"0\" : ans;\n }\n};",
"memory": "16800"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int compare(long long a, long long b) {\n string s1 = to_string(a);\n string s2 = to_string(b);\n string temp1 = s1 + s2;\n string temp2 = s2 + s1;\n int x=0;\n while(x<temp1.length() && x<temp2.length()) {\n if(temp1[x]>temp2[x]) return a;\n else if(temp1[x]<temp2[x]) return b;\n x++;\n }\n return a;\n }\n string largestNumber(vector<int>& nums) {\n vector<int>temp = nums;\n for(int i=1;i<nums.size();i++) {\n int tmp = i;\n for(int j=i-1;j>=0;j--) {\n if(compare(temp[i], temp[j])==temp[i]) {\n tmp = j;\n }\n }\n if(tmp==i) continue;\n else {\n vector<int>temp2 = temp;\n for(int i1=tmp+1;i1<=i;i1++) {\n temp[i1]=temp2[i1-1];\n }\n temp[tmp] = nums[i];\n }\n }\n string ans = \"\";\n for(int i: temp) {\n string s = to_string(i);\n ans+=s;\n }\n if(ans==\"0\") return ans;\n if(ans[0]!='0') return ans;\n\n int i = 0; \n while (ans[i] == '0') \n i++; \n ans.erase(0, i-1); \n return ans;\n }\n};",
"memory": "16900"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool compare(long long a, long long b) {\n string s1 = to_string(a);\n string s2 = to_string(b);\n string temp1 = s1 + s2;\n string temp2 = s2 + s1;\n int x=0;\n while(x<temp1.length() && x<temp2.length()) {\n if(temp1[x]>temp2[x]) return true;\n else if(temp1[x]<temp2[x]) return false;\n x++;\n }\n return true;\n }\n string largestNumber(vector<int>& nums) {\n vector<int>temp = nums;\n for(int i=1;i<nums.size();i++) {\n int tmp = i;\n for(int j=i-1;j>=0;j--) {\n if(compare(temp[i], temp[j])) {\n tmp = j;\n }\n }\n if(tmp==i) continue;\n else {\n vector<int>temp2 = temp;\n for(int i1=tmp+1;i1<=i;i1++) {\n temp[i1]=temp2[i1-1];\n }\n temp[tmp] = nums[i];\n }\n }\n string ans = \"\";\n for(int i: temp) {\n string s = to_string(i);\n ans+=s;\n }\n if(ans==\"0\") return ans;\n if(ans[0]!='0') return ans;\n\n int i = 0; \n while (ans[i] == '0') \n i++; \n ans.erase(0, i-1); \n return ans;\n }\n};",
"memory": "16900"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n string largestNumber(vector<int>& nums) {\n\n int n = nums.size();\n vector<string>str(n);\n string result = \"\";\n\n //convert nums from int to string\n for(int i = 0; i < n; i++){\n str[i] = to_string(nums[i]);\n }\n\n //lambda function to sort elements i.e; if ba > ab then swap.\n sort(str.begin(), str.end(), [](string& a, string& b) {\n return a + b > b + a;\n });\n \n //if all elements are 0 \n if (str[0] == \"0\") {\n return \"0\";\n }\n\n for(int i = 0; i < n; i++){\n result += str[i];\n }\n return result;\n\n }\n\n};",
"memory": "17000"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n string largestNumber(vector<int>& nums) {\n std::sort(nums.begin(), nums.end(), [](int a, int b) {\n return std::to_string(a) + std::to_string(b) > std::to_string(b) + std::to_string(a);\n });\n \n if (nums[0] == 0) {\n return \"0\";\n }\n\n std::string result;\n size_t length = std::accumulate(nums.begin(), nums.end(), \n 0,\n [](int acc, int num) {\n return acc + std::to_string(num).length();\n }\n );\n result.reserve(length);\n return std::accumulate(nums.begin(), nums.end(), \n std::move(result), \n [](std::string acc, int num) {\n return acc + std::to_string(num);\n }\n );\n }\n};\n",
"memory": "17100"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n string largestNumber(vector<int>& nums) {\n std::ranges::sort(nums,\n [](const std::string& x, const std::string& y) {\n return x + y > y + x;\n },\n [](const int x) {\n return std::to_string(x);\n });\n if (nums.front() == 0) return \"0\";\n std::string ret;\n for (const auto num : nums) {\n ret.append(std::to_string(num));\n }\n return ret;\n }\n};",
"memory": "17100"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n string largestNumber(vector<int>& nums) {\n vector<string>v;\n for(int i:nums){\n v.push_back(to_string(i));\n }\n sort(v.begin(),v.end(),[](string &s1,string &s2){return s1+s2>s2+s1;});\n \n string res;\n for(string s:v){\n res+=s;\n }\n while(res[0]=='0' && res.size()>1){\n res.erase(0,1);\n }\n return res;\n }\n};",
"memory": "17200"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n\n static bool mycomp(string a , string b){\n string t1 = a+b;\n string t2 = b+a;\n return t1 > t2 ;\n }\n string largestNumber(vector<int>& nums) {\n\n vector<string> arr;\n\n for(auto& it: nums){\n arr.push_back(to_string(it));\n }\n\n sort(arr.begin() , arr.end() , mycomp);\n\n if(arr[0] == \"0\"){\n return \"0\";\n }\n\n string ans = \"\";\n\n for(auto it : arr){\n ans += it;\n }\n\n return ans;\n \n }\n};",
"memory": "17200"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n string largestNumber(vector<int>& nums) {\n vector<string> strNums;\n for (int num : nums) {\n strNums.push_back(to_string(num));\n }\n\n \n sort(strNums.begin(), strNums.end(), [](string &a, string &b) {\n return a + b > b + a;\n });\n\n \n if (strNums[0] == \"0\") \n return \"0\";\n\n string result;\n for (string &s : strNums) {\n result += s;\n }\n\n return result;\n }\n};",
"memory": "17300"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n string largestNumber(vector<int>& nums) {\n vector<string> str;\n for (auto x : nums) {\n str.push_back(to_string(x));\n }\n sort(str.begin(), str.end(), [](string &s1, string &s2){ return s1 + s2 > s2 + s1;});\n string ans;\n for (int i = 0; i < str.size(); i++) {\n ans += str[i];\n }\n while(ans[0]=='0' && ans.length()>1)\n ans.erase(0,1);\n return ans;\n }\n};",
"memory": "17300"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n string largestNumber(vector<int>& nums) {\n vector<string> nums2;\n\n for(int n : nums){\n nums2.push_back(to_string(n));\n }\n\n sort(nums2.begin(),nums2.end(), [](string &a, string &b) {\n return a + b > b + a;\n });\n\n string res=\"\";\n for(int i=0;i<nums2.size(); i++){\n res+=nums2[i];\n }\n\n if(res[0]== '0'){\n return \"0\";\n }\n\n return res;\n }\n};",
"memory": "17400"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\n static bool mycomp(string a, string b){\n return a+b > b+a;\n }\n\n string largestNumber(vector<int>& nums) {\n vector<string> nums2;\n\n for(int n : nums){\n nums2.push_back(to_string(n));\n }\n\n sort(nums2.begin(),nums2.end(), mycomp);\n\n if(nums2[0]== \"0\"){\n return \"0\";\n }\n\n string res=\"\";\n for(int i=0;i<nums2.size(); i++){\n res+=nums2[i];\n }\n\n return res;\n }\n};",
"memory": "17400"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string largestNumber(vector<int>& nums) {\n vector<string> nums2;\n\n for(int n : nums){\n nums2.push_back(to_string(n));\n cout<<n<<endl;\n }\n cout<<endl;\n\n sort(nums2.begin(),nums2.end(), [](string &a, string &b) {\n return a + b > b + a;\n });\n\n string res=\"\";\n for(int i=0;i<nums2.size(); i++){\n res+=nums2[i];\n cout<<nums2[i]<<endl;\n }\n\n if(res[0]== '0'){\n return \"0\";\n }\n\n return res;\n }\n};",
"memory": "17500"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n static bool mycomp(string a, string b){\n string t1 = a+b;\n string t2 = b+a;\n return t1>t2;\n }\n string largestNumber(vector<int>& nums) {\n vector<string>snums;\n for(auto n:nums){\n snums.push_back(to_string(n));\n }\n sort(snums.begin(),snums.end(),mycomp);//lexographical sorting\n if(snums[0]==\"0\") return \"0\";\n\n string ans = \"\";\n for(auto str:snums){\n ans += str;\n }\n return ans;\n }\n};",
"memory": "17500"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string largestNumber(vector<int>& nums) \n {\n string ans = \"\";\n vector<string>arr;\n\n for(int i = 0 ; i < nums.size() ; i++)\n arr.push_back(to_string(nums[i]));\n\n sort(arr.rbegin() , arr.rend() , [&](auto const &a , auto const &b ){\n \n\n return a+b < b+a;\n\n\n // int s1 = 0; \n // while(s1<a.size() && s1 < b.size())\n // {\n // if(a[s1] < b[s1] )\n // return 1;\n\n // s1++;\n // }\n\n // if(s1 == a.size())\n // {\n // while(s1<b.size())\n // {\n // if(b[s1] != '0')\n // return 1;\n // s1++;\n // }\n\n // return 0;\n // }\n\n // else\n // {\n // while(s1 < a.size())\n // {\n // if(a[s1] != '0')\n // return 0;\n // s1++;\n // }\n\n // return 1; \n // }\n } );\n\n for(int i = 0 ; i < nums.size() ; i++)\n {\n if(ans.size() == 0 && arr[i] == \"0\")\n continue;\n\n ans+=arr[i];\n }\n\n if(ans.size() == 0)\n return \"0\";\n return ans;\n\n }\n};",
"memory": "17600"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string largestNumber(vector<int>& nums) {\n\n std::vector<std::string> stringNums(nums.size());\n std::transform(nums.begin(), nums.end(),\n stringNums.begin(), \n [](int num) {\n return std::to_string(num);\n }\n );\n\n std::sort(stringNums.begin(), stringNums.end(), [](const string& s1, const string& s2) {\n return s1 + s2 > s2 + s1;\n });\n \n if (stringNums[0] == \"0\") {\n return \"0\";\n }\n\n std::string result;\n size_t length = std::accumulate(stringNums.begin(), stringNums.end(), \n 0,\n [](size_t acc, const string& num) {\n return acc + num.length();\n }\n );\n result.reserve(length);\n return std::accumulate(stringNums.begin(), stringNums.end(), \n std::move(result), \n [](std::string acc, const string& num) {\n return acc + num;\n }\n );\n }\n};\n",
"memory": "17700"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string largestNumber(vector<int>& nums) {\n\n std::vector<std::string> stringNums(nums.size());\n std::transform(nums.begin(), nums.end(),\n stringNums.begin(), \n [](int num) {\n return std::to_string(num);\n }\n );\n\n std::sort(stringNums.begin(), stringNums.end(), [](const string& s1, const string& s2) {\n return s1 + s2 > s2 + s1;\n });\n \n if (stringNums[0] == \"0\") {\n return \"0\";\n }\n\n std::string result;\n result.reserve(\n std::accumulate(stringNums.begin(), stringNums.end(), \n 0,\n [](size_t acc, const string& num) { return acc + num.length();}\n )\n );\n return std::accumulate(stringNums.begin(), stringNums.end(), \n std::move(result), \n [](std::string acc, const string& num) {\n return acc + num;\n }\n );\n }\n};\n",
"memory": "17700"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string largestNumber(vector<int>& nums) {\n vector<string> arr;\n for(auto i:nums){\n arr.push_back(to_string(i));\n }\n sort(arr.begin(), arr.end(), [=]( string &a, string &b){\n // tricky part\n return a+b > b+a;\n });\n if(arr[0] == \"0\"){\n return \"0\";\n }\n stringstream res;\n for(auto & n: arr){\n res << n;\n }\n return res.str();\n }\n};",
"memory": "17800"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n \n static bool cmp(string a,string b){\n return a+b>b+a;\n }\n\n string largestNumber(vector<int>& nums) {\n int n=nums.size();\n int sum=0;\n vector<string>v(n);\n for(int i=0;i<n;i++){\n v[i]=to_string(nums[i]);\n \n }\n int count=0;\n for(int i=0;i<n;i++){\n if(v[i]==\"0\"){\n count++;\n }\n }\n if(count==n){\n return \"0\";\n }\n sort(v.begin(),v.end(),cmp);\n string ans=\"\";\n for(int i=0;i<n;i++){\n ans=ans+v[i];\n }\n return ans;\n \n }\n};",
"memory": "17900"
} |
179 | <p>Given a list of non-negative integers <code>nums</code>, arrange them such that they form the largest number and return it.</p>
<p>Since the result may be very large, so you need to return a string instead of an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,2]
<strong>Output:</strong> "210"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,30,34,5,9]
<strong>Output:</strong> "9534330"
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n static std::string largestNumber(std::vector<int> &nums) {\n\n std::vector<std::string> stringNums;\n stringNums.reserve(nums.size());\n std::transform(nums.begin(), nums.end(), std::back_inserter(stringNums),\n [](int num) {\n return std::to_string(num);\n }\n );\n\n std::sort(stringNums.begin(), stringNums.end(), [](const std::string &s1, const std::string &s2) {\n return s1 + s2 > s2 + s1;\n });\n\n if (stringNums[0] == \"0\") {\n return \"0\";\n }\n\n std::string result;\n result.reserve(\n std::accumulate(stringNums.begin(), stringNums.end(), 0,\n [](size_t acc, const std::string &num) {\n return acc + num.length();\n }\n )\n );\n return std::accumulate(stringNums.begin(), stringNums.end(), std::move(result),\n [](const std::string &acc, const std::string &num) {\n return acc + num;\n }\n );\n }\n};\n",
"memory": "17900"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n std::vector<std::string>\n findRepeatedDnaSequences(const std::string_view s) {\n if (s.size() < 10u)\n return {};\n\n // Store sequence of 10 symbols in single 64-bit number.\n // Each symbol uses 5 bits.\n\n // Prepare initial string representation.\n uint64_t code =\n (uint64_t(s[0] - 'A') << 00u) | (uint64_t(s[1] - 'A') << 05u) |\n (uint64_t(s[2] - 'A') << 10u) | (uint64_t(s[3] - 'A') << 15u) |\n (uint64_t(s[4] - 'A') << 20u) | (uint64_t(s[5] - 'A') << 25u) |\n (uint64_t(s[6] - 'A') << 30u) | (uint64_t(s[7] - 'A') << 35u) |\n (uint64_t(s[8] - 'A') << 40u) | (uint64_t(s[9] - 'A') << 45u);\n\n // Collect all codes into vector.\n std::vector<uint64_t> codes;\n codes.reserve(s.size() - 9u);\n\n // Use sliding window approach.\n for (size_t i = 10u; i < s.size(); ++i) {\n codes.push_back(code);\n code = (code >> 5u) | (uint64_t(s[i] - 'A') << 45u);\n }\n if (s.size() > 10u)\n codes.push_back(code);\n\n // Sort codes to group identical codes.\n std::sort(codes.begin(), codes.end());\n\n std::vector<std::string> res;\n // Iterate over sorted codes, search for duplicates.\n auto it = codes.begin();\n while (it != codes.end()) {\n auto next_it = std::next(it);\n if (next_it == codes.end())\n break;\n while (next_it != codes.end() && *next_it == *it)\n ++next_it;\n\n if (std::next(it) < next_it) {\n // Convert compact representation back into string.\n std::string sequence;\n sequence.resize(10u);\n const uint64_t code = *it;\n for (uint64_t i = 0; i < 10u; ++i)\n sequence[i] = 'A' + ((code >> (i * 5u)) & 31u);\n res.emplace_back(sequence);\n }\n\n it = next_it;\n }\n return res;\n }\n};",
"memory": "9971"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\n /*inline int DNAInt(char l)\n {\n switch (l)\n {\n case 'A':\n return 0;\n case 'C':\n return 1;\n case 'G':\n return 2;\n case 'T':\n default:\n return 3;\n }\n }*/\n\n struct HashTrack\n {\n bool seen = false;\n bool repeat = false;\n };\n\n vector<string> findRepeatedDnaSequences(string s) {\n constexpr int NUMHASHES = 4 * 4 * 4 * 4 * 4 * 4 * 4 * 4 * 4 * 4;\n\n int n = s.length();\n if (n <= 10)\n return vector<string>();\n \n HashTrack hashes[NUMHASHES] {};\n int DNAInt[85];\n DNAInt['A'] = 0;\n DNAInt['C'] = 1;\n DNAInt['G'] = 2;\n DNAInt['T'] = 3;\n\n int hash = 0;\n for (int i = 0; i < 10; ++i)\n {\n hash <<= 2;\n hash += DNAInt[s[i]];\n }\n hashes[hash].seen = true;\n\n vector<string> result;\n for (int i = 10; i < n; ++i)\n {\n hash &= 0b00111111111111111111;\n hash <<= 2;\n hash += DNAInt[s[i]];\n if (hashes[hash].seen && !hashes[hash].repeat)\n {\n result.emplace_back(s.substr(i - 9, 10));\n hashes[hash].repeat = true;\n }\n else\n hashes[hash].seen = true;\n }\n\n return result;\n }\n};",
"memory": "10203"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n struct HashTrack\n {\n bool seen = false;\n bool repeat = false;\n };\n\n vector<string> findRepeatedDnaSequences(string s) {\n constexpr int NUMHASHES = 4 * 4 * 4 * 4 * 4 * 4 * 4 * 4 * 4 * 4;\n constexpr int MASK = NUMHASHES - 1;\n\n int n = s.length();\n if (n <= 10)\n return vector<string>();\n \n HashTrack hashes[NUMHASHES] {};\n int DNAInt[85];\n DNAInt['A'] = 0;\n DNAInt['C'] = 1;\n DNAInt['G'] = 2;\n DNAInt['T'] = 3;\n\n int hash = 0;\n for (int i = 0; i < 10; ++i)\n {\n hash = (hash << 2) + DNAInt[s[i]];\n }\n hashes[hash].seen = true;\n\n vector<string> result;\n for (int i = 10; i < n; ++i)\n {\n hash = ((hash << 2) + DNAInt[s[i]]) & MASK;\n if (!hashes[hash].repeat && hashes[hash].seen)\n {\n result.emplace_back(s.substr(i - 9, 10));\n hashes[hash].repeat = true;\n }\n else\n hashes[hash].seen = true;\n }\n\n return result;\n }\n};",
"memory": "10203"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\n inline int DNAInt(char l)\n {\n switch (l)\n {\n case 'A':\n return 0;\n case 'C':\n return 1;\n case 'G':\n return 2;\n case 'T':\n default:\n return 3;\n }\n }\n\n inline string IntDNA(int hash)\n {\n std::string dna;\n dna.resize(10);\n for (int i = 9; i >= 0; --i)\n {\n switch (hash % 4)\n {\n case 0:\n dna[i] = 'A';\n break;\n case 1:\n dna[i] = 'C';\n break;\n case 2:\n dna[i] = 'G';\n break;\n case 3:\n default:\n dna[i] = 'T';\n break;\n }\n\n hash >>= 2;\n }\n return dna;\n }\n\n struct HashTrack\n {\n bool seen = false;\n bool repeat = false;\n };\n\n vector<string> findRepeatedDnaSequences(string s) {\n constexpr int NUMHASHES = 4 * 4 * 4 * 4 * 4 * 4 * 4 * 4 * 4 * 4;\n HashTrack hashes[NUMHASHES] {};\n //std::unordered_set<int> repeats;\n int n = s.length();\n\n int hash = 0;\n for (int i = 0; i < 10; ++i)\n {\n hash <<= 2;\n hash += DNAInt(s[i]);\n }\n hashes[hash].seen = true;\n\n vector<string> result;\n for (int i = 10; i < n; ++i)\n {\n hash &= 0b00111111111111111111;\n hash <<= 2;\n hash += DNAInt(s[i]);\n if (hashes[hash].seen && !hashes[hash].repeat)//repeats.emplace(hash).second)\n {\n result.emplace_back(s.substr(i - 9, 10));\n hashes[hash].repeat = true;\n }\n else\n hashes[hash].seen = true;\n }\n\n return result;\n }\n};",
"memory": "10434"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\n inline int DNAInt(char l)\n {\n switch (l)\n {\n case 'A':\n return 0;\n case 'C':\n return 1;\n case 'G':\n return 2;\n case 'T':\n default:\n return 3;\n }\n }\n\n inline string IntDNA(int hash)\n {\n std::string dna;\n dna.resize(10);\n for (int i = 9; i >= 0; --i)\n {\n switch (hash % 4)\n {\n case 0:\n dna[i] = 'A';\n break;\n case 1:\n dna[i] = 'C';\n break;\n case 2:\n dna[i] = 'G';\n break;\n case 3:\n default:\n dna[i] = 'T';\n break;\n }\n\n hash >>= 2;\n }\n return dna;\n }\n\n struct HashTrack\n {\n bool seen = false;\n bool repeat = false;\n };\n\n vector<string> findRepeatedDnaSequences(string s) {\n constexpr int NUMHASHES = 4 * 4 * 4 * 4 * 4 * 4 * 4 * 4 * 4 * 4;\n HashTrack hashes[NUMHASHES] {};\n //std::unordered_set<int> repeats;\n int n = s.length();\n\n int hash = 0;\n for (int i = 0; i < 10; ++i)\n {\n hash <<= 2;\n hash += DNAInt(s[i]);\n }\n hashes[hash].seen = true;\n\n vector<string> result;\n for (int i = 10; i < n; ++i)\n {\n hash &= 0b00111111111111111111;\n hash <<= 2;\n hash += DNAInt(s[i]);\n if (hashes[hash].seen && !hashes[hash].repeat)//repeats.emplace(hash).second)\n {\n result.emplace_back(s.substr(i - 9, 10));\n hashes[hash].repeat = true;\n }\n else\n hashes[hash].seen = true;\n }\n\n return result;\n }\n};",
"memory": "10434"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\n /*inline int DNAInt(char l)\n {\n switch (l)\n {\n case 'A':\n return 0;\n case 'C':\n return 1;\n case 'G':\n return 2;\n case 'T':\n default:\n return 3;\n }\n }*/\n\n struct HashTrack\n {\n bool seen = false;\n bool repeat = false;\n };\n\n vector<string> findRepeatedDnaSequences(string s) {\n constexpr int NUMHASHES = 4 * 4 * 4 * 4 * 4 * 4 * 4 * 4 * 4 * 4;\n\n int n = s.length();\n if (n <= 10)\n return vector<string>();\n \n HashTrack hashes[NUMHASHES] {};\n int DNAInt[85];\n DNAInt['A'] = 0;\n DNAInt['C'] = 1;\n DNAInt['G'] = 2;\n DNAInt['T'] = 3;\n\n int hash = 0;\n for (int i = 0; i < 10; ++i)\n {\n hash <<= 2;\n hash += DNAInt[s[i]];\n }\n hashes[hash].seen = true;\n\n vector<string> result;\n for (int i = 10; i < n; ++i)\n {\n hash &= 0b00111111111111111111;\n hash <<= 2;\n hash += DNAInt[s[i]];\n if (hashes[hash].seen && !hashes[hash].repeat)\n {\n result.emplace_back(s.substr(i - 9, 10));\n hashes[hash].repeat = true;\n }\n else\n hashes[hash].seen = true;\n }\n\n return result;\n }\n};",
"memory": "10665"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\n /*inline int DNAInt(char l)\n {\n switch (l)\n {\n case 'A':\n return 0;\n case 'C':\n return 1;\n case 'G':\n return 2;\n case 'T':\n default:\n return 3;\n }\n }*/\n\n struct HashTrack\n {\n bool seen = false;\n bool repeat = false;\n };\n\n vector<string> findRepeatedDnaSequences(string s) {\n constexpr int NUMHASHES = 4 * 4 * 4 * 4 * 4 * 4 * 4 * 4 * 4 * 4;\n\n int n = s.length();\n if (n <= 10)\n return vector<string>();\n \n HashTrack hashes[NUMHASHES] {};\n int DNAInt[85];\n DNAInt['A'] = 0;\n DNAInt['C'] = 1;\n DNAInt['G'] = 2;\n DNAInt['T'] = 3;\n\n int hash = 0;\n for (int i = 0; i < 10; ++i)\n {\n hash <<= 2;\n hash += DNAInt[s[i]];\n }\n hashes[hash].seen = true;\n\n vector<string> result;\n for (int i = 10; i < n; ++i)\n {\n hash &= 0b00111111111111111111;\n hash <<= 2;\n hash += DNAInt[s[i]];\n if (hashes[hash].seen && !hashes[hash].repeat)\n {\n result.emplace_back(s.substr(i - 9, 10));\n hashes[hash].repeat = true;\n }\n else\n hashes[hash].seen = true;\n }\n\n return result;\n }\n};",
"memory": "10665"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n unordered_set<string>v;\n vector<string>v1;\n if(s.length()<10 || s.length()==10 || s.length()>5000){\n return v1;\n }\n\n string str;\n int flag=0;\n for(int i=0;i<s.length()-9;i++){\n flag=0;\n str=\"\";\n for(int j=i;j<(i+10);j++){\n str=str+s[j];\n }\n \n if(v.find(str)!=v.end()){\n flag=1;\n }\n\n for(int j=0;j<v1.size();j++){\n if(v1[j]==str){\n flag=0;\n break;\n }\n }\n if(flag==1){\n v1.push_back(str);\n }\n v.insert(str);\n }\n return v1;\n }\n};",
"memory": "10896"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n vector<string>v;\n vector<string>v1;\n if(s.length()<10 || s.length()==10 || s.length()>5000){\n return v;\n }\n\n string str;\n int flag=0;\n for(int i=0;i<s.length()-9;i++){\n flag=0;\n str=\"\";\n for(int j=i;j<(i+10);j++){\n str=str+s[j];\n }\n for(int j=0;j<v.size();j++){\n if(v[j]==str){\n flag=1;\n v[j]=\"-\";\n break;\n }\n }\n for(int j=0;j<v1.size();j++){\n if(v1[j]==str){\n flag=0;\n break;\n }\n }\n if(flag==1){\n v1.push_back(str);\n }\n v.push_back(str);\n }\n return v1;\n }\n};",
"memory": "11128"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n std::vector<std::string>\n findRepeatedDnaSequences(const std::string_view s) {\n if (s.size() < 10u)\n return {};\n\n // Store sequence of 10 symbols in single 64-bit number.\n // Each symbol uses 5 bits.\n\n // Prepare initial string representation.\n uint64_t code =\n (uint64_t(s[0] - 'A') << 00u) | (uint64_t(s[1] - 'A') << 05u) |\n (uint64_t(s[2] - 'A') << 10u) | (uint64_t(s[3] - 'A') << 15u) |\n (uint64_t(s[4] - 'A') << 20u) | (uint64_t(s[5] - 'A') << 25u) |\n (uint64_t(s[6] - 'A') << 30u) | (uint64_t(s[7] - 'A') << 35u) |\n (uint64_t(s[8] - 'A') << 40u) | (uint64_t(s[9] - 'A') << 45u);\n\n // Collect all codes into vector.\n std::vector<uint64_t> codes;\n\n // Use sliding window approach.\n for (size_t i = 10u; i < s.size(); ++i) {\n codes.push_back(code);\n code = (code >> 5u) | (uint64_t(s[i] - 'A') << 45u);\n }\n if (s.size() > 10u)\n codes.push_back(code);\n\n // Sort codes to group identical codes.\n std::sort(codes.begin(), codes.end());\n\n std::vector<std::string> res;\n // Iterate over sorted codes, search for duplicates.\n auto it = codes.begin();\n while (it != codes.end()) {\n auto next_it = std::next(it);\n if (next_it == codes.end())\n break;\n while (next_it != codes.end() && *next_it == *it)\n ++next_it;\n\n if (std::next(it) < next_it) {\n // Convert compact representation back into string.\n std::string sequence;\n sequence.resize(10u);\n const uint64_t code = *it;\n for (uint64_t i = 0; i < 10u; ++i)\n sequence[i] = 'A' + ((code >> (i * 5u)) & 31u);\n res.emplace_back(sequence);\n }\n\n it = next_it;\n }\n return res;\n }\n};",
"memory": "11128"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n std::vector<std::string>\n findRepeatedDnaSequences(const std::string_view s) {\n if (s.size() < 10u)\n return {};\n\n // Store sequence of 10 symbols in single 64-bit number.\n // Each symbol uses 5 bits.\n\n // Prepare initial string representation.\n uint64_t code =\n (uint64_t(s[0] - 'A') << 00u) | (uint64_t(s[1] - 'A') << 05u) |\n (uint64_t(s[2] - 'A') << 10u) | (uint64_t(s[3] - 'A') << 15u) |\n (uint64_t(s[4] - 'A') << 20u) | (uint64_t(s[5] - 'A') << 25u) |\n (uint64_t(s[6] - 'A') << 30u) | (uint64_t(s[7] - 'A') << 35u) |\n (uint64_t(s[8] - 'A') << 40u) | (uint64_t(s[9] - 'A') << 45u);\n\n std::vector<uint64_t> codes;\n\n // Use sliding window approach.\n for (size_t i = 10u; i < s.size(); ++i) {\n codes.push_back(code);\n code = (code >> 5u) | (uint64_t(s[i] - 'A') << 45u);\n }\n if (s.size() > 10u)\n codes.push_back(code);\n\n std::sort(codes.begin(), codes.end());\n\n std::vector<std::string> res;\n\n auto it= codes.begin();\n while(it != codes.end())\n {\n auto next_it= std::next(it);\n if(next_it == codes.end())\n break;\n while(next_it != codes.end() && *next_it == *it)\n ++next_it;\n\n if (it + 1 < next_it) {\n const uint64_t code= *it;\n // Convert compact representation back into string.\n std::string sequence;\n sequence.resize(10u);\n for (uint64_t i = 0; i < 10u; ++i)\n sequence[i] = 'A' + ((code >> (i * 5u)) & 31u);\n res.emplace_back(sequence);\n }\n\n it= next_it;\n }\n return res;\n }\n};",
"memory": "11359"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n\tvector<size_t> indices(s.size());\n iota(indices.begin(), indices.end(), 0);\n sort(indices.begin(), indices.end(), [&s](size_t i0, size_t i1) {\n while (i0 < s.size() && i1 < s.size()) {\n if (s[i0] < s[i1])\n return true;\n else if (s[i0] > s[i1])\n return false;\n ++i0;\n ++i1;\n }\n return i0 < s.size();\n });\n vector<string> result;\n size_t index = 0;\n while (index < indices.size() - 1) {\n auto other = index + 1;\n if (s.size() - indices[index] >= 10 && s.size() - indices[other] >= 10 && s.compare(indices[index], 10, s, indices[other], 10) == 0) {\n result.push_back(s.substr(indices[index], 10));\n ++other;\n while (other < indices.size() - 1 && s.size() - indices[other] >= 10 && s.compare(indices[index], 10, s, indices[other], 10) == 0)\n ++other;\n index = other;\n } else {\n ++index;\n }\n }\n return result;\n }\n};",
"memory": "11590"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n\tvector<size_t> indices(s.size());\n iota(indices.begin(), indices.end(), 0);\n sort(indices.begin(), indices.end(), [&s](size_t i0, size_t i1) {\n for (;;) {\n if (i0 == s.size())\n return i1 != s.size();\n if (i1 == s.size())\n return false;\n if (s[i0] < s[i1])\n return true;\n if (s[i0] > s[i1])\n return false;\n ++i0;\n ++i1;\n }\n });\n vector<string> result;\n size_t index = 0;\n while (index < indices.size() - 1) {\n auto other = index + 1;\n if (s.size() - indices[index] >= 10 && s.size() - indices[other] >= 10 && s.compare(indices[index], 10, s, indices[other], 10) == 0) {\n result.push_back(s.substr(indices[index], 10));\n ++other;\n while (other < indices.size() - 1 && s.size() - indices[other] >= 10 && s.compare(indices[index], 10, s, indices[other], 10) == 0)\n ++other;\n index = other;\n } else {\n ++index;\n }\n }\n return result;\n }\n};",
"memory": "11590"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n\tvector<size_t> indices(s.size());\n iota(indices.begin(), indices.end(), 0);\n sort(indices.begin(), indices.end(), [&s](size_t i0, size_t i1) {\n while (i0 < s.size() && i1 < s.size()) {\n if (s[i0] < s[i1])\n return true;\n else if (s[i0] > s[i1])\n return false;\n ++i0;\n ++i1;\n }\n return i1 < s.size();\n });\n vector<string> result;\n size_t index = 0;\n while (index < indices.size() - 1) {\n auto other = index + 1;\n if (s.size() - indices[index] >= 10 && s.size() - indices[other] >= 10 && s.compare(indices[index], 10, s, indices[other], 10) == 0) {\n result.push_back(s.substr(indices[index], 10));\n ++other;\n while (other < indices.size() - 1 && s.size() - indices[other] >= 10 && s.compare(indices[index], 10, s, indices[other], 10) == 0)\n ++other;\n index = other;\n } else {\n ++index;\n }\n }\n return result;\n }\n};",
"memory": "11821"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\n int hashVal(char c) {\n switch (c) {\n case 'A':\n return 0;\n case 'C':\n return 1;\n case 'G':\n return 1;\n case 'T':\n return 1;\n default:\n return 0;\n }\n }\n\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n if (s.length() < 10)\n return {};\n\n unordered_map<int, vector<int>> hash_map;\n unordered_set<string> result_set;\n\n int hash = 0;\n int base = 4;\n int window = 10;\n\n int highestbase = pow(base, window - 1);\n for (int i = 0; i < window; i++) {\n hash = hash * base + hashVal(s[i]);\n }\n hash_map[hash].push_back(0);\n\n for (int i = window; i < s.length(); i++) {\n hash = (hash - hashVal(s[i - window]) * highestbase) * base +\n hashVal(s[i]);\n if (hash_map.find(hash) != hash_map.end()) {\n\n for (int start_index : hash_map[hash]) {\n if (s.substr(start_index, window) ==\n s.substr(i - window + 1, window)) {\n result_set.insert(s.substr(i - window + 1, window));\n\n break;\n }\n }\n }\n hash_map[hash].push_back(i - window + 1);\n }\n\n vector<string> result(result_set.begin(), result_set.end());\n\n return result;\n }\n};",
"memory": "12053"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\n int hashVal(char c) {\n switch (c) {\n case 'A':\n return 0;\n case 'C':\n return 1;\n case 'G':\n return 1;\n case 'T':\n return 1;\n default:\n return 0;\n }\n }\n\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n if (s.length() < 10)\n return {};\n\n unordered_map<int, vector<int>> hash_map;\n unordered_set<string> result_set;\n\n int hash = 0;\n int base = 4;\n int window = 10;\n\n int highestbase = pow(base, window - 1);\n for (int i = 0; i < window; i++) {\n hash = hash * base + hashVal(s[i]);\n }\n hash_map[hash].push_back(0);\n\n for (int i = window; i < s.length(); i++) {\n hash = (hash - hashVal(s[i - window]) * highestbase) * base +\n hashVal(s[i]);\n if (hash_map.find(hash) != hash_map.end()) {\n\n for (int start_index : hash_map[hash]) {\n if (s.substr(start_index, window) ==\n s.substr(i - window + 1, window)) {\n result_set.insert(s.substr(i - window + 1, window));\n\n break;\n }\n }\n }\n hash_map[hash].push_back(i - window + 1);\n }\n\n vector<string> result(result_set.begin(), result_set.end());\n\n return result;\n }\n};",
"memory": "12053"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int nucToInt (char c) {\n return c == 'G' ? 2 : c == 'T' ? 3 : c != 'A';\n }\n string intToDNA(int intDNA) {\n string res = \"\";\n int currNuc;\n while (res.size() < 10) {\n currNuc = intDNA % 4;\n res += currNuc == 3 ? 'T' : currNuc == 2 ? 'G' : currNuc ? 'C' : 'A';\n intDNA >>= 2;\n }\n return res;\n }\n vector<string> findRepeatedDnaSequences(string s) {\n int len = s.size();\n if (len < 10) return {};\n uint32_t intDNA = 0, freq[1048575];\n for (int i = 0; i < 1048575; i++) freq[i] = 0;\n vector<string> res;\n for (int i = 0; i < 10; i++) {\n intDNA >>= 2;\n intDNA += nucToInt(s[i]) << 18;\n }\n freq[intDNA]++;\n for (int i = 10; i < len; i++) {\n intDNA >>= 2;\n intDNA += nucToInt(s[i]) << 18;\n freq[intDNA]++;\n if (freq[intDNA] == 2) {\n res.push_back(intToDNA(intDNA));\n }\n }\n return res;\n }\n};",
"memory": "12284"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int power_four(int n){\n if (n == 0) {\n return 1;\n }\n int ans = 1;\n for (int i = 0; i < n; i++) {\n ans *= 4;\n }\n return ans;\n\n }\n\n string idx2string(int n) {\n string s = \"\";\n for (int i = 9; i >= 0; i--) {\n int q = n / power_four(i);\n int r = n % power_four(i);\n n = r;\n switch(q) {\n case 0:\n s += 'A';\n break;\n case 1:\n s += 'C';\n break;\n case 2:\n s += 'G';\n break;\n case 3:\n s += 'T';\n break;\n default:\n break;\n }\n }\n std::reverse(s.begin(), s.end());\n return s;\n }\n\n int string2idx(const string& st) {\n int idx = 0;\n for (int i {0}; i < st.size(); i++) {\n switch(st[i]) {\n case 'A':\n idx += 0 * power_four(i);\n break;\n case 'C':\n idx += 1 * power_four(i);\n break;\n case 'G':\n idx += 2 * power_four(i);\n break;\n case 'T':\n idx += 3 * power_four(i);\n break;\n default:\n assert(true);\n }\n }\n return idx;\n\n }\n\n vector<string> findRepeatedDnaSequences(string s) {\n constexpr int FOUR_TO_TEN = 1048576;\n // std::cout << INT_MAX << \"\\n\";\n int freq[FOUR_TO_TEN] {};\n int n = s.size();\n int end = 9;\n\n for (; end < n; end++) {\n freq[string2idx(s.substr(end-9, 10))] += 1;\n //std::cout << s.substr(end-9, 10) << \"\\n\";\n //std::cout << string2idx(s.substr(end-9, 10)) << \"\\n\";\n //std::cout << idx2string(string2idx(s.substr(end-9, 10))) << \"\\n\";\n }\n\n vector<string> ans {};\n for (int i = 0; i < FOUR_TO_TEN; i++) {\n if (freq[i] > 1) {\n ans.push_back(idx2string(i));\n }\n }\n return ans;\n }\n};",
"memory": "12515"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n static constexpr char bases[5] = \"ACGT\";\n int power_four(int n){\n if (n == 0) {\n return 1;\n }\n int ans = 1;\n for (int i = 0; i < n; i++) {\n ans *= 4;\n }\n return ans;\n\n }\n\n string idx2string(int n) {\n char s[11] = {};\n for (int i = 9; i >= 0; i--) {\n int q = n / power_four(i);\n int r = n % power_four(i);\n n = r;\n s[i] = bases[q];\n }\n // std::reverse(s.begin(), s.end());\n return string(s);\n }\n\n int string2idx(const string& st) {\n int idx = 0;\n for (int i {0}; i < st.size(); i++) {\n switch(st[i]) {\n case 'A':\n idx += 0 * power_four(i);\n break;\n case 'C':\n idx += 1 * power_four(i);\n break;\n case 'G':\n idx += 2 * power_four(i);\n break;\n case 'T':\n idx += 3 * power_four(i);\n break;\n default:\n assert(true);\n }\n }\n return idx;\n\n }\n\n vector<string> findRepeatedDnaSequences(string s) {\n constexpr int FOUR_TO_TEN = 1048576;\n // std::cout << INT_MAX << \"\\n\";\n int freq[FOUR_TO_TEN] {};\n int n = s.size();\n int end = 9;\n\n for (; end < n; end++) {\n freq[string2idx(s.substr(end-9, 10))] += 1;\n //std::cout << s.substr(end-9, 10) << \"\\n\";\n //std::cout << string2idx(s.substr(end-9, 10)) << \"\\n\";\n //std::cout << idx2string(string2idx(s.substr(end-9, 10))) << \"\\n\";\n }\n\n vector<string> ans {};\n for (int i = 0; i < FOUR_TO_TEN; i++) {\n if (freq[i] > 1) {\n ans.push_back(idx2string(i));\n }\n }\n return ans;\n }\n};",
"memory": "12515"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n static constexpr char bases[5] = \"ACGT\";\n int power_four(int n){\n if (n == 0) {\n return 1;\n }\n int ans = 1;\n for (int i = 0; i < n; i++) {\n ans *= 4;\n }\n return ans;\n\n }\n\n string idx2string(int n) {\n char s[11] = {};\n for (int i = 9; i >= 0; i--) {\n int q = n / power_four(i);\n int r = n % power_four(i);\n n = r;\n s[i] = bases[q];\n }\n // std::reverse(s.begin(), s.end());\n return s;\n }\n\n int string2idx(const string& st) {\n int idx = 0;\n for (int i {0}; i < st.size(); i++) {\n switch(st[i]) {\n case bases[0]:\n idx += 0 * power_four(i);\n break;\n case bases[1]:\n idx += 1 * power_four(i);\n break;\n case bases[2]:\n idx += 2 * power_four(i);\n break;\n case bases[3]:\n idx += 3 * power_four(i);\n break;\n default:\n assert(true);\n }\n }\n return idx;\n\n }\n\n vector<string> findRepeatedDnaSequences(string s) {\n constexpr int FOUR_TO_TEN = 1048576;\n // std::cout << INT_MAX << \"\\n\";\n int freq[FOUR_TO_TEN] {};\n int n = s.size();\n int end = 9;\n\n for (; end < n; end++) {\n freq[string2idx(s.substr(end-9, 10))] += 1;\n //std::cout << s.substr(end-9, 10) << \"\\n\";\n //std::cout << string2idx(s.substr(end-9, 10)) << \"\\n\";\n //std::cout << idx2string(string2idx(s.substr(end-9, 10))) << \"\\n\";\n }\n\n vector<string> ans {};\n for (int i = 0; i < FOUR_TO_TEN; i++) {\n if (freq[i] > 1) {\n ans.push_back(idx2string(i));\n }\n }\n return ans;\n }\n};",
"memory": "12746"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n unordered_map<int, int> freq; \n vector<string> res; \n int curr = 0; \n for(int i = 0; i < s.length(); i++) {\n curr <<= 2; \n curr &= ~(~0 << 20); \n switch(s[i]) {\n case 'A': \n curr |= 0; \n break; \n case 'C': \n curr |= 1; \n break;\n case 'G': \n curr |= 2;\n break; \n case 'T':\n curr |= 3; \n break; \n }\n\n if(i >= 9) {\n freq[curr]++; \n if(freq[curr] == 2) res.push_back(s.substr(i - 9, 10)); \n }\n }\n\n return res; \n }\n};",
"memory": "12978"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "// Solution1:\n// sliding window to get all 10 letters long substring + hashmap\n// Time: O(N*L), O(N) for the number of substring, O(L) for the comparation of the substring\n// Space: O(N*L)\n\n// Solution2: rabin-karp\n// ACGT=>0,1,2,3, calculate the corresponding number of the substring, sliding window to achieve O(L) => O(1) \n// Time: O(N), Space: O(N)\n\n/*\nclass Solution {\n const int L = 10;\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n vector<string> ans;\n unordered_map<string, int> cnt;\n int n = s.length();\n for (int i = 0; i <= n - L; ++i) {\n string sub = s.substr(i, L);\n if (++cnt[sub] == 2) {\n ans.push_back(sub);\n }\n }\n return ans;\n }\n};\n*/\n\n#if 0\nclass Solution {\n const int L = 10;\n const int base = 4;\n const int lgBase = 2;\n char bin[26];\npublic:\n Solution() {\n bin['A'-'A']=0;\n bin['C'-'A']=1;\n bin['G'-'A']=2;\n bin['T'-'A']=3;\n }\n\n vector<string> findRepeatedDnaSequences(string s) {\n vector<string> ans;\n int n = s.length();\n if (n <= L) {\n return ans;\n }\n int x = 0;\n for (int i = 0; i < L; ++i) {\n x = (x*base) + bin[s[i]-'A'];\n }\n\n unordered_map<int, int> cnt;\n cnt[x]=1;\n for (int i = L; i < n; ++i) {\n // 2^20 < 2^31\n // [NOTE]: so easy to mix the binary and base operation\n // or value(new_string) = (value(old_string)*base+bin[i])-bin[i-L]*pow(base, L)\n x = (x * base) + bin[s[i]-'A'] - bin[s[i-L]-'A']*pow(base, L); \n if (++cnt[x] == 2) {\n ans.push_back(s.substr(i-L+1, L));\n }\n }\n return ans;\n }\n};\n#endif\n\n// optimization:we can use binary operation to accelerate the calculation because the base is 4\nclass Solution {\n const int L = 10;\n const int lgBase = 2;\n char bin[26];\n const int mask=(1 << (L * lgBase)) - 1;\npublic:\n Solution() {\n bin['A'-'A']=0;\n bin['C'-'A']=1;\n bin['G'-'A']=2;\n bin['T'-'A']=3;\n }\n\n vector<string> findRepeatedDnaSequences(string s) {\n vector<string> ans;\n int n = s.length();\n if (n <= L) {\n return ans;\n }\n int x = 0;\n unordered_map<int, int> cnt;\n for (int i = 0; i < L; ++i) {\n x = (x << lgBase) | bin[s[i]-'A'];\n }\n cnt[x]=1;\n for (int i = L; i < n; ++i) {\n // Here, we choose to mask the low 20 bits\n // great idea here !!!\n x = ((x << lgBase) | bin[s[i]-'A']) & mask;\n if (++cnt[x] == 2) {\n ans.push_back(s.substr(i-L+1, L));\n }\n }\n return ans;\n }\n};",
"memory": "13209"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n if (s.length() < 10) {\n return {}; // If the string is too short, return an empty vector\n }\n\n unordered_set<int> seen; // To store the encoded sequences\n unordered_set<string> repeated; // To store the result sequences\n vector<string> result;\n\n int bitmask = 0;\n unordered_map<char, int> charToBits = {{'A', 0}, {'C', 1}, {'G', 2}, {'T', 3}};\n\n // Iterate through the string\n for (int i = 0; i < s.length(); ++i) {\n // Add current character's 2-bit representation to the bitmask\n bitmask = ((bitmask << 2) | charToBits[s[i]]) & ((1 << 20) - 1); // Keep only the last 20 bits\n \n if (i >= 9) { // We only start checking after we have at least 10 characters\n if (seen.find(bitmask) != seen.end()) {\n repeated.insert(s.substr(i - 9, 10)); // Insert the repeated sequence\n } else {\n seen.insert(bitmask);\n }\n }\n }\n\n // Convert set to vector for the result\n for (const auto& seq : repeated) {\n result.push_back(seq);\n }\n return result;\n }\n};",
"memory": "13440"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\nvector<string> findRepeatedDnaSequences(string s) {\n if (s.size() < 10) return {}; // Early return if string is too short\n \n unordered_map<int, int> sequenceCount; // To store the 20-bit encoded sequences and their counts\n vector<string> result;\n \n int bitmask = 0;\n unordered_map<char, int> charToBit = {{'A', 0}, {'C', 1}, {'G', 2}, {'T', 3}}; // Encoding: A=00, C=01, G=10, T=11\n \n // First, process the first 9 characters to set up the bitmask\n for (int i = 0; i < 9; ++i) {\n bitmask = (bitmask << 2) | charToBit[s[i]]; // Build the initial bitmask\n }\n \n // Now slide over the string and process each 10-letter sequence\n for (int i = 9; i < s.size(); ++i) {\n bitmask = ((bitmask << 2) | charToBit[s[i]]) & ((1 << 20) - 1); // Shift left, add new character, and keep the last 20 bits\n \n sequenceCount[bitmask]++; // Count occurrences of this 10-letter sequence\n \n // If this sequence has occurred exactly twice, add it to the result\n if (sequenceCount[bitmask] == 2) {\n result.push_back(s.substr(i - 9, 10)); // Add the 10-letter substring to the result\n }\n }\n \n return result;\n }\n};",
"memory": "13671"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\nprivate:\n \npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n int k = 10 , n = s.size();\n vector<string> arr;\n if(n < k)\n return arr;\n \n unordered_map<int,int> charToInt;\n unordered_set<string> ans;\n unordered_set<int>st;\n charToInt['A'-'A']=0;\n charToInt['C'-'A']=1;\n charToInt['G'-'A']=2;\n charToInt['T'-'A']=3;\n \n for(int i=0;i<=n-k;i++){\n int hash = 0;\n string temp;\n for(int j=i;j<i+k;j++){\n temp.push_back(s[j]);\n hash = hash<<2;\n // hash|=charToInt[s[j]-'A'];\n hash+=charToInt[s[j]-'A'];\n }\n if(st.find(hash)!=st.end()){\n ans.insert(temp);\n }\n st.insert(hash);\n }\n for(auto x : ans){\n arr.push_back(x);\n }\n return arr;\n }\n};",
"memory": "13903"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\nprivate:\n \npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n unordered_set<int> seen;\n unordered_set<int> dup;\n vector<string> result;\n vector<char> m(26);\n m['A' - 'A'] = 0;\n m['C' - 'A'] = 1;\n m['G' - 'A'] = 2;\n m['T' - 'A'] = 3;\n \n for (int i = 0; i + 10 <= s.size(); ++i) {\n string substr = s.substr(i, 10);\n int v = 0;\n for (int j = i; j < i + 10; ++j) { //20 bits < 32 bit int\n v <<= 2;\n v |= m[s[j] - 'A'];\n }\n if (seen.count(v) == 0) { //not seen\n seen.insert(v);\n } else if (dup.count(v) == 0) { //seen but not dup\n dup.insert(v);\n result.push_back(substr);\n } //dup\n }\n return result;\n }\n};",
"memory": "13903"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n unordered_map<int,int>mp; // hash value to count \n unordered_map<char,int>pair = {{'A', 0}, {'C', 1}, {'G', 2}, {'T', 3}};\n\n int hash_val = 0;\n int base = 4;\n int n = s.length();\n if(n < 10) return vector<string>();\n\n // Initialize the first hash value for the first 10-letter sequence\n int power = pow(4,9);\n for(int i = 0; i < 10; i++){\n int val = pair[s[i]];\n hash_val += (val*power);\n power /= 4;\n }\n \n mp[hash_val] = 1;\n unordered_set<string> ans;\n int highest_power = pow(base, 9); // 4^9\n \n // Rolling hash calculation for the remaining sequences\n for(int i = 1; i < n-10+1; i++){\n // Remove the first character and add the new character\n hash_val = (hash_val - pair[s[i-1]] * highest_power) * base + pair[s[i+9]];\n \n if(mp[hash_val] == 1) {\n ans.insert(s.substr(i, 10));\n }\n mp[hash_val]++;\n }\n \n vector<string> result(ans.begin(), ans.end());\n return result;\n }\n};\n",
"memory": "14134"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\n int getValue(char c) {\n int value;\n if(c=='A') value = 1;\n else if (c=='C') value = 2;\n else if (c=='G') value = 3;\n else value = 4;\n return value;\n }\n int calculateHash(string s, int start, int end) {\n int hash = 0;\n for(int i=start; i<=end; i++) {\n int value=getValue(s[i]);\n hash += value * pow(5, end-i);\n }\n return hash;\n }\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n vector<string> res;\n int len = s.length();\n int k = 10;\n if(len<k) {\n return res;\n }\n\n int previousHash = calculateHash(s, 0, k-1);\n unordered_map<int,int> m;\n m[previousHash] = 1;\n\n\n for(int i=k; i<len; i++) {\n int prevCharValue = getValue(s[i-k]);\n int nextCharValue = getValue(s[i]);\n int newHash = (previousHash - (prevCharValue * pow(5,k-1))) * 5 + nextCharValue;\n if(m.find(newHash)!= m.end()) {\n if(m[newHash]==1) {\n res.push_back(s.substr(i-k+1, k));\n }\n m[newHash]++;\n } else {\n m[newHash] = 1;\n }\n previousHash = newHash;\n }\n return res;\n }\n};",
"memory": "14365"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\n int base=5;\n int getValue(char c) {\n int value;\n if(c=='A') value = 1;\n else if (c=='C') value = 2;\n else if (c=='G') value = 3;\n else value = 4;\n return value;\n }\n int calculateHash(string s, int start, int end) {\n int hash = 0;\n for(int i=start; i<=end; i++) {\n int value=getValue(s[i]);\n hash += value * pow(base, end-i);\n }\n return hash;\n }\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n vector<string> res;\n int len = s.length();\n int k = 10;\n if(len<k) {\n return res;\n }\n\n int previousHash = calculateHash(s, 0, k-1);\n unordered_map<int,int> m;\n m[previousHash] = 1;\n\n\n for(int i=k; i<len; i++) {\n int prevCharValue = getValue(s[i-k]);\n int nextCharValue = getValue(s[i]);\n int newHash = (previousHash - (prevCharValue * pow(base,k-1))) * base + nextCharValue;\n if(m.find(newHash)!= m.end()) {\n if(m[newHash]==1) {\n res.push_back(s.substr(i-k+1, k));\n }\n m[newHash]++;\n } else {\n m[newHash] = 1;\n }\n previousHash = newHash;\n }\n return res;\n }\n};",
"memory": "14365"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n vector<string> res;\n int n=s.length();\n\n int label[n];\n for(int i=0;i<n;i++){\n if(s[i]=='A') label[i]=0; //00\n else if(s[i]=='C') label[i]=1; //01\n else if(s[i]=='G') label[i]=2; //10\n else label[i]=3; //11\n }\n\n long long int hash_val=0;\n unordered_map<int,int>mp;\n for(int i=0;i<n-9;i++){\n if(i==0){\n for(int i=0;i<10;i++){\n hash_val= (hash_val<<2) + label[i]; //converting to decimal number \n }\n mp[hash_val]++;\n }\n else{\n // Finding new hash value \n hash_val = (hash_val<<2) - (label[i - 1]<<20) + label[i + 9]; \n mp[hash_val]++;\n if(mp[hash_val]==2){\n res.push_back(s.substr(i,10));\n }\n }\n }\n return res;\n }\n};",
"memory": "14596"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "//rolling hash - rabin carp\n// class Solution {\n// public:\n// vector<string> findRepeatedDnaSequences(string s) {\n// if(s.length() <= 10) return {};\n// unordered_set<long long> uset;\n// vector<int> prime(26);\n// prime[0] = 1;\n// prime['C' - 'A'] = 2;\n// prime['G' - 'A'] = 3;\n// prime['T' - 'A'] = 5;\n// long long hash = 0;\n// for(int i = 0; i < 10; i++) {\n// hash = (hash*4) + prime[s[i] - 'A']; //\n// cout<<hash<<\" \";\n// }\n \n// uset.insert(hash);\n// unordered_set<string> result;\n// for(int st = 0, en = 10; en < s.length(); st++, en++) {\n// //out\n// //hash = hash - 4^(en-st-1);//pow(4, (en-st-1)); //\n// //in\n// //hash = (hash*4) + prime[s[en] - 'A']; //\n// double pw=pow(4,10);\n// hash = hash * 4 - prime[s[st] - 'A'] * pw + prime[s[en] - 'A'];\n// cout<<hash<<\" \";\n// if(uset.find(hash) != uset.end()) {\n// result.insert(s.substr(st+1, 10));\n \n// }\n// else {\n// uset.insert(hash);\n// }\n// }\n// return vector<string>(result.begin(), result.end());\n// }\n// };\n\n\n\nclass Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n vector<string> st;\n double pw=pow(4,10);\n int n=s.length();\n int li[n];\n for(int i=0;i<n;i++){\n if(s[i]=='A'){\n li[i]=0;\n }\n else if(s[i]=='T'){\n li[i]=1;\n }\n else if(s[i]=='G'){\n li[i]=2;\n }\n else{\n li[i]=3;\n }\n }\n long long int hash_val=0;\n unordered_map<int,int>mp;\n for(int i=0;i<n-9;i++){\n if(i==0){\n for(int i=0;i<10;i++){\n hash_val= 4*hash_val + li[i]; //converting to decimal number \n }\n mp[hash_val]++;\n }\n else{\n hash_val = hash_val * 4 - li[i - 1] * pw + li[i + 9]; // Finding new hash value \n mp[hash_val]++;\n if(mp[hash_val]==2){\n st.push_back(s.substr(i,10));\n }\n }\n }\n return st;\n }\n};\n",
"memory": "14828"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n int L = 10, n = s.size();\n if (n <= L) return vector<string>();\n\n // rolling hash parameters: base a\n int a = 4, aL = pow(a, L);\n\n // convert string to array of integers\n unordered_map<char, int> to_int = {\n {'A', 0}, {'C', 1}, {'G', 2}, {'T', 3}};\n vector<int> nums(n);\n for (int i = 0; i < n; ++i) nums[i] = to_int[s[i]];\n\n int h = 0;\n unordered_set<int> seen;\n unordered_set<string> output;\n // iterate over all sequences of length L\n for (int start = 0; start < n - L + 1; ++start) {\n // compute hash of the current sequence in O(1) time\n if (start != 0)\n h = h * a - nums[start - 1] * aL + nums[start + L - 1];\n\n // compute hash of the first sequence in O(L) time\n else\n for (int i = 0; i < L; ++i) h = h * a + nums[i];\n\n // update output and hashset of seen sequences\n if (seen.find(h) != seen.end()) output.insert(s.substr(start, L));\n seen.insert(h);\n }\n return vector<string>(output.begin(), output.end());\n }\n};",
"memory": "15059"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n unordered_set<string> output;\n int L = 10;\n unordered_set<int> seen;\n if(s.length()<=10) return {};\n unordered_map<char,int> to_int = {{'A',1},{'C',2},{'G',3},{'T',4}};\n vector<int> nums(s.length());\n for(int i=0;i<s.length();i++) nums[i] = to_int[s[i]];\n int hash = 0;\n int base = 4;\n int aL = pow(base, L);\n for(int i=0;i<=s.length()-L;i++){\n if(i!=0){\n hash = hash*base - nums[i-1]*aL + nums[i+L-1];\n }\n else {\n for (int j = 0; j < L; j++) hash = hash * base + nums[j];\n }\n if (seen.find(hash) != seen.end()) output.insert(s.substr(i, L));\n seen.insert(hash);\n }\n return vector<string>(output.begin(), output.end());\n \n }\n};",
"memory": "15290"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n int L = 10, n = s.size();\n if (n <= L) return vector<string>();\n\n // rolling hash parameters: base a\n int a = 4, aL = pow(a, L);\n\n // convert string to array of integers\n unordered_map<char, int> to_int = {\n {'A', 0}, {'C', 1}, {'G', 2}, {'T', 3}};\n vector<int> nums(n);\n for (int i = 0; i < n; ++i) nums[i] = to_int[s[i]];\n\n int h = 0;\n unordered_set<int> seen;\n unordered_set<string> output;\n // iterate over all sequences of length L\n for (int start = 0; start < n - L + 1; ++start) {\n // compute hash of the current sequence in O(1) time\n if (start != 0)\n h = h * a - nums[start - 1] * aL + nums[start + L - 1];\n\n // compute hash of the first sequence in O(L) time\n else\n for (int i = 0; i < L; ++i) h = h * a + nums[i];\n\n // update output and hashset of seen sequences\n if (seen.find(h) != seen.end()) output.insert(s.substr(start, L));\n seen.insert(h);\n }\n return vector<string>(output.begin(), output.end());\n }\n};",
"memory": "15290"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "#include <algorithm> // for find\n#include <cstddef> // for size_t\n#include <iterator> // for distance\n#include <string> // for basic_string, allocator, string, char_traits, hash\n#include <vector> // for vector\n\nclass Solution {\npublic:\n\tstd::vector<std::string> findRepeatedDnaSequences(std::string s) // NOLINT\n\t{\n\t\tstd::set<std::size_t> hashes;\n\t\tstd::set<std::string> result;\n\t\tfor (auto begin = s.cbegin(); begin != s.cend(); ++begin) {\n\t\t\tif (std::distance(begin, s.cend()) < 10) {\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tstd::string substr = {begin, begin + 10};\n\t\t\tauto hash = hashfunc(substr);\n\t\t\tif (!hashes.insert(hash).second) {\n\t\t\t\tresult.insert(substr);\n\t\t\t}\n\t\t}\n\n std::vector<std::string> data;\n data.reserve(result.size());\n std::move(result.cbegin(), result.cend(), std::back_inserter(data));\n\n\t\treturn data;\n\t}\nprivate:\n\tstd::hash<std::string> hashfunc{};\n};",
"memory": "15521"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n std::vector<std::string>\n findRepeatedDnaSequences(const std::string_view s) {\n if(s.size() < 10)\n return {};\n\n std::unordered_map<uint64_t, uint32_t> sequences;\n\n uint64_t code=\n (uint64_t(s[0] - 'A') << 0u) |\n (uint64_t(s[1] - 'A') << 5u) |\n (uint64_t(s[2] - 'A') << 10u) |\n (uint64_t(s[3] - 'A') << 15u) |\n (uint64_t(s[4] - 'A') << 20u) |\n (uint64_t(s[5] - 'A') << 25u) |\n (uint64_t(s[6] - 'A') << 30u) |\n (uint64_t(s[7] - 'A') << 35u) |\n (uint64_t(s[8] - 'A') << 40u) |\n (uint64_t(s[9] - 'A') << 45u);\n\n const uint64_t digits_mask= (uint64_t(1) << 50u) - 1u;\n for (size_t i = 10; i <= s.size(); ++i)\n {\n ++sequences[code];\n if(i < s.size())\n code= (code >> 5u) | (uint64_t(s[i] - 'A') << 45u);\n }\n std::vector<std::string> res;\n for (const auto& pair : sequences)\n if (pair.second > 1)\n {\n std::string sequence;\n sequence.resize(10);\n for(uint32_t i= 0 ;i < 10; ++i)\n sequence[i]= 'A' + ((pair.first >> (i * 5u)) & 31u);\n res.emplace_back(sequence);\n }\n return res;\n }\n};",
"memory": "17140"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n std::vector<std::string>\n findRepeatedDnaSequences(const std::string_view s) {\n if(s.size() < 10)\n return {};\n\n std::unordered_map<uint64_t, uint32_t> sequences;\n\n uint64_t code=\n (uint64_t(s[0] - 'A') << 0u) |\n (uint64_t(s[1] - 'A') << 5u) |\n (uint64_t(s[2] - 'A') << 10u) |\n (uint64_t(s[3] - 'A') << 15u) |\n (uint64_t(s[4] - 'A') << 20u) |\n (uint64_t(s[5] - 'A') << 25u) |\n (uint64_t(s[6] - 'A') << 30u) |\n (uint64_t(s[7] - 'A') << 35u) |\n (uint64_t(s[8] - 'A') << 40u) |\n (uint64_t(s[9] - 'A') << 45u);\n\n const uint64_t digits_mask= (uint64_t(1) << 50u) - 1u;\n for (size_t i = 10; i < s.size(); ++i)\n {\n ++sequences[code];\n code= (code >> 5u) | (uint64_t(s[i] - 'A') << 45u);\n }\n if(s.size() > 10)\n ++sequences[code];\n \n std::vector<std::string> res;\n for (const auto& pair : sequences)\n if (pair.second > 1)\n {\n std::string sequence;\n sequence.resize(10);\n for(uint32_t i= 0 ;i < 10; ++i)\n sequence[i]= 'A' + ((pair.first >> (i * 5u)) & 31u);\n res.emplace_back(sequence);\n }\n return res;\n }\n};",
"memory": "17140"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\n public:\n vector<string> findRepeatedDnaSequences(string s) {\n unordered_set<string> ans;\n unordered_set<string_view> seen;\n const string_view sv(s);\n\n for (int i = 0; i + 10 <= s.length(); ++i) {\n if (seen.contains(sv.substr(i, 10)))\n ans.insert(s.substr(i, 10));\n seen.insert(sv.substr(i, 10));\n }\n\n return {ans.begin(), ans.end()};\n }\n};",
"memory": "17371"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\n public:\n vector<string> findRepeatedDnaSequences(string s) {\n unordered_set<string> ans;\n unordered_set<string_view> seen;\n const string_view sv(s);\n\n for (int i = 0; i + 10 <= s.length(); ++i) {\n if (seen.contains(sv.substr(i, 10)))\n ans.insert(s.substr(i, 10));\n seen.insert(sv.substr(i, 10));\n }\n\n return {ans.begin(), ans.end()};\n }\n};",
"memory": "17603"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\n public:\n vector<string> findRepeatedDnaSequences(string s) {\n unordered_set<string> ans;\n unordered_set<string_view> seen;\n const string_view sv(s);\n\n for (int i = 0; i + 10 <= s.length(); ++i) {\n if (seen.contains(sv.substr(i, 10)))\n ans.insert(s.substr(i, 10));\n seen.insert(sv.substr(i, 10));\n }\n\n return {ans.begin(), ans.end()};\n }\n};",
"memory": "17834"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\n public:\n vector<string> findRepeatedDnaSequences(string s) {\n unordered_set<string> ans;\n unordered_set<string_view> seen;\n const string_view sv(s);\n\n for (int i = 0; i + 10 <= s.length(); ++i) {\n if (seen.contains(sv.substr(i, 10)))\n ans.insert(s.substr(i, 10));\n seen.insert(sv.substr(i, 10));\n }\n\n return {ans.begin(), ans.end()};\n }\n};",
"memory": "18065"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n if(s.length()<=10)\n return {};\n vector<string> subsets;\n vector<string> ans;\n for(int i=0;i<s.length()-9;i++)\n {\n string sub=s.substr(i,10);\n subsets.push_back(sub);\n }\n sort(subsets.begin(),subsets.end());\n int count=0;\n for(int i=0;i<subsets.size()-1;i++)\n {\n \n if((subsets[i]==subsets[i+1]))\n {\n count++;\n }\n else\n {\n if(count>0)\n {\n ans.push_back(subsets[i]);\n count=0;\n }\n }\n \n }\n if (count > 0) {\n ans.push_back(subsets.back());\n }\n \n return ans; \n }\n};",
"memory": "18296"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution \n{\npublic:\n vector<string> findRepeatedDnaSequences(string s) \n {\n vector<string> strs;\n vector<string> dna;\n\n if(s.size() <= 10)\n return {};\n\n for(int i=s.size()-10; i>=0; i--)\n strs.push_back(string(s.begin()+i, s.begin()+i+10));\n\n std::sort(strs.begin(), strs.end());\n for (int i = 1; i < strs.size(); i++) \n if (strs[i] == strs[i - 1]) \n if (dna.empty() || dna.back() != strs[i])\n dna.push_back(strs[i]);\n return dna;\n }\n};",
"memory": "18528"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution \n{\npublic:\n vector<string> findRepeatedDnaSequences(string s) \n {\n vector<string> strs;\n vector<string> dna;\n\n if(s.size() <= 10)\n return {};\n\n for(int i=s.size()-10; i>=0; i--)\n strs.push_back(string(s.begin()+i, s.begin()+i+10));\n\n std::sort(strs.begin(), strs.end());\n for (int i = 1; i < strs.size(); i++) \n if (strs[i] == strs[i - 1]) \n if (dna.empty() || dna.back() != strs[i])\n dna.push_back(strs[i]);\n return dna;\n }\n};",
"memory": "18759"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution \n{\npublic:\n vector<string> findRepeatedDnaSequences(string s) \n {\n vector<string> strs;\n vector<string> dna;\n\n if(s.size() <= 10)\n return {};\n\n for(int i=s.size()-10; i>=0; i--)\n strs.push_back(string(s.begin()+i, s.begin()+i+10));\n\n std::sort(strs.begin(), strs.end());\n for (int i = 1; i < strs.size(); i++) \n if (strs[i] == strs[i - 1]) \n if (dna.empty() || dna.back() != strs[i])\n dna.push_back(strs[i]);\n return dna;\n }\n};",
"memory": "18759"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n \n int L = 10;\n int str_length = s.length();\n vector<int> string_number;\n\n if(str_length < L) return vector<string>();\n\n int a = 4;\n int ha = pow(a, L);\n\n //convert the given string\n map<char, int> char_to_int {{'A', 0}, {'C', 1}, {'T', 2}, {'G', 3}};\n\n unordered_set<int> seen;\n unordered_set<string> output;\n int h = 0;\n\n for(auto& c : s)\n {\n string_number.push_back(char_to_int[c]);\n }\n\n //now itr over every sub string of length 10 \n for(int i = 0; i < str_length - L + 1; i++)\n {\n if(i == 0)\n {\n //the first computation\n for(int d = 0; d < L; d++)\n {\n h = h * a + string_number[d];\n }\n\n }else{\n\n //compute based on prev computation \n h = h * a - string_number[i - 1] * ha + string_number[i + L - 1];\n }\n\n if(seen.find(h) != seen.end()) output.insert(s.substr(i, L));\n seen.insert(h);\n }\n\n return vector<string>(output.begin(), output.end());\n\n }\n};",
"memory": "18990"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n unordered_set<string> v, ans;\n v.reserve(size(s));\n ans.reserve(size(s));\n for(int i = 0; i < size(s); ++i) {\n string cur = s.substr(i, 10);\n if(v.count(cur)) {\n ans.insert(cur);\n }\n v.insert(cur);\n }\n return vector<string>(begin(ans), end(ans));\n }\n};",
"memory": "19221"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "\nsize_t encode(char letter) {\n switch (letter) {\n case 'A':\n return 0x0;\n case 'C':\n return 0x1;\n case 'G':\n return 0x2;\n case 'T':\n return 0x3;\n }\n return 0;\n}\n\n// TODO should maybe be string view\nvoid find_and_insert(size_t num, size_t index_start, const std::string &s,\n std::unordered_map<size_t, int> &map,\n std::vector<std::string> &out) {\n auto found = map.find(num);\n if (found == std::end(map)) {\n auto emplace_out = map.emplace(num, 0);\n found = emplace_out.first;\n }\n found->second++;\n if (found->second == 2) {\n out.push_back(s.substr(index_start, 10));\n }\n}\n\nclass Solution {\npublic:\n std::vector<std::string> findRepeatedDnaSequences(std::string s) {\n std::unordered_map<size_t, int> set{};\n std::vector<std::string> out{};\n\n size_t end_len = s.size() >= 10 ? s.size() - 9 : 0;\n size_t prev = 0;\n if (end_len == 0) return out;\n for (size_t j = 0; j < 10; ++j) {\n\n prev |= encode(s[j]) << (j * 2);\n }\n find_and_insert(prev, 0, s, set, out);\n for (size_t i = 1; i < end_len; ++i) {\n prev = prev >> 2;\n\n size_t index_start = i;\n // encoding scheme A = 0x0 C = 0x1 G = 0x2 T = 0x3\n size_t num = prev | encode(s[index_start + 9]) << 18;\n prev = num;\n find_and_insert(num, index_start, s, set, out);\n // std::cout << num << std::endl;\n }\n return out;\n // iterate through string (10 letter slices; figure out math)\n // Convert into a number representation\n // throw into unordered map if not already in map. Also append to vector\n // Return a vector which has all the elements of the map\n }\n};\n",
"memory": "19453"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n long long bin_power_mod(long long base, long long power, long long mod){\n base%=mod;\n \n long long res = 1;\n\n while(power > 0){\n if(power & 1){\n res = res * base % mod;\n }\n base = base * base % mod;\n power>>=1;\n }\n\n return res;\n }\n\n long long modulo_inverse(long long base, long long mod){\n return bin_power_mod(base, mod-2, mod);\n }\n vector<string> findRepeatedDnaSequences(string s) {\n long long n = s.length();\n if(n<10) return {};\n long long mod = 1e9 + 7;\n\n vector<long long>hash(n);\n map<char, long long>mapping;\n map<long long, long long>cnt;\n mapping['A'] = 1;\n mapping['C'] = 2;\n mapping['G'] = 3;\n mapping['T'] = 4;\n\n hash[0] = mapping[s[0]];\n\n for(long long i=1; i<10; i++){ \n hash[i] = (((hash[i-1] * 5)%mod) + mapping[s[i]])%mod;\n }\n cnt[hash[9]]++;\n vector<string>ans;\n for(long long i=10; i<n; i++){\n hash[i] = ((((hash[i-1] - ((mapping[s[i-10]]*bin_power_mod(5, 9, mod)) % mod) + mod) % mod) * 5 ) % mod + mapping[s[i]]) % mod;\n if(cnt[hash[i]]==1){\n ans.push_back(s.substr(i-9, 10));\n } \n cnt[hash[i]]++;\n }\n\n return ans;\n }\n};\n\n",
"memory": "19684"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n // int findCount(const string& s, int low, int high, string& curr){\n // int ans = 0;\n // for(int i = low; i <= high; i++){\n // int j = 0;\n // int start = i;\n // while(j < curr.size() && start <= high && s[start] == curr[j]){\n // j++;\n // start++;\n // }\n // if(j == curr.size()) ans++;\n // }\n // return ans;\n // }\n // vector<string> findRepeatedDnaSequences(string s) {\n // vector <string> ans;\n // int n = s.size();\n // if(n < 10) return ans;\n // string curr = s.substr(0, 10);\n // set <string> covered;\n // int count = findCount(s, 0, n-1, curr);\n // if(count >= 2) ans.push_back(curr);\n // covered.insert(curr);\n // for(int i = 10; i <= n-1; i++){\n // curr = curr.substr(1) + s[i];\n // if(covered.find(curr) == covered.end()){\n // covered.insert(curr);\n // if(findCount(s, i-9, n-1, curr) >= 2) ans.push_back(curr);\n // }\n // }\n // return ans;\n // }\n\n vector<string> findRepeatedDnaSequences(string s){\n unordered_map <long long int, int> count;\n vector <string> ans;\n int n = s.size();\n if(n < 10) return ans;\n string curr;\n map <char, char> mp;\n mp['A'] = '0';\n mp['C'] = '1';\n mp['G'] = '2';\n mp['T'] = '3';\n for(int i = 0; i < 10; i++) curr += mp[s[i]];\n count[stoll(curr)]++;\n for(int i = 10; i < s.size(); i++){\n curr = curr.substr(1);\n curr += mp[s[i]];\n count[stoll(curr)]++;\n }\n map <char, int> rev_mp;\n rev_mp[0] = 'A';\n rev_mp[1] = 'C';\n rev_mp[2] = 'G';\n rev_mp[3] = 'T';\n for(auto &[num, freq]: count){\n cout << num << \" \";\n if(freq >= 2){\n string curr = to_string(num);\n string s;\n for(int i = 0; i < 10 - curr.size(); i++) s += \"A\";\n for(auto ele: curr){\n string num;\n num += ele;\n s += rev_mp[stoi(num)];\n }\n ans.push_back(s);\n }\n }\n cout << \"\\n\";\n return ans;\n }\n};\n",
"memory": "19915"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n // int findCount(const string& s, int low, int high, string& curr){\n // int ans = 0;\n // for(int i = low; i <= high; i++){\n // int j = 0;\n // int start = i;\n // while(j < curr.size() && start <= high && s[start] == curr[j]){\n // j++;\n // start++;\n // }\n // if(j == curr.size()) ans++;\n // }\n // return ans;\n // }\n // vector<string> findRepeatedDnaSequences(string s) {\n // vector <string> ans;\n // int n = s.size();\n // if(n < 10) return ans;\n // string curr = s.substr(0, 10);\n // set <string> covered;\n // int count = findCount(s, 0, n-1, curr);\n // if(count >= 2) ans.push_back(curr);\n // covered.insert(curr);\n // for(int i = 10; i <= n-1; i++){\n // curr = curr.substr(1) + s[i];\n // if(covered.find(curr) == covered.end()){\n // covered.insert(curr);\n // if(findCount(s, i-9, n-1, curr) >= 2) ans.push_back(curr);\n // }\n // }\n // return ans;\n // }\n\n vector<string> findRepeatedDnaSequences(string s){\n unordered_map <long long int, int> count;\n vector <string> ans;\n int n = s.size();\n if(n < 10) return ans;\n string curr;\n map <char, char> mp;\n mp['A'] = '0';\n mp['C'] = '1';\n mp['G'] = '2';\n mp['T'] = '3';\n for(int i = 0; i < 10; i++) curr += mp[s[i]];\n count[stoll(curr)]++;\n for(int i = 10; i < s.size(); i++){\n curr = curr.substr(1);\n curr += mp[s[i]];\n count[stoll(curr)]++;\n }\n map <char, int> rev_mp;\n rev_mp[0] = 'A';\n rev_mp[1] = 'C';\n rev_mp[2] = 'G';\n rev_mp[3] = 'T';\n for(auto &[num, freq]: count){\n cout << num << \" \";\n if(freq >= 2){\n string curr = to_string(num);\n string s;\n for(int i = 0; i < 10 - curr.size(); i++) s += \"A\";\n for(auto ele: curr){\n string num;\n num += ele;\n s += rev_mp[stoi(num)];\n }\n ans.push_back(s);\n }\n }\n cout << \"\\n\";\n return ans;\n }\n};\n",
"memory": "20146"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n const int p = 7, M = 1e9 + 9;\n vector<string> findRepeatedDnaSequences(string s) {\n long long n = s.size(), hash[n], p_pow[n];\n hash[0] = s[0] - 'A' + 1; p_pow[0] = 1;\n for (int i = 1; i < n; i++) {\n hash[i] = (hash[i - 1] * p % M + s[i] - 'A' + 1) % M;\n p_pow[i] = p_pow[i - 1] * p % M;\n }\n\n auto sub_hash = [&] (int l, int r) {\n if (!l) return hash[r];\n else return ((hash[r] - hash[l - 1] * p_pow[r - l + 1]) % M + M) % M;\n };\n\n map<long long, int> mpp; vector<string> res;\n for (int i = 0; i < n - 9; i++) {\n int x = sub_hash(i, i + 9);\n if(mpp[x] == 1) res.push_back(s.substr(i, 10));\n mpp[x]++;\n }\n return res; \n }\n};",
"memory": "20378"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n int n = s.size();\n unordered_set<string> hash;\n unordered_set<string> temp;\n for(int i = 0; i <= n-10; i++) {\n string str = s.substr(i, 10);\n if(hash.count(str) > 0) temp.insert(str);\n hash.insert(str);\n }\n return {temp.begin(), temp.end()};\n }\n};",
"memory": "20609"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution\n{\n public:\n std::vector<std::string> findRepeatedDnaSequences(const std::string& s)\n {\n std::unordered_set<std::string> seen, result;\n\n for(std::size_t i=0;i+10<=s.size(); ++i)\n {\n const auto current = s.substr(i,10);\n if(seen.contains(current))\n result.insert(current);\n else\n seen.insert(current);\n }\n\n return {result.begin(), result.end()};\n }\n};",
"memory": "20840"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n //sliding window\n //cache each window\n\n //get a size 10 window\n //check if the window has been cached\n //if it has\n //add this to a return set\n //otherwise cache it\n \n //slide the window up\n //take all the values from the return set and add it to a return vector\n\n std::unordered_set<string> return_set;\n std::unordered_set<string> duplicate_set;\n //edge case if there is a dna sequence that is less than 10 long\n if(s.size() < 10){\n return {};\n }\n // int pointer = 0;\n const int length = 10;\n for(int i = 0; i < s.size() - length + 1; i++){\n string dna_sequence = s.substr(i, length);\n if(duplicate_set.find(dna_sequence) != duplicate_set.end()){\n //exists already\n return_set.insert(dna_sequence);\n }\n else{\n duplicate_set.insert(dna_sequence);\n }\n\n }\n std::vector<string> return_vector;\n for(auto dna : return_set){\n return_vector.push_back(dna);\n }\n return return_vector;\n }\n};",
"memory": "21071"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n vector<string> ans;\n if (s.size() < 10 ){\n return ans;\n }\n unordered_set<string> seq;\n unordered_set<string> inserted;\n for (int i = 0; i <= s.size() - 10; ++i){\n string curr_substr = s.substr(i, 10);\n if (seq.find(curr_substr) == seq.end()){\n seq.insert(curr_substr);\n cout << curr_substr << endl;;\n } else {\n if (inserted.find(curr_substr) == inserted.end()){\n inserted.insert(curr_substr);\n cout << \"y\";\n } else {\n cout << \"x\";\n }\n cout << \"l\";\n }\n }\n for (auto str: inserted){\n ans.push_back(str);\n }\n\n return ans;\n\n \n }\n};",
"memory": "21071"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "#include <vector>\n#include <string>\n#include <unordered_set>\n\nclass Solution {\npublic:\n std::vector<std::string> findRepeatedDnaSequences(std::string s) {\n std::unordered_set<std::string> seen, repeated;\n std::vector<std::string> result;\n \n for (int i = 0; i + 9 < s.length(); ++i) {\n std::string substring = s.substr(i, 10);\n if (seen.count(substring)) {\n repeated.insert(substring);\n } else {\n seen.insert(substring);\n }\n }\n \n for (const std::string& seq : repeated) {\n result.push_back(seq);\n }\n \n return result;\n }\n};\n",
"memory": "21303"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n unordered_set<string>st;\n string temp=\"\";\n unordered_set<string>ans;\n vector<string>result;\n for(int i=0;i<s.length();i++){\n temp+=s[i];\n if(i>=9){\n if(st.empty()||st.find(temp)==st.end()){\n st.insert(temp);\n }else{\n ans.insert(temp);\n }\n temp=temp.substr(1);\n }\n }\n for(auto it:ans){\n result.push_back(it);\n }\n return result;\n }\n};",
"memory": "21303"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n if (s.size() < 10)\n return {};\n int i;\n int num=0;\n string s1;\n unordered_set<string> map1;\n unordered_set<string> map2;\n \n i=0;\n while (i<=s.size()-10) {\n s1 = s.substr(i, 10);\n if (map1.find(s1) != map1.end()) {\n //pattern already present\n map2.insert(s1);\n }\n map1.insert(s1);\n i++;\n }\n vector<string> ans(map2.begin(), map2.end());\n return ans;\n }\n};",
"memory": "21534"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n if (s.size() < 10) {\n return {};\n }\n std::unordered_set<std::string> count;\n\n auto lhs = s.begin();\n auto rhs = s.begin() + 10;\n\n set<string> ret;\n while (rhs != s.end()) {\n std::string sub = std::string(lhs, rhs);\n if (count.contains(sub)) {\n ret.insert(std::move(sub));\n } else {\n count.insert(std::move(sub));\n }\n\n ++lhs;\n ++rhs;\n }\n std::string sub = std::string(lhs, rhs);\n if (count.contains(sub)) {\n ret.insert(std::move(sub));\n } else {\n count.insert(std::move(sub));\n }\n\n return std::vector<string>(ret.begin(), ret.end());\n }\n};",
"memory": "21534"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n if(s.size()<10)\n return {};\n unordered_set<string>st,as;\n vector<string>ans;\n \n for(int i=0;i<=s.size()-9;i++){\n string str=s.substr(i,10);\n if(st.find(str)!=st.end()){\n as.insert(str);\n }\n else{\n st.insert(str);\n }\n }\n for(auto a:as)\n ans.push_back(a);\n return ans;\n }\n};",
"memory": "21765"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n vector<string> ans;\n set<string>mp;\n set<string>ansst;\n int r=9;\n int n=s.length();\n while(r<n)\n {\n string copy=s.substr(r-9,10);\n cout<<copy<<\" sub\"<<endl;\n if(mp.find(copy)!=mp.end()&&ansst.find(copy)==ansst.end()){\n ans.push_back(copy);\n ansst.insert(copy);\n } else{\n mp.insert(copy);\n }\n r++;\n }\n return ans;\n }\n};",
"memory": "21996"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n set<string> ans;\n set<string> helper;\n\n for(int i=0;i<s.size();i++){\n string str=s.substr(i,10);\n if(helper.find(str) != helper.end()) ans.insert(str);\n helper.insert(str);\n }\n\n vector<string> res;\n copy(ans.begin(), ans.end(), back_inserter(res));\n\n return res;\n }\n};",
"memory": "22228"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n set<string> st;\n int n=s.length();\n string temp=\"\";\n for(int i=0;i<10;i++){\n temp=temp+s[i];\n }\n set<string> ans;\n st.insert(temp);\n for(int i=10;i<n;i++){\n temp=s.substr(i-9,10);\n cout<<temp<<endl;\n// temp=temp+s[i];\n if(st.find(temp)!=st.end()) ans.insert(temp);\n else st.insert(temp);\n \n }\n vector<string> fans;\n for(auto it : ans){\n fans.push_back(it);\n }\n return fans;\n }\n};",
"memory": "22459"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n set<string> occurences;\n set<string> result;\n if(s.length() < 10) return {};\n\n string substring;\n for(int i = 9; i < s.length(); i++)\n { \n substring = s.substr(i-9, 10);\n if(occurences.count(substring) > 0) result.insert(substring);\n else occurences.insert(substring);\n }\n\n vector<string> answer;\n answer.assign(result.begin(), result.end());\n return answer;\n }\n};",
"memory": "22690"
} |
187 | <p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, and <code>'T'</code>.</p>
<ul>
<li>For example, <code>"ACGAATTCCG"</code> is a <strong>DNA sequence</strong>.</li>
</ul>
<p>When studying <strong>DNA</strong>, it is useful to identify repeated sequences within the DNA.</p>
<p>Given a string <code>s</code> that represents a <strong>DNA sequence</strong>, return all the <strong><code>10</code>-letter-long</strong> sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
<strong>Output:</strong> ["AAAAACCCCC","CCCCCAAAAA"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "AAAAAAAAAAAAA"
<strong>Output:</strong> ["AAAAAAAAAA"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'A'</code>, <code>'C'</code>, <code>'G'</code>, or <code>'T'</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> findRepeatedDnaSequences(string s) {\n set<string> st;\n set<string> ans;\n\n if(s.size() < 10) return {};\n \n for(int i = 0; i <= (s.size() - 10); i++) {\n string t = s.substr(i, 10);\n if(st.find(t) != st.end()) {\n ans.insert(t);\n }\n else {\n st.insert(t);\n }\n }\n return vector<string> (ans.begin(), ans.end()); \n }\n};",
"memory": "22690"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.