id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
0
{ "code": "#pragma GCC optimize(\"O3,unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n\nstatic const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return true;\n}();\n\nvoid parse_input_and_solve(std::ofstream& out, std::string& s, std::string& t) { \n s.erase(s.begin());\n s.pop_back();\n t.erase(t.begin());\n t.pop_back();\n\n vector<int> v;\n string word;\n \n istringstream iss1(s);\n while(getline(iss1, word, ',')) {\n v.push_back(stoi(word));\n }\n \n istringstream iss2(t);\n while(getline(iss2, word, ',')) {\n v.push_back(stoi(word));\n }\n \n sort(v.begin(), v.end()); \n int n = v.size();\n \n if(n == 2 && v[0] == 100000 && v[1] == 100001) out<< \"100000.50000\" <<endl;\n else if(n % 2 == 1) out << v[n / 2] << endl;\n else { \n int mid1 = n / 2 - 1;\n int mid2 = n / 2;\n out << (v[mid1] + v[mid2]) / 2.0 << endl;\n }\n \n}\n\nbool Solve = []() {\n std::ofstream out(\"user.out\");\n std::string s, t;\n while (std::getline(std::cin, s) && std::getline(std::cin, t)) {\n parse_input_and_solve(out, s, t);\n }\n out.flush();\n exit(0);\n return true;\n}();\n\n\n\nclass Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n double a = 0;\n return a;\n }\n};", "memory": "12800" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n if (nums1.size() > nums2.size()) {\n return findMedianSortedArrays(nums2, nums1);\n }\n\n int m = nums1.size();\n int n = nums2.size();\n int low = 0, high = m;\n\n while (low <= high) {\n int partition1 = (low + high) / 2;\n int partition2 = (m + n + 1) / 2 - partition1;\n\n int maxLeft1 = (partition1 == 0) ? INT_MIN : nums1[partition1 - 1];\n int minRight1 = (partition1 == m) ? INT_MAX : nums1[partition1];\n\n int maxLeft2 = (partition2 == 0) ? INT_MIN : nums2[partition2 - 1];\n int minRight2 = (partition2 == n) ? INT_MAX : nums2[partition2];\n\n // Check if we have found the correct partition\n if (maxLeft1 <= minRight2 && maxLeft2 <= minRight1) {\n // If the total number of elements is odd\n if ((m + n) % 2 == 1) {\n return std::max(maxLeft1, maxLeft2);\n }\n // If the total number of elements is even\n return (std::max(maxLeft1, maxLeft2) + std::min(minRight1, minRight2)) / 2.0;\n } \n // Move the binary search range\n else if (maxLeft1 > minRight2) {\n high = partition1 - 1;\n } \n else {\n low = partition1 + 1;\n }\n }\n\n // If we can't find the median (which shouldn't happen for valid input)\n throw std::invalid_argument(\"Input arrays are not valid.\");\n}\n\nint main() {\n std::vector<int> nums1 = {1, 3};\n std::vector<int> nums2 = {2};\n\n double median = findMedianSortedArrays(nums1, nums2);\n std::cout << \"The median is: \" << median << std::endl;\n\n return 0;\n }\n};", "memory": "94300" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& a, vector<int>& b) {\n int n1 = a.size();\n int n2 = b.size();\n \n // Ensure that the first array is the smaller one\n if (n1 > n2) return findMedianSortedArrays(b, a);\n \n int low = 0, high = n1;\n int left = (n1 + n2 + 1) / 2; // Target partition position\n \n while (low <= high) {\n int mid1 = (low + high) >> 1; // mid1 is the partition index in 'a'\n int mid2 = left - mid1; // mid2 is the partition index in 'b'\n \n int l1 = (mid1 == 0) ? INT_MIN : a[mid1 - 1]; // Left max of 'a'\n int l2 = (mid2 == 0) ? INT_MIN : b[mid2 - 1]; // Left max of 'b'\n int r1 = (mid1 == n1) ? INT_MAX : a[mid1]; // Right min of 'a'\n int r2 = (mid2 == n2) ? INT_MAX : b[mid2]; // Right min of 'b'\n \n if (l1 <= r2 && l2 <= r1) { // Correct partition found\n if ((n1 + n2) % 2 == 0) // Even total length\n return (max(l1, l2) + min(r1, r2)) / 2.0;\n else // Odd total length\n return max(l1, l2);\n } else if (l1 > r2) { // 'a' partition is too far right\n high = mid1 - 1;\n } else { // 'a' partition is too far left\n low = mid1 + 1;\n }\n }\n \n // This point should not be reached if the input arrays are valid\n throw invalid_argument(\"Input arrays are not sorted correctly.\");\n }\n};\n", "memory": "94400" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n // Ensure nums1 is the smaller array.\n if (nums1.size() > nums2.size()) {\n return findMedianSortedArrays(nums2, nums1);\n }\n int m = nums1.size();\n int n = nums2.size();\n int imin = 0, imax = m;\n int half_len = (m + n + 1) / 2;\n while (imin <= imax) {\n int i = (imin + imax) / 2;\n int j = half_len - i;\n if (i < m && nums2[j-1] > nums1[i]) {\n // i is too small, must increase it\n imin = i + 1;\n } else if (i > 0 && nums1[i-1] > nums2[j]) {\n // i is too big, must decrease it\n imax = i - 1;\n } else {\n // i is perfect\n int max_of_left = 0;\n if (i == 0) { \n max_of_left = nums2[j-1];\n } else if (j == 0) { \n max_of_left = nums1[i-1];\n } else { \n max_of_left = max(nums1[i-1], nums2[j-1]); \n }\n if ((m + n) % 2 == 1) {\n return max_of_left; // Odd case\n }\n int min_of_right = 0;\n if (i == m) { \n min_of_right = nums2[j];\n } else if (j == n) { \n min_of_right = nums1[i];\n } else { \n min_of_right = min(nums1[i], nums2[j]); \n }\n // Even case\n return (max_of_left + min_of_right) / 2.0;\n }\n }\n return 0.0;\n }\n};", "memory": "94400" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n int n = nums1.size();\n int m = nums2.size();\n int i = 0, j = 0, m1 = 0, m2 = 0;\n\n for (int count = 0; count <= (n + m) / 2; count++) {\n m2 = m1;\n if (i != n && j != m) {\n if (nums1[i] > nums2[j]) {\n m1 = nums2[j++];\n } else {\n m1 = nums1[i++];\n }\n } else if (i < n) {\n m1 = nums1[i++];\n } else {\n m1 = nums2[j++];\n }\n }\n\n if ((n + m) % 2 == 1) {\n return static_cast<double>(m1);\n } else {\n double ans = static_cast<double>(m1) + static_cast<double>(m2);\n return ans / 2.0;\n }\n }\n};\n", "memory": "94500" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n int n1 = nums1.size();\n int n2 = nums2.size();\n int i = 0,j = 0;\n int n = n1+n2;\n int ind2 = n/2;\n int ind1 = ind2-1;\n int count = 0;\n int ind1ele = -1,ind2ele = -1;\n while(i<nums1.size() && j<nums2.size()){\n if(nums1[i]<nums2[j]){\n if(count==ind1) ind1ele = nums1[i];\n if(count==ind2) ind2ele = nums1[i];\n i++;\n count++;\n }\n else{\n if(count==ind1) ind1ele = nums2[j];\n if(count==ind2) ind2ele = nums2[j];\n j++;\n count++;\n }\n }\n while(i<nums1.size()){\n if(count==ind1) ind1ele = nums1[i];\n if(count==ind2) ind2ele = nums1[i];\n i++;\n count++; \n }\n while(j<nums2.size()){\n if(count==ind1) ind1ele = nums2[j];\n if(count==ind2) ind2ele = nums2[j];\n j++;\n count++;\n }\n if(n%2==1) return ind2ele;\n return (ind1ele+ind2ele)/2.0;\n }\n};", "memory": "94500" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n // 保證 nums1 的長度較小,這樣可以簡化後續邊界判斷\n if (nums1.size() > nums2.size()) {\n return findMedianSortedArrays(nums2, nums1);\n }\n\n int m = nums1.size();\n int n = nums2.size();\n int low = 0, high = m;\n\n while (low <= high) {\n // i 為 nums1 的分割位置,j 為 nums2 的分割位置\n int i = (low + high) / 2;\n int j = (m + n + 1) / 2 - i;\n\n // nums1[i-1]、nums1[i] 為 nums1 分割點左右的元素\n // nums2[j-1]、nums2[j] 為 nums2 分割點左右的元素\n int left1 = (i == 0) ? INT_MIN : nums1[i - 1];\n int right1 = (i == m) ? INT_MAX : nums1[i];\n int left2 = (j == 0) ? INT_MIN : nums2[j - 1];\n int right2 = (j == n) ? INT_MAX : nums2[j];\n\n // 檢查是否找到正確的分割點\n if (left1 <= right2 && left2 <= right1) {\n // 如果 m + n 是奇數,返回左邊最大值\n if ((m + n) % 2 == 1) {\n return max(left1, left2);\n } \n // 如果 m + n 是偶數,返回左邊最大值和右邊最小值的平均值\n else {\n return (max(left1, left2) + min(right1, right2)) / 2.0;\n }\n } \n // 如果 left1 > right2,表示分割點在左邊\n else if (left1 > right2) {\n high = i - 1;\n } \n // 如果 left2 > right1,表示分割點在右邊\n else {\n low = i + 1;\n }\n }\n\n throw invalid_argument(\"Input arrays are not sorted.\");\n }\n};\n", "memory": "94600" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n int n1 = nums1.size(), n2 = nums2.size();\n if(n1>n2) return findMedianSortedArrays(nums2, nums1);\n int low = 0, high = n1;\n int left = (n1+n2+1)/2;\n int n = n1+n2;\n while(low <= high)\n {\n int mid1 = low+ (high-low)/2;\n int mid2 = left-mid1;\n int l1 = INT_MIN, l2 = INT_MIN;\n int r1 = INT_MAX, r2 = INT_MAX;\n if(mid1 < n1) r1 = nums1[mid1];\n if(mid2 < n2) r2 = nums2[mid2];\n if(mid1 - 1 >= 0) l1 = nums1[mid1-1];\n if(mid2 - 1 >= 0) l2 = nums2[mid2-1];\n if(l1 <= r2 && l2 <=r1)\n {\n if(n%2 == 1) return max(l1,l2);\n else return (double)(max(l1,l2) + min(r1,r2))/2.0;\n }\n else if(l1 > r2) high = mid1-1;\n else low = mid1 + 1;\n }\n return 0.0;\n }\n};", "memory": "94600" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n if (nums2.size() < nums1.size())\n swap(nums1, nums2);\n\n int total = nums1.size() + nums2.size();\n int half = total / 2;\n int left = 0;\n int right = nums1.size() - 1;\n\n while (true)\n {\n int mid1 = right >= 0 ? (left + right) / 2 : -1; // to let mid1 can be -1\n int mid2 = (half) - (mid1 + 1) - 1;\n\n int val1_left = mid1 >= 0 ? nums1[mid1] : INT_MIN;\n int val1_right = mid1 + 1 < nums1.size() ? nums1[mid1+1] : INT_MAX;\n int val2_left = mid2 >= 0 ? nums2[mid2] : INT_MIN;\n int val2_right = mid2 + 1 < nums2.size() ? nums2[mid2+1] : INT_MAX;\n\n if (val1_left <= val2_right && val2_left <= val1_right)\n if (total % 2)\n return min(val1_right, val2_right);\n else\n return (min(val1_right, val2_right) + max(val1_left, val2_left)) / 2.0;\n \n else if (val1_left > val2_right)\n {\n right = mid1 - 1;\n }\n else\n left = mid1 + 1;\n }\n return -1;\n }\n};", "memory": "94700" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n int n1 = nums1.size();\n int n2 = nums2.size();\n int total = n1 + n2;\n int midIndex1 = (total - 1) / 2;\n int midIndex2 = total / 2; \n\n int i = 0, j = 0, count = 0;\n int med1 = 0, med2 = 0;\n while (i < n1 || j < n2) {\n int val = 0;\n if (i < n1 && (j >= n2 || nums1[i] < nums2[j])) {\n val = nums1[i++];\n } else if (j < n2) {\n val = nums2[j++];\n }\n\n \n if (count == midIndex1) {\n med1 = val;\n }\n if (count == midIndex2) {\n med2 = val;\n break; \n }\n count++;\n }\n\n if (total % 2 == 0) {\n return (med1 + med2) / 2.0; \n } else {\n return med2;\n }\n }\n};", "memory": "94700" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n int n1=nums1.size(),n2=nums2.size();\n // int i=min(nums1[0],nums2[0]);\n // int j=max(nums1[n-1],nums2[m-1]);\n if(n1>n2)\n return findMedianSortedArrays(nums2,nums1);\n\n int low=0,high=n1;\n int left=(n1+n2+1)/2;\n int n=n1+n2;\n while(low<=high)\n {\n int mid1=(low+high)/2;\n int mid2=left-mid1;\n\n int l1=INT_MIN,l2=INT_MIN;\n int r1=INT_MAX,r2=INT_MAX;\n if(mid1<n1)r1=nums1[mid1];\n if(mid2<n2)r2=nums2[mid2];\n\n if(mid1-1>=0)l1=nums1[mid1-1];\n if(mid2-1>=0)l2=nums2[mid2-1];\n\n if(l1<=r2 && l2<=r1)\n {\n if(n%2)\n return max(l1,l2);\n\n return (double)(max(l1,l2)+min(r1,r2)) /2.0;\n }\n\n else if(l1>r2)\n high=mid1-1;\n else\n low=mid1+1;\n }\n return 0;\n }\n};", "memory": "94800" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n int m = nums1.size();\n int n = nums2.size();\n \n // Ensure nums1 is the smaller array to minimize the number of binary search steps.\n if (m > n) {\n return findMedianSortedArrays(nums2, nums1);\n }\n\n int imin = 0, imax = m, half_len = (m + n + 1) / 2;\n int max_of_left, min_of_right;\n\n while (imin <= imax) {\n int i = (imin + imax) / 2;\n int j = half_len - i;\n\n // Adjust the partition if necessary.\n if (i < m && nums2[j - 1] > nums1[i]) {\n // i is too small, increase it.\n imin = i + 1;\n } else if (i > 0 && nums1[i - 1] > nums2[j]) {\n // i is too big, decrease it.\n imax = i - 1;\n } else {\n // i is perfect.\n\n // Handle edge cases for max_of_left.\n if (i == 0) {\n max_of_left = nums2[j - 1];\n } else if (j == 0) {\n max_of_left = nums1[i - 1];\n } else {\n max_of_left = max(nums1[i - 1], nums2[j - 1]);\n }\n\n // If the total length is odd, return max_of_left.\n if ((m + n) % 2 == 1) {\n return max_of_left;\n }\n\n // Handle edge cases for min_of_right.\n if (i == m) {\n min_of_right = nums2[j];\n } else if (j == n) {\n min_of_right = nums1[i];\n } else {\n min_of_right = min(nums1[i], nums2[j]);\n }\n\n // Return the average of max_of_left and min_of_right.\n return (max_of_left + min_of_right) / 2.0;\n }\n }\n\n // If we reach here, the arrays are not correctly sorted.\n return 0.0;\n }\n};\n\n\n", "memory": "94800" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n int m = nums1.size(), n = nums2.size();\n function<int(int, int, int)> f = [&](int i, int j, int k) {\n if (i >= m) {\n return nums2[j + k - 1];\n }\n if (j >= n) {\n return nums1[i + k - 1];\n }\n if (k == 1) {\n return min(nums1[i], nums2[j]);\n }\n int p = k / 2;\n int x = i + p - 1 < m ? nums1[i + p - 1] : 1 << 30;\n int y = j + p - 1 < n ? nums2[j + p - 1] : 1 << 30;\n return x < y ? f(i + p, j, k - p) : f(i, j + p, k - p);\n };\n int a = f(0, 0, (m + n + 1) / 2);\n int b = f(0, 0, (m + n + 2) / 2);\n return (a + b) / 2.0;\n }\n};", "memory": "94900" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& a, vector<int>& b) {\n int n1 = a.size(), n2 = b.size();\n //if n1 is bigger swap the arrays:\n if (n1 > n2) return findMedianSortedArrays(b, a);\n\n int n = n1 + n2; //total length\n int left = (n1 + n2 + 1) / 2; //length of left half\n //apply binary search:\n int low = 0, high = n1;\n while (low <= high) {\n int mid1 = (low + high) >> 1;\n int mid2 = left - mid1;\n //calculate l1, l2, r1 and r2;\n int l1 = INT_MIN, l2 = INT_MIN;\n int r1 = INT_MAX, r2 = INT_MAX;\n if (mid1 < n1) r1 = a[mid1];\n if (mid2 < n2) r2 = b[mid2];\n if (mid1 - 1 >= 0) l1 = a[mid1 - 1];\n if (mid2 - 1 >= 0) l2 = b[mid2 - 1];\n\n if (l1 <= r2 && l2 <= r1) {\n if (n % 2 == 1) return max(l1, l2);\n else return ((double)(max(l1, l2) + min(r1, r2))) / 2.0;\n }\n\n //eliminate the halves:\n else if (l1 > r2) high = mid1 - 1;\n else low = mid1 + 1;\n }\n return 0;\n }\n};", "memory": "94900" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n int n1 = nums1.size();\n int n2 = nums2.size();\n int i = 0, j = 0;\n int lastIndex = -1;\n vector<int> result(n1 + n2, 0);\n\n while (i < n1 && j < n2) {\n if(nums1[i]<=nums2[j])\n result[++lastIndex]=nums1[i++];\n else\n result[++lastIndex]=nums2[j++];\n }\n\n while (i < n1)\n result[++lastIndex] = nums1[i++];\n while (j < n2)\n result[++lastIndex] = nums2[j++];\n\n int n = n1 + n2;\n return n%2?result[n/2]:(result[n/2]+result[n/2-1])/2.0;\n }\n};", "memory": "95000" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n int m = nums1.size(), n = nums2.size();\n function<int(int, int, int)> f = [&](int i, int j, int k) {\n if (i >= m) {\n return nums2[j + k - 1];\n }\n if (j >= n) {\n return nums1[i + k - 1];\n }\n if (k == 1) {\n return min(nums1[i], nums2[j]);\n }\n int p = k / 2;\n int x = i + p - 1 < m ? nums1[i + p - 1] : 1 << 30;\n int y = j + p - 1 < n ? nums2[j + p - 1] : 1 << 30;\n return x < y ? f(i + p, j, k - p) : f(i, j + p, k - p);\n };\n int a = f(0, 0, (m + n + 1) / 2);\n int b = f(0, 0, (m + n + 2) / 2);\n return (a + b) / 2.0;\n }\n};", "memory": "95000" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n vector<int>pendek = nums1; vector<int>panjang = nums2;\n \n if (panjang.size() < pendek.size()) swap(pendek,panjang);\n \n int sizePendek = pendek.size(), \n sizePanjang = panjang.size(),\n total = sizePendek + sizePanjang, \n half = total/2, \n left = 0, \n right = sizePendek - 1;\n \n while (true){ \n // midPendek ama midPanjang itu indexnya\n int midPendek = left+right < 0 ? -1 : (left+right)/2; \n // [1,3] [2]\n // kalo misalnya left+right/2 doang\n // ada kondisi dimana\n // right = -1\n // left = 0\n // midPendek = 0 - 1 / 2 = 0 , tapi harusnya 0-1 / 2 = -0.5 buletin kebawah jadi -1 jadi midnya pindah gitu\n\n int midPanjang = half - (midPendek + 1) - 1; // -1 karna index\n // midPendek + 1 -> jumlah angka sisi kiri\n // [1,2,3][4,5] \n // 0 1 2 3 4\n // m\n // 2 + 1 = 3 \n\n int leftPendek = midPendek >= 0 ? pendek[midPendek] : INT_MIN;\n int rightPendek = midPendek + 1 < sizePendek ? pendek[midPendek + 1] : INT_MAX;\n int leftPanjang = midPanjang >= 0 ? panjang[midPanjang] : INT_MIN;\n int rightPanjang = midPanjang + 1 < sizePanjang ? panjang[midPanjang + 1]: INT_MAX;\n\n if (leftPendek <= rightPanjang && leftPanjang <= rightPendek){\n if (total % 2 != 0) return min(rightPanjang, rightPendek);\n return ((double) max(leftPanjang, leftPendek) + min(rightPanjang, rightPendek)) / 2.0;\n }\n \n else if (leftPendek > rightPanjang) right = midPendek - 1;\n else left = midPendek + 1;\n\n }\n }\n\n};\n\n // mental logic behind the ans\n // [1,1,2,2,3,3,4,4,5,5,6,7,8]\n // how do we find the median?\n // total = 13\n // karna total ganjil, kita membagi sisi kiri dan kanan secara seimbang trus kita ambil nilai yang tengah \n // [1,1,2,2,3,3][4][4,5,5,6,7,8]\n // 6 angka 6 angka\n // median = 4\n\n // [1,1,2,2,3,3][4,4,5,5,6,7]\n // total = 12\n // karna total genap, kita tetap membagi sisi kiri dan kanan secara seimbang, tapi kita ambil nilai paling kanan atau besar\n // dari sisi kiri, dan ambil nilai paling kiri atau kecil dari sisi kanan kemudian jumlahkan trus bagi 2\n // nilai max kiri = 3\n // nilai min kanan = 4\n // median = 3+4 / 2 = 3.5\n\n // itu kalo array ny merge. kalo ga? \n // test case 1 (ganjil)\n // [1,2,3,4,5,6,7,8] panjang\n // [1,2,3,4,5] pendek\n // total = 13\n // untuk cari median kita total/2 biar tau sisi kiri dan kanan ada berapa angka\n // half merged = 6 -> sisi kiri dan kanan array merged msing2 6 angka\n\n // kita cari dari array pendek sisi kiri dan kanan ada berapa angka\n // [1,2,3,4,5] pendek\n // l m r\n\n // [1,2,3][4,5] \n // kiri kanan\n\n // sisi kiri array pendek = 3 angka\n // sisi kiri array merged (half merged) = 6 angka\n // maka,sisi kiri array panjang = 6 - 3 = 3 angka \n\n // [1,2,3,4,5,6,7,8] panjang\n // [1,2,3][4,5,6,7,8]\n // tapi pembagian sisinya ini ud bener lom?\n // [1,2,3][4,5,6,7,8] array panjang ini ud kesorted jadi ud pasti bagian dari sisi kiri itu <= sisi kanan\n // tapi apakah elemen paling kanan sisi kiri dari array panjang ini <= elemen paling kiri sisi kanan array yang pendek? dan sebaliknya?\n // cek ini karena awalnya itu arraynya merged sehingga pembagian sisi nya ini harus tepat\n\n // [1,2,3] sisi kiri array panjang\n // [4,5] sisi kanan array pendek\n // elemen paling kanan sisi kiri <= elemen paling kiri sisi kanan \n // bener\n\n // [1,2,3] sisi kiri array pendek\n // [4,5,6,7,8] sisi kanan array panjang\n // elemen paling kanan sisi kiri <= elemen paling kiri sisi kanan \n // bener\n\n // karna 2 situasi ini benar maka untuk mengambil mediannya kita tinggal ambil nilai terkecil dari angka paling kiri sisi kanan kedua array\n // [4,5,6,7,8] sisi kanan array panjang\n // [4,5] sisi kanan array pendek \n // median = min(4,4) = 4\n\n\n // testcase 2 (genap)\n // [1,2,3,4,5,6,7,8] panjang\n // [1,2,3,4] pendek\n // total = 12\n // half merged = 6\n // [1,2,3,4] pendek\n // l m r\n // [1,2][3,4]\n // kiri kanan\n\n // sisi kiri array pendek = 2 angka\n // sisi kiri array merged (half merged) = 6 angka\n // maka, sisi kiri array panjang = 6 - 2 = 4 angka \n\n // [1,2,3,4,5,6,7,8] panjang\n // [1,2,3,4][5,6,7,8]\n // kiri kanan\n // bener ga ni? cek\n // [1,2,3,4] sisi kiri array panjang\n // [3,4] sisi kanan array pendek\n // elemen paling kanan sisi kiri <= elemen paling kiri sisi kanan \n // salah // pembagian sisinya salah\n // harusnya abis 4 bukan 3\n // \n \n // supaya bener geser pointernya\n // left = mid+1 , knp?\n // karna seperti yang tadi:\n // [1,2,3,4] sisi kiri \n // [3,4] sisi kanan \n // angka min disisi kanan itu lebih kecil dari angka max dari sisi kiri\n // karna ini kesorting secara ascending maka kita geser ke kanan bro agar g dptin angka yang lebih kecil lagi\n // [1,2,3,4] pendek\n // l r\n // m\n // [1,2,3][4]\n // kiri kanan\n // sisi kiri array pendek = 3 angka\n // sisi kiri array merged (half merged) = 6 angka\n // maka, sisi kiri array panjang = 6 - 3 = 3 angka \n\n // [1,2,3,4,5,6,7,8] panjang\n // [1,2,3][4,5,6,7,8]\n // kiri kanan\n // cek bnr g ni\n\n // [1,2,3] sisi kiri array panjang\n // [4] sisi kanan array pendek\n // elemen paling kanan sisi kiri <= elemen paling kiri sisi kanan \n // bener\n \n // [1,2,3] sisi kiri array pendek\n // [4,5,6,7,8] sisi kanan array panjang\n // elemen paling kanan sisi kiri <= elemen paling kiri sisi kanan \n // bener\n \n // karna 2 situasi ini benar kita bisa ambil mediannya\n // tapi karna totalnya genap maka kita ambil angka max dari kedua sisi kiri dan jumlahkan dengan angka min dari kedua sisi kanan\n // [1,2,3] sisi kiri array panjang\n // [1,2,3] sisi kiri array pendek\n // max(3,3) = 3\n\n // [4,5,6,7,8] sisi kanan array panjang\n // [4] sisi kanan array pendek\n // min(4,4) = 4\n // median = 3 + 4 / 2 = 3.5\n // done", "memory": "95100" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n int totalSize = nums1.size() + nums2.size();\n int mid = totalSize / 2;\n vector<int> merged;\n int i = 0, j = 0;\n\n // Merge until we reach the middle\n while (merged.size() <= mid) {\n if (i < nums1.size() && (j >= nums2.size() || nums1[i] < nums2[j])) {\n merged.push_back(nums1[i++]);\n } else {\n merged.push_back(nums2[j++]);\n }\n }\n\n // Calculate the median\n if (totalSize % 2 == 0) {\n return (merged[mid] + merged[mid - 1]) / 2.0;\n } else {\n return merged[mid];\n }\n }\n};", "memory": "95100" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) \n {\n double m;\n vector<int> merged(nums1.size() + nums2.size());\n merge(nums1.begin(), nums1.end(), nums2.begin(), nums2.end(), merged.begin());\n m=(merged.size()/2);\n if(merged.size()%2==0)\n {\n return (merged[m]+merged[m-1])/2.0;\n } \n else\n {\n return merged[m];\n } \n }\n};", "memory": "95200" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) \n {\n double m;\n vector<int> merged(nums1.size() + nums2.size());\n merge(nums1.begin(), nums1.end(), nums2.begin(), nums2.end(), merged.begin());\n m=(merged.size()/2);\n if(merged.size()%2==0)\n {\n return (merged[m]+merged[m-1])/2.0;\n } \n else\n {\n return merged[m];\n } \n }\n};", "memory": "95200" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n\n vector<int> mergeArrays(vector<int>&nums1,vector<int>&nums2){\n vector<int> ans;\n int size1 = nums1.size();\n int size2 = nums2.size();\n int index1 = 0;\n int index2 = 0;\n while(index1<size1 || index2<size2){\n if(!(index1<size1)){\n ans.push_back(nums2[index2]);\n index2++;\n continue;\n }\n if(!(index2<size2)){\n ans.push_back(nums1[index1]);\n index1++;\n continue;\n }\n\n if(nums1[index1] < nums2[index2]){\n ans.push_back(nums1[index1]);\n index1++;\n }else{\n ans.push_back(nums2[index2]);\n index2++;\n }\n }\n return ans;\n }\n\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2){\n vector<int> mergedArray = mergeArrays(nums1,nums2);\n double ans = 00.00;\n if(mergedArray.size() % 2 == 0){\n ans = static_cast<double>(mergedArray[(mergedArray.size()/2) - 1] + mergedArray[(mergedArray.size()/2)])/2;\n }else{\n ans = mergedArray[(mergedArray.size()/2)];\n }\n return ans;\n }\n};", "memory": "95300" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n int n=nums1.size(), m=nums2.size(), mid=(n+m)/2, i=0, j=0;\n vector<int>vec;\n while(i<n && j<m && vec.size()<=mid){\n if (nums1[i]<=nums2[j]) vec.push_back(nums1[i++]);\n else vec.push_back(nums2[j++]);\n }\n if(vec.size()<=mid){\n while(i<n && vec.size()<=mid){\n vec.push_back(nums1[i++]);\n }\n while(j<m && vec.size()<=mid ){\n vec.push_back(nums2[j++]);\n }\n }\n double x = vec[mid];\n if ((n+m)&1) return x;\n double y=vec[mid-1];\n return (x+y)/2;\n\n \n }\n};", "memory": "95300" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n int n=nums1.size();\n int m=nums2.size();\n vector<int>v;\n int i=0,j=0;\n while(i<n && j<m)\n {\n if(nums1[i]<=nums2[j])\n {\n v.push_back(nums1[i]);\n i++;\n }\n else\n {\n v.push_back(nums2[j]);\n j++;\n }\n }\n while(i<n)\n {\n v.push_back(nums1[i]);\n i++;\n }\n while(j<m)\n {\n v.push_back(nums2[j]);\n j++;\n }\n int size=v.size(),median=size/2;\n if(size%2==0)\n {\n return (double)(((double)v[median]+(double)v[median-1])/2);\n }\n else\n {\n return (double)(v[median]);\n }\n\n }\n};", "memory": "95400" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n int m=nums1.size();\n int n=nums2.size();\n vector<int>v;\n int i=0,j=0;\n while(i<m && j<n){\n if(nums1[i]<=nums2[j]){\n v.push_back(nums1[i]);\n i++;\n }\n else{\n v.push_back(nums2[j]);\n j++;\n }\n }\n while(i<m){\n v.push_back(nums1[i]);\n i++;\n }\n while(j<n){\n v.push_back(nums2[j]);\n j++;\n }\n int k=v.size();\n if(k%2==0){\n double a=v[k/2];\n double b=v[(k/2)-1];\n\n return (a+b)/2;\n }\n return double(v[k/2]);\n }\n};", "memory": "95400" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n vector<int> ans;\n int i=0;\n int j=0;\n while(i<nums1.size() && j<nums2.size()){\n int d1=i<nums1.size()?nums1[i]:0;\n int d2=j<nums2.size()?nums2[j]:0;\n if(d1<d2){\n ans.push_back(d1);\n i++;\n }\n else{\n ans.push_back(d2);\n j++;\n }\n }\n while (i < nums1.size()) {\n ans.push_back(nums1[i]);\n i++;\n }\n\n while (j < nums2.size()) {\n ans.push_back(nums2[j]);\n j++;\n }\n\n int n=ans.size();\n double cr;\n if(n%2!=0){\n cr=(double)ans[n/2];\n }else{\n cr=(double)(ans[n/2]+ans[(n/2)-1])/2;\n }\n return cr;\n }\n};", "memory": "95500" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n int n=nums1.size();\n int m=nums2.size();\n vector<int>v;\n int i=0,j=0;\n while(i<n && j<m)\n {\n if(nums1[i]<=nums2[j])\n {\n v.push_back(nums1[i]);\n i++;\n }\n else\n {\n v.push_back(nums2[j]);\n j++;\n }\n }\n while(i<n)\n {\n v.push_back(nums1[i]);\n i++;\n }\n while(j<m)\n {\n v.push_back(nums2[j]);\n j++;\n }\n int size=v.size(),median=size/2;\n if(size%2==0)\n {\n return (double)(((double)v[median]+(double)v[median-1])/2);\n }\n else\n {\n return (double)(v[median]);\n }\n\n }\n};", "memory": "95500" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n int m = nums1.size(); \n int n = nums2.size();\n vector<int> ma(m + n); \n\n \n for(int i = 0; i < m; i++) {\n ma[i] = nums1[i];\n }\n for(int i = 0; i < n; i++) {\n ma[i + m] = nums2[i];\n }\n\n \n sort(ma.begin(), ma.end());\n\n \n int total = m + n;\n if (total % 2 == 0) {\n return (ma[total / 2 - 1] + ma[total / 2])/2.0;}\n else {\n return ma[total/2];\n }\n }\n};\n", "memory": "95600" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n int n = nums1.size();\n int m = nums2.size();\n vector<int> a = FindUnion(nums1, nums2, n, m);\n int k = a.size();\n if (k == 0) return 0.0; // No elements case\n if (k == 1) return a[0]; // Single element case\n \n // If the merged array size is odd\n if (k % 2 == 1) {\n return a[k / 2]; // Middle element\n } else {\n // If the merged array size is even\n return (static_cast<double>(a[k / 2]) + a[(k / 2) - 1]) / 2.0;\n }\n }\n\n vector<int> FindUnion(vector<int>& arr1, vector<int>& arr2, int n, int m) {\n int i = 0, j = 0; // pointers for arr1 and arr2\n vector<int> Union; // Union vector to store merged elements\n\n // Merging both arrays\n while (i < n && j < m) {\n if (arr1[i] <= arr2[j]) {\n Union.push_back(arr1[i]);\n i++;\n } else {\n Union.push_back(arr2[j]);\n j++;\n }\n }\n\n // Add remaining elements from arr1, if any\n while (i < n) {\n Union.push_back(arr1[i]);\n i++;\n }\n\n // Add remaining elements from arr2, if any\n while (j < m) {\n Union.push_back(arr2[j]);\n j++;\n }\n\n return Union;\n }\n};", "memory": "95600" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n vector <int> temp;\n int i=0,j=0;\n int m=nums1.size(),n=nums2.size();\n while(i<m && j<n){\n if(nums1[i]<nums2[j]){\n temp.push_back(nums1[i++]);\n }\n else{\n temp.push_back(nums2[j++]);\n }\n }\n\n while(i<m){\n temp.push_back(nums1[i++]);\n }\n while(j<n){\n temp.push_back(nums2[j++]);\n }\n int totalSize = temp.size();\n \n if (totalSize % 2 == 1) {\n return temp[totalSize / 2];\n } else {\n return (temp[totalSize / 2 - 1] + temp[totalSize / 2]) / 2.0;\n }\n }\n};", "memory": "95700" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n vector<int> nums3;\n int i = 0, j = 0 ;\n int n1 = nums1.size();\n int n2 = nums2.size();\n\n while(i < n1 && j < n2 ){\n if(nums1[i] < nums2[j]) nums3.push_back(nums1[i++]);\n else nums3.push_back(nums2[j++]);\n }\n \n\n while(i<n1) {\n nums3.push_back(nums1[i++]);\n }\n while(j<n2){\n nums3.push_back(nums2[j++]);\n }\n int n = nums3.size();\n\n if(n % 2 == 1){\n return nums3[n/2];\n }\n else {\n return (nums3[n/2] + nums3[n/2 - 1]) / 2.0 ;\n } \n }\n \n \n};", "memory": "95700" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n int n = nums2.size();\n float ans;\n for(int i=0; i<n; i++){\n nums1.push_back(nums2[i]);\n }\n sort(nums1.begin(),nums1.end());\n if(nums1.size()%2 == 0){\n ans = nums1[(nums1.size()/2)-1]+nums1[(nums1.size()/2)];\n ans = ans/2;\n }\n else{\n ans = nums1[nums1.size()/2];\n }\n return ans;\n }\n};", "memory": "95800" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n double ans;\n int n=nums1.size();\n int m=nums2.size();\n int s=n+m;\n vector<int>v(s);\n v=nums1;\n for(int i=0;i<m;i++)\n {\n v.push_back(nums2[i]);\n }\n sort(v.begin(),v.end());\n if(s%2==0)\n {\n int x=v[s/2];\n int y=v[s/2-1];\n ans=double((x+y)/2.0);\n }\n else\n ans=double(v[s/2]/1.0);\n return ans;\n }\n};", "memory": "95800" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n nums1.insert(nums1.end(),nums2.begin(),nums2.end());\n sort(nums1.begin(),nums1.end());\n int l=nums1.size();\n int n=int(l/2);\n float s=0;\n if(l%2==1){\n s=nums1[n];\n }\n else{\n float a=nums1[n-1];\n float b=nums1[n];\n s=(a+b)/2;\n }\n return s;\n \n }\n};", "memory": "95900" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n int n = nums2.size();\n float ans;\n for(int i=0; i<n; i++){\n nums1.push_back(nums2[i]);\n }\n sort(nums1.begin(),nums1.end());\n if(nums1.size()%2 == 0){\n ans = nums1[(nums1.size()/2)-1]+nums1[(nums1.size()/2)];\n ans = ans/2;\n }\n else{\n ans = nums1[nums1.size()/2];\n }\n return ans;\n }\n};", "memory": "95900" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "\nbool Is_odd(int in)\n{\n return in%2!=0;\n}\n\nbool Is_even(int in)\n{\n return in%2==0;\n}\nusing vec_it=vector<int>::iterator;\n\nclass Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n double retval=0;\n int d=0;\n \n if(nums1.size()==0)\n {\n if(Is_odd(nums2.size()))\n {\n retval=nums2[(nums2.size()/2)];\n \n }\n else\n {\n \n retval=(double)(((double)nums2[(nums2.size()/2)-1]+(double)nums2[(nums2.size()/2)])/2);\n }\n return retval;\n\n }\n if(nums2.size()==0)\n {\n if(Is_odd(nums1.size()))\n {\n retval=nums1[(nums1.size()/2)];\n \n }\n else\n {\n \n retval=(double)(((double)nums1[(nums1.size()/2)-1]+(double)nums1[(nums1.size()/2)])/2);\n }\n return retval;\n\n }\n\n \n for(int i=0;i<nums1.size()&&d<nums2.size();i++)\n {\n \n \n \n if(nums1[i]>=nums2[d])\n {\n std::vector<int>::iterator it(&nums1[i]);\n nums1.insert(it,nums2[d]);\n d++;\n } \n \n }\n if(d!=nums2.size())\n {\n std::vector<int>::iterator it2(&nums2[d]);\n nums1.insert(nums1.end(),it2,nums2.end());\n }\n \n \n int medium=(nums1.size()/2);\n if(Is_odd(nums1.size()))\n {\n retval=nums1[medium];\n \n }\n else\n {\n \n retval=(double)(((double)nums1[medium-1]+(double)nums1[medium])/2);\n }\n return retval;\n }\n};", "memory": "96000" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n // Get the sizes of both input arrays.\n int n = nums1.size();\n int m = nums2.size();\n\n // Merge the arrays into a single sorted array.\n vector<int> merged;\n for (int i = 0; i < n; i++) {\n merged.push_back(nums1[i]);\n }\n for (int i = 0; i < m; i++) {\n merged.push_back(nums2[i]);\n }\n\n // Sort the merged array.\n sort(merged.begin(), merged.end());\n\n // Calculate the total number of elements in the merged array.\n int total = merged.size();\n\n if (total % 2 == 1) {\n // If the total number of elements is odd, return the middle element as the median.\n return static_cast<double>(merged[total / 2]);\n } else {\n // If the total number of elements is even, calculate the average of the two middle elements as the median.\n int middle1 = merged[total / 2 - 1];\n int middle2 = merged[total / 2];\n return (static_cast<double>(middle1) + static_cast<double>(middle2)) / 2.0;\n }\n }\n};", "memory": "96000" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n vector<int> sorted;\n sorted.insert(sorted.end(),nums1.begin(),nums1.end());\n sorted.insert(sorted.end(),nums2.begin(),nums2.end());\n sort(sorted.begin(),sorted.end());\n int s = sorted.size();\n int in=0;\n double median=0.0;\n if(s%2==0){\n in = s/2;\n median = (sorted[(s / 2)-1] + sorted[s / 2]) / 2.0;\n }else{\n median = sorted[s/2];\n }\n return median;\n }\n};", "memory": "96100" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n // Get the sizes of both input arrays.\n int n = nums1.size();\n int m = nums2.size();\n\n // Merge the arrays into a single sorted array.\n vector<int> merged;\n for (int i = 0; i < n; i++) {\n merged.push_back(nums1[i]);\n }\n for (int i = 0; i < m; i++) {\n merged.push_back(nums2[i]);\n }\n\n // Sort the merged array.\n sort(merged.begin(), merged.end());\n\n // Calculate the total number of elements in the merged array.\n int total = merged.size();\n\n if (total % 2 == 1) {\n // If the total number of elements is odd, return the middle element as the median.\n return static_cast<double>(merged[total / 2]);\n } else {\n // If the total number of elements is even, calculate the average of the two middle elements as the median.\n int middle1 = merged[total / 2 - 1];\n int middle2 = merged[total / 2];\n return (static_cast<double>(middle1) + static_cast<double>(middle2)) / 2.0;\n }\n }\n};", "memory": "96100" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n std::vector<int> result;\n\n for(int num : nums1) {\n result.push_back(num);\n }\n\n for(int num : nums2) {\n result.push_back(num);\n }\n\n sort(result.begin(), result.end());\n int n = result.size();\n\n return n % 2 ? result[n/2] : (result[n/2 - 1] + result[n/2]) / 2.0;\n }\n};", "memory": "96200" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n vector<int> sorted;\n sorted.insert(sorted.end(),nums1.begin(),nums1.end());\n sorted.insert(sorted.end(),nums2.begin(),nums2.end());\n sort(sorted.begin(),sorted.end());\n int s = sorted.size();\n int in=0;\n double median=0.0;\n if(s%2==0){\n in = s/2;\n median = (sorted[(s / 2)-1] + sorted[s / 2]) / 2.0;\n }else{\n median = sorted[s/2];\n }\n return median;\n }\n};", "memory": "96200" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n vector<int>v;\n for(auto num:nums1){\n v.push_back(num);\n }\n for(auto num:nums2){\n v.push_back(num);\n }\n sort(v.begin(),v.end());\n int n =v.size();\n return n % 2 ? v[n/2] : (v[n/2-1]+v[n/2])/2.0; \n }\n};", "memory": "96300" }
4
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p> <p>The overall run time complexity should be <code>O(log (m+n))</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3], nums2 = [2] <strong>Output:</strong> 2.00000 <strong>Explanation:</strong> merged array = [1,2,3] and median is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2], nums2 = [3,4] <strong>Output:</strong> 2.50000 <strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>nums1.length == m</code></li> <li><code>nums2.length == n</code></li> <li><code>0 &lt;= m &lt;= 1000</code></li> <li><code>0 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= m + n &lt;= 2000</code></li> <li><code>-10<sup>6</sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {\n vector<int>v;\n for(int i=0;i<nums1.size();i++){\n v.push_back(nums1[i]);\n }\n for(int i=0;i<nums2.size();i++){\n v.push_back(nums2[i]);\n }\n sort(v.begin(),v.end());\n int n=v.size();\n if(n%2){\n double ans=v[n/2];\n return ans;\n }\n else{\n double ans=(double)(v[n/2]+v[(n-1)/2])/2;\n return ans;\n }\n double p=0;\n return p; \n }\n};", "memory": "96300" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n string longestPalindrome(string &s) {\n\n int start=-1,end=-1;\n\n int n=s.length();\n int len=0;\n for(int i=0;i<n;++i){\n for(int j=n-1;j>=0;--j){\n if(s[i]==s[j] && len<(j-i+1)){\n int k=1;\n while(j-k>i+k && s[i+k]==s[j-k]){\n k++;\n }\n\n if(j-k<=i+k && len<j-i+1){\n start=i;end=j;\n len=j-i+1;\n }\n }\n\n }\n }\n\n return s.substr(start,len);\n\n }\n};", "memory": "11023" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n\n bool isPalindrome(const string& s, int i, int j) {\n while (i < j) {\n if (s[i] != s[j]) \n {\n return false;\n }\n i++;\n j--;\n }\n return true;\n }\n string longestPalindrome(string s) {\n int n = s.size();\n if (n == 0) return \"\";\n\n int maxLen = 0;\n int startIdx = 0;\n\n for (int i = 0; i < n; i++) {\n for (int j = i; j < n; j++) \n {\n if (isPalindrome(s, i, j)) \n {\n int currentLen = j - i + 1;\n if (currentLen > maxLen) \n {\n maxLen = currentLen;\n startIdx = i;\n }\n }\n }\n }\n return s.substr(startIdx, maxLen);\n }\n};\n\n\n// bool ispallindrome(string s)\n// {\n// int i = 0 ;\n// int j = s.size() - 1;\n// while(i <= j)\n// {\n// if(s[i] != s[j])\n// {\n// return false;\n// }\n// i++;\n// j--;\n// }\n// return true;\n// }\n\n// string longestPalindrome(string s) \n// {\n// int sp=0;\n// int n = s.size();\n// int maxlen = 0;\n// for(int i = 0 ; i < n ; i++)\n// {\n// for(int j = i ; j < n ; j++)\n// {\n// if(ispallindrome(s.substr(i , j-i+1)) == true)\n// {\n// int currentlen = j-i+1;\n// if(currentlen > maxlen)\n// {\n// maxlen = currentlen;\n// sp=i;\n// }\n// }\n// }\n// }\n// return s.substr(sp,maxlen);\n// }", "memory": "11023" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool palindromic(std::string& str) {\n int size = str.size();\n int size2 = size / 2;\n for (int i = 0; i < size2; i++) {\n if (str[i] != str[size - (i + 1)]) {\n return false;\n }\n }\n return true;\n }\n string longestPalindrome(string s) {\n std::vector<std::string> array_pol;\n std::string help = \"\";\n int pos = 0;\n int size = s.size();\n int max_pos = 0;\n int max_size = 0;\n int help_size = 0;\n while (pos != size) {\n for (int i = pos; s[i] != '\\0'; i++) {\n help += s[i];\n help_size = help.size();\n if (help_size > max_size && palindromic(help)) {\n array_pol.push_back(help);\n if (max_size < help_size) {\n max_pos = array_pol.size();\n max_size = help_size;\n }\n }\n }\n help = \"\";\n pos++;\n }\n\n return array_pol[max_pos - 1];\n }\n};", "memory": "17069" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool palindromic(std::string& str) {\n int size = str.size();\n int size2 = size / 2;\n for (int i = 0; i < size2; i++) {\n if (str[i] != str[size - (i + 1)]) {\n return false;\n }\n }\n return true;\n }\n string longestPalindrome(string s) {\n std::vector<std::string> array_pol;\n std::string help = \"\";\n int pos = 0;\n int size = s.size();\n int max_pos = 0;\n int max_size = 0;\n int help_size = 0;\n while (pos != size) {\n for (int i = pos; s[i] != '\\0'; i++) {\n help += s[i];\n help_size = help.size();\n if (help_size > max_size && palindromic(help)) {\n array_pol.push_back(help);\n if (max_size < help_size) {\n max_pos = array_pol.size();\n max_size = help_size;\n }\n }\n }\n help = \"\";\n pos++;\n }\n\n return array_pol[max_pos - 1];\n }\n};", "memory": "17069" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string longestPalindrome(string s) {\n int best_len = 0;\n string best_s = \"\";\n int n = s.length();\n for(int mid = 0; mid < n; mid++){\n for(int x = 0; mid - x >= 0 && mid + x < n; x++){\n if(s[mid-x] != s[mid+x]){\n break;\n }\n int len = 2 * x + 1;\n if(len > best_len){\n best_len = len;\n best_s = s.substr(mid - x, len);\n }\n }\n }\n\n for(int mid = 0; mid < n - 1; mid++){\n for(int x = 1; mid - x + 1 >= 0 && mid + x < n; x++){\n if(s[mid-x + 1] != s[mid+x]){\n break;\n }\n int len = 2 * x;\n if(len > best_len){\n best_len = len;\n best_s = s.substr(mid - x + 1, len);\n }\n }\n }\n return best_s;\n }\n};", "memory": "23115" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n string longestPalindrome(string s) {\n if(s.length()<2){\n return s;\n }\n int maxlen =0;\n string result =\"\" ;\n int n = s.length();\n \n for(int i=0;i<n;i++){\n int left =i;\n int right =i;\n while(left>=0 && right<n &&(s[left]==s[right])){\n if(right-left+1>maxlen){\n result = s.substr(left,right-left+1);\n maxlen=right-left+1;\n }\n left--;\n right++;\n }\n }\n \n for(int i=0;i<n;i++){\n int left =i;\n int right =i+1;\n while(left>=0 && right<n &&(s[left]==s[right])){\n if(right-left+1>maxlen){\n result = s.substr(left,right-left+1);\n maxlen=right-left+1;\n }\n left--;\n right++;\n }\n }\n \n return result ;\n }\n\n};", "memory": "23115" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool isPalindrome(string& temp, size_t length)\n {\n for(int i = 0; i < length/2 + 1; ++i)\n {\n if(temp[i] != temp[length - i - 1])\n return false;\n }\n return true;\n }\n\n string longestPalindrome(string s) {\n // not sure exactly if what I am thinking is dynamic programming, but,\n // 1 way to do this is, start from index 0, and then keep on going and from back, start checking \n // if paindrome, but that is very tedious, what else\n\n int length = s.length();\n map<int, string> mapp;\n\n // right now odd number one palindrome\n int left = 0, right = 0, center = 0;\n for(; center < length; ++center)\n {\n int sublength = 3;\n for(left = center - 1, right = center + 1; left >= 0 && right <= length - 1; left--, right++)\n {\n if(s[left] != s[right])\n break;\n else\n {\n if(mapp.find(sublength) == mapp.end())\n {\n mapp[sublength] = s.substr(left, right - left + 1);\n }\n }\n sublength += 2;\n }\n }\n\n // for even now\n for(center = 0; center < length - 1; ++center)\n {\n int sublength = 2;\n for(left = center, right = center + 1; left >= 0 && right <= length - 1; left--, right++)\n {\n if(s[left] != s[right])\n break;\n else\n {\n if(mapp.find(sublength) == mapp.end())\n {\n mapp[sublength] = s.substr(left, right - left + 1);\n }\n }\n sublength += 2;\n }\n }\n\n if(mapp.size() == 0)\n return string(1, s[0]);\n \n auto itr = prev(mapp.end());\n return itr->second;\n }\n};", "memory": "29161" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n std::string longestPalindrome(const std::string &s)\n {\n int n = s.length();\n if(n == 0) return \"\";\n int start = 0, max_length = 1;\n std::vector<std::vector<bool>> dp(n, std::vector<bool>(n, false));\n for(int i =0; i<n;i++) {\n dp[i][i] = true;\n }\n for(int i = 0; i< n-1;i++) {\n if(s[i] == s[i+1]) {\n dp[i][i+1] = true;\n start = i;\n max_length = 2;\n }\n }\n\n for(int len = 3; len< n+1;len++) {\n for(int i = 0; i < n- len+1;i++) {\n int j = i+len-1;\n if (s[i] == s[j] && dp[i+1][j-1]) {\n dp[i][j] = true;\n if(len > max_length) {\n max_length = len;\n start = i;\n }\n }\n }\n }\n\n return s.substr(start, max_length);\n\n }\n};", "memory": "29161" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "bool palindromic_check(string s)\n{\n bool flag = true;\n \n for (int i = 0; i < s.size() / 2; i++)\n {\n if (s[i] != s[s.size() - 1 - i])\n {\n flag = false;\n break;\n }\n }\n \n return flag;\n}\n\nstring check_for_odd(string s)\n{\n int n = s.size();\n \n vector<int> arr(n, 0);\n \n int max_length = 0;\n \n int i_max = 0;\n \n int l = 0, r = -1;\n \n for (int i = 0; i < n; i++)\n {\n int length = 0;\n \n if (i > r)\n {\n \n while ((i + length < n) && (i - length >= 0) && palindromic_check(s.substr(i - length, 2 * length + 1)))\n {\n length++;\n }\n \n length--;\n \n l = i - length;\n r = i + length;\n }\n else // if (i <= r)\n {\n length = arr[l + r - i];\n \n if (i + length < n && i - length >= 0)\n {\n \n while ((i + length < n) && (i - length >= 0) && palindromic_check(s.substr(i - length, 2 * length + 1)))\n {\n length++;\n }\n \n length--;\n \n if (r < i + length)\n {\n l = i - length;\n r = i + length;\n }\n }\n }\n \n arr[i] = length;\n \n if (length > max_length)\n {\n max_length = length;\n i_max = i;\n }\n }\n \n string answer = s.substr(i_max - max_length, 2 * max_length + 1);\n \n return answer;\n}\n\nstring check_for_even(string s)\n{\n {\n int n = s.size();\n \n vector<int> arr(n, 0);\n \n int max_length = 0;\n \n int i_max = 0;\n \n int l = 0, r = -1;\n \n for (int i = 0; i < n; i++)\n {\n int length = 0;\n \n if (i > r)\n {\n while ((i + length < n) && (i - length + 1 >= 0) && palindromic_check(s.substr(i - length + 1, 2 * length)))\n {\n length++;\n }\n \n length--;\n \n l = i - length + 1;\n r = i + length;\n }\n else // if (i <= r)\n {\n length = arr[l + r - i];\n \n if (i + length < n && i - length + 1 >= 0)\n {\n \n while ((i + length < n) && (i - length + 1 >= 0) && palindromic_check(s.substr(i - length + 1, 2 * length)))\n {\n length++;\n }\n \n length--;\n \n if (r < i + length)\n {\n l = i - length + 1;\n r = i + length;\n }\n }\n }\n \n arr[i] = length;\n \n if (length > max_length)\n {\n max_length = length;\n i_max = i;\n }\n }\n \n string answer = s.substr(i_max - max_length + 1, 2 * max_length);\n \n return answer;\n }\n}\n\nclass Solution\n{\npublic:\n string longestPalindrome(string s)\n {\n string ans1 = check_for_odd(s);\n string ans2 = check_for_even(s);\n \n string answer = ans1.size() > ans2.size() ? ans1 : ans2;\n \n return answer;\n }\n};\n", "memory": "35208" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string longestPalindrome(string s) {\n int l = s.length();\n\n if(l <= 1) return s;\n\n string ans = \"\";\n ans += s[0];\n\n for(int i = 0; i < l; ++i) {\n string cur = \"\";\n for(int j = 1; i-j >= 0 && i+j < l; ++j) {\n if(s[i-j] == s[i+j]) {\n cur += s[i+j];\n } else {\n break;\n }\n }\n\n string rev = cur;\n reverse(rev.begin(), rev.end());\n cur = rev + s[i] + cur;\n\n if(ans.length() < cur.length()) ans = cur;\n cur = \"\";\n\n for(int j = 0; i-j >= 0 && i+j+1 < l; ++j) {\n if(s[i-j] == s[i+j+1]) {\n cur += s[i+j+1];\n } else {\n break;\n }\n }\n\n rev = cur;\n reverse(rev.begin(), rev.end());\n cur = rev + cur;\n\n if(ans.length() < cur.length()) ans = cur;\n } \n \n return ans;\n }\n};", "memory": "35208" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string longestPalindrome(string s) {\n int n = s.size();\n int mx = 0;\n string ans;\n vector<vector<bool>> dp(n,vector<bool>(n,0));\n for(int i=0;i<n;i++){\n dp[i][i] = 1;\n for(int j=0;j<=i;j++){\n if(s[i]==s[j] && (i-j<=2 || dp[j+1][i-1])){\n dp[j][i] = true;\n if(i-j+1 > mx){\n mx = i-j+1;\n ans = s.substr(j,i-j+1);\n }\n }\n }\n }\n return ans;\n }\n};", "memory": "41254" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string longestPalindrome(string s) {\n int n = s.length();\n vector<vector<bool>> matrix(n, vector<bool>(n, false));\n int maxlen = 0;\n string res;\n for (int i = n - 1; i >= 0; i--) {\n for (int j = i; j < n; j++) {\n if (s[i] == s[j] && (j - i <= 2 || matrix[i + 1][j - 1])) {\n matrix[i][j] = true;\n if (j - i + 1 > maxlen) {\n maxlen = j - i + 1;\n res = s.substr(i, j - i + 1);\n }\n }\n }\n }\n return res;\n }\n};", "memory": "41254" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string longestPalindrome(string s) {\n int n = s.size();\n\n string ans = \"\";\n int Mx = 0;\n for(int i = 0; i < n; i++){ \n // case 1 : odd length\n // choose current ele as middle\n int l = i-1;\n int r = i+1;\n int ct = 1;\n string temp = \"\";\n temp += s[i];\n int lw = i;\n while(l >= 0 and r < n){\n if(s[l] == s[r]){\n lw = l;\n temp += s[l];\n temp += s[l];\n l--;\n r++;\n ct += 2;\n }\n else break;\n }\n // cout <<\"lw \" << lw << endl;\n temp = s.substr(lw,ct);\n // cout << \"ct \" << ct << \" \" << temp << endl;\n if(ct > Mx){\n Mx = ct;\n ans = temp;\n }\n\n // case 1 : even length\n int l1 = i;\n int r1 = i+1;\n int ct1 = 0;\n int lw1 = -1;\n string temp1 = \"\";\n while(l1 >= 0 and r1 < n){\n if(s[l1] == s[r1]){\n lw1 = l1;\n temp1 += s[l1];\n temp1 += s[l1];\n l1--;\n r1++;\n ct1 += 2;\n }\n else break;\n }\n if(lw1 == -1) continue;\n\n // cout <<\"lw1 \" << lw1 << endl;\n temp1 = s.substr(lw1,ct1);\n // cout << \"ct1 \" << ct1 << \" \" << temp1 << endl;\n if(ct1 > Mx){\n Mx = ct1;\n ans = temp1;\n }\n }\n return ans;\n }\n};", "memory": "47300" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string longestPalindrome(string s) {\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n int ans = 1;\n pair<int,int> ans_ind= {0,0};\n stack<pair<int,int>> st;\n for (int i = 0; i<s.length(); i++){\n st.push({i,i});\n if (i != s.length()-1 && s[i] == s[i+1]){\n st.push({i,i+1});\n ans = 2;\n ans_ind = {i,i+1};\n }\n\n }\n while(!st.empty()){\n auto [start,end] = st.top();\n // cout<<start<<\" \"<<end<<endl;\n st.pop();\n if (start == 0 || end == s.length()-1){\n continue;\n }\n else{\n if (s[start-1] == s[end+1]){\n if (end-start+3 > ans){\n ans = end-start+3;\n ans_ind = {start-1,end+1};\n }\n st.push({start-1,end+1});\n }\n }\n }\n string ans_string = \"\";\n for (int i = ans_ind.first; i<= ans_ind.second; i++){\n ans_string += s[i];\n }\n return ans_string;\n }\n};", "memory": "47300" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string longestPalindrome(string s) {\n string ans = \"\";\n auto check = [&](int l, int r, string str)\n {\n string left,right;\n while(l >= 0 && r < s.size())\n {\n if(s[l] == s[r])\n {\n left.push_back(s[l]);\n right.push_back(s[r]);\n l--;\n r++;\n }\n else\n {\n break;\n }\n }\n reverse(left.begin(),left.end());\n string tmp = left + str + right;\n return tmp;\n };\n for(int i = 0; i < s.size(); i++)\n {\n string str;\n str += s[i];\n string odd = check(i-1,i+1,str);\n string even;\n if(i+1 < s.size() && s[i] == s[i+1])\n {\n str += s[i+1];\n even = check(i-1,i+2,str);\n }\n // cout << i << \" \" << odd << \" \" << even << endl; \n if(odd.size() > ans.size()) ans = odd;\n if(even.size() > ans.size()) ans = even;\n }\n return ans;\n }\n};", "memory": "53346" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n //Checks if index value is valid for string\n bool IsBoundsValid(string& string_to_check, int index_value)\n {\n return (index_value >= 0 && index_value <= string_to_check.length());\n }\n //Finds longest palindromic substring possible by expanding left and right from a given center of the string\n string PalindromeFromCenter(string& string_to_check, int left_ptr, int right_ptr)\n {\n int max_left_limit = -1;\n int max_right_limit = -1;\n while(IsBoundsValid(string_to_check, left_ptr)\n && IsBoundsValid(string_to_check, right_ptr)\n && string_to_check[left_ptr] == string_to_check[right_ptr])\n {\n max_left_limit = left_ptr--;\n max_right_limit = right_ptr++;\n }\n // if(IsBoundsValid(string_to_check, max_left_limit) && IsBoundsValid(string_to_check, max_right_limit))\n // {\n // string palindrome = \"\";\n // return string_to_check.substr(max_left_limit, max_right_limit - max_left_limit + 1);\n // }\n return string_to_check.substr(left_ptr + 1, right_ptr - left_ptr - 1);\n }\n //Returns the longer of two strings. If both strings are of same length returns the first one\n string GetLongerString(string& string_1, string& string_2)\n {\n return (string_1.length() >= string_2.length() ? string_1 : string_2);\n }\n //Finds the longest palindromic substring in s\n string longestPalindrome(string s) {\n //Initialise result with first chaaracter of s as it is given length of s > 0\n string longest_palindrome = \"\";\n longest_palindrome += s[0];\n\n for(int i=0; i<s.length(); i++)\n {\n string palindrome = PalindromeFromCenter(s, i-1, i+1);\n longest_palindrome = GetLongerString(longest_palindrome, palindrome);\n if(IsBoundsValid(s, i+1) && s[i] == s[i+1])\n {\n palindrome = PalindromeFromCenter(s, i, i+1);\n longest_palindrome = GetLongerString(longest_palindrome, palindrome);\n }\n }\n return longest_palindrome;\n }\n};", "memory": "53346" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string longestPalindrome(const string &s) {\n string longest = \"\";\n int current_size = 0;\n int left = 0, right = 0;\n for (int i = 0; i < s.size(); ++i){\n left = i;\n right = i;\n checkPalingdrome(s, left, right);\n if(left >= 0 && right >= 0){\n current_size = right - left + 1;\n // std::cout << \"size: \" << current_size << std::endl;\n longest = (current_size > longest.size()) ? s.substr(left, current_size): longest; \n }\n\n left = i;\n right = i + 1;\n checkPalingdrome(s, left, right);\n if(left >= 0 && right >= 0){\n current_size = right - left + 1;\n // std::cout << \"size: \" << current_size << std::endl;\n longest = (current_size > longest.size()) ? s.substr(left, current_size): longest; \n }\n // std::cout<< odd << std::endl;\n // std::cout<< even << std::endl;\n \n } \n // std::cout<< longest << std::endl;\n return longest;\n }\n\n void checkPalingdrome(const string &s, int &left, int &right){\n if (left < 0 || right >= s.size()){\n left = -1;\n right = -1;\n return ;\n }\n\n while(left >= 0 && right <= s.size() - 1 && s[left] == s[right]){\n // std::cout << \"left: \" << left << \" - right: \" << right <<std::endl;\n left--;\n right++;\n \n }\n // std::cout<< s.substr(left + 1, right - left - 1) << std::endl;\n // return s.substr(left + 1, right - left - 1);\n // std::cout << \"Palingdrome from \" << left + 1 << \" to \" << right - 1 <<std::endl;\n left++;\n right--;\n // return \"\";\n \n }\n\n\n};", "memory": "59393" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool checkPalindrome(string s, int start, int end){\n while(start < end){\n if(s[start] != s[end]) return false;\n start++;\n end--;\n }\n return true;\n }\n\n string longestPalindrome(string s) {\n int n = (int) s.size();\n if(n == 1) return s;\n int arr[n];\n arr[0] = 0;\n for(int i=1; i<n; i++){\n if(arr[i-1]-1 >=0 && s[arr[i-1]-1] == s[i]){\n arr[i] = arr[i-1] -1;\n continue;\n }\n int index = arr[i-1];\n arr[i] = i;\n while(index < i){\n if(checkPalindrome(s, index, i)){\n arr[i] = index;\n break;\n }\n index++;\n }\n }\n string answer = \"\";\n for(int i=0; i<n; i++){\n string sub = s.substr(arr[i], i-arr[i]+1);\n if (sub.size() > answer.size()) answer = sub;\n }\n return answer;\n }\n};", "memory": "95670" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string longestPalindrome(string s) {\n int sum=0;\n vector<pair<int,string>> vp;\n for(int i=0;i<s.length();i++){\n int l=i;\n int length=0;\n string d=\"\";\n for(int j=s.length()-1;j>=l;j--){\n if(s[j]==s[l] && j!=l){\n l++;\n length+=2;\n d+=s[j];\n }\n else if(s[j]==s[l] && j==l){\n length+=1;\n d+=s[j];\n l++;\n break;\n }\n else if(length!=0 && s[j]!=s[l]){\n j+=l-i;\n length=0;\n d.clear();\n l=i;\n }\n }\n if(length==1){\n vp.push_back({length,d});\n }\n else if(length%2==0){\n string e=d;\n reverse(e.begin(),e.end());\n d+=e;\n vp.push_back({length,d});\n }\n else if(length%2!=0){\n string e=d.substr(0,length/2);\n reverse(e.begin(),e.end());\n d+=e;\n vp.push_back({length,d});\n }\n }\n sort(vp.begin(),vp.end());\n reverse(vp.begin(),vp.end());\n for(auto x:vp){\n cout<<x.second<<\" \";\n }\n return vp[0].second;\n }\n};", "memory": "95670" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool isPalindrome(string s) {\n int left = 0, right = s.length() - 1;\n while (left < right) {\n if (s[left] != s[right]) return false;\n left++;\n right--;\n }\n return true;\n }\n\n bool palindromeExist(string s, int len) {\n int left = 0;\n while (left + len <= s.length()) {\n if (isPalindrome(s.substr(left, len))) {\n return true;\n }\n left++;\n }\n return false;\n }\n\n string findPalindrome(string s, int len) {\n int left = 0;\n while (left + len <= s.length()) {\n if (isPalindrome(s.substr(left, len))) {\n return s.substr(left, len);\n }\n left++;\n }\n return \"\";\n }\n\n int findEvenPalindrome(string s) {\n if (!palindromeExist(s, 2)) {\n return 0;\n }\n int left = 2, right = s.length(), mid, ans;\n while (left <= right) {\n mid = (left + right) / 2;\n if (mid % 2 != 0) mid--;\n if (palindromeExist(s, mid)) {\n ans = mid;\n left = mid + 2;\n } else {\n right = mid - 2;\n }\n }\n return ans;\n }\n\n int findOddPalindrome(string s) {\n if (!palindromeExist(s, 3)) {\n return 1;\n }\n int left = 3, right = s.length(), mid, ans;\n while (left <= right) {\n mid = (left + right) / 2;\n if (mid % 2 == 0) mid--;\n if (palindromeExist(s, mid)) {\n ans = mid;\n left = mid + 2;\n } else {\n right = mid - 2;\n }\n }\n return ans;\n }\n\n string longestPalindrome(string s) {\n if (s.length() == 1) return s;\n if (s.length() == 2) {\n if (isPalindrome(s)) {\n return s;\n }\n return s.substr(0, 1);\n }\n \n return findPalindrome(s, max(findEvenPalindrome(s), findOddPalindrome(s)));\n }\n};", "memory": "101716" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n void setlongestPalindrome(string s, int index, int& maxCount, string& maxString){\n int count = 1;\n int prev = index - 1;\n int next = index + 1;\n while(prev>=0 && next<s.size()){\n if(s[prev] == s[next]){\n count += 2;\n }else{\n break;\n }\n if(count > maxCount){\n //cout<<s<<endl;\n //cout<<\"Odd\"<<endl;\n maxString = s.substr(prev, next-prev+1);\n maxCount = count;\n //cout<<maxString<<endl;\n }\n --prev;\n ++next;\n }\n prev = index;\n next = index + 1;\n count = 0;\n while(prev>=0 && next<s.size()){\n if(s[prev] == s[next]){\n count += 2;\n }else{\n break;\n }\n if(count > maxCount){\n //cout<<s<<endl;\n //cout<<\"Even\"<<endl;\n maxString = s.substr(prev, next-prev+1);\n maxCount = count;\n //cout<<maxString<<endl;\n }\n --prev;\n ++next;\n }\n }\n string longestPalindrome(string s) {\n if(s.empty()){\n return s;\n }\n int maxCount = 1;\n string maxString = s.substr(0,1);\n for(int i=0; i<s.size(); i++){\n setlongestPalindrome(s, i, maxCount, maxString);\n }\n return maxString;\n }\n};", "memory": "101716" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string result;\n int get_len(int itr1,int itr2,string s)\n {\n int len=0;\n while(itr1>-1 and itr2<s.size())\n {\n if(s[itr1]==s[itr2])\n {\n len++;\n itr1--;\n itr2++;\n }\n else\n {\n break;\n }\n }\n return len;\n }\n\n string longestPalindrome(string s) \n {\n int max_ans=0;\n for(int itr=0;itr<s.size();itr++)\n {\n int ans=0;\n cout<<itr<<endl;\n \n if(itr>0 and s[itr-1]==s[itr])\n {\n ans=get_len(itr-1,itr,s);\n cout<<\"ans=\"<<ans<<endl;\n if(max_ans<ans*2)\n {\n max_ans=ans*2;\n result=s.substr(itr-ans,ans*2);\n }\n \n }\n if(itr>0 and itr<s.size())\n {\n ans=get_len(itr-1,itr+1,s);\n cout<<\"ans=\"<<ans<<endl;\n if(max_ans<ans*2+1)\n {\n max_ans=ans*2+1;\n result=s.substr(itr-ans,(ans*2)+1);\n }\n ans++;\n }\n if(max_ans==0)\n {\n max_ans=1;\n result=s[itr];\n \n }\n max_ans=max(max_ans,ans);\n\n\n }\n\n return result;\n\n }\n};", "memory": "107763" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string result;\n int get_len(int itr1,int itr2,string s)\n {\n int len=0;\n while(itr1>-1 and itr2<s.size())\n {\n if(s[itr1]==s[itr2])\n {\n len++;\n itr1--;\n itr2++;\n }\n else\n {\n break;\n }\n }\n return len;\n }\n\n string longestPalindrome(string s) \n {\n int max_ans=0;\n for(int itr=0;itr<s.size();itr++)\n {\n int ans=0;\n cout<<itr<<endl;\n \n if(itr>0 and s[itr-1]==s[itr])\n {\n ans=get_len(itr-1,itr,s);\n //cout<<\"ans=\"<<ans<<endl;\n if(max_ans<ans*2)\n {\n max_ans=ans*2;\n result=s.substr(itr-ans,ans*2);\n }\n \n }\n if(itr>0 and itr<s.size())\n {\n ans=get_len(itr-1,itr+1,s);\n //cout<<\"ans=\"<<ans<<endl;\n if(max_ans<ans*2+1)\n {\n max_ans=ans*2+1;\n result=s.substr(itr-ans,(ans*2)+1);\n }\n ans++;\n }\n if(max_ans==0)\n {\n max_ans=1;\n result=s[itr];\n \n }\n max_ans=max(max_ans,ans);\n\n\n }\n\n return result;\n\n }\n};", "memory": "107763" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string expand(int st, int dr, string s){\n string temp;\n while (st >= 1 && dr < s.length() - 1 && s[st - 1] == s[dr + 1]) {\n st--;\n dr++;\n }\n temp = s.substr(st, dr-st+1);\n return temp;\n }\n string longestPalindrome(string s) {\n string s1, s2;\n string maxim = string(1, s[0]);\n for(int i = 0; i < s.length()-1; i ++){\n s1 = expand(i , i , s);\n if(s[i] == s[i+1]) s2 = expand(i, i+1, s);\n if(s1.length() > maxim.length()) maxim = s1;\n if(s2.length() > maxim.length()) maxim = s2;\n }\n return maxim;\n }\n \n};", "memory": "113809" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int ans_start = 0, ans_end = 0;\n string longestPalindrome(string s) {\n ans_start = 0; ans_end = 0;\n int N = s.size();\n for (int mid = 0; mid < s.size(); mid++) {\n expand(s,mid, mid);\n if (mid + 1 < N && s[mid] == s[mid+1]) {\n expand(s,mid, mid+1);\n }\n }\n return s.substr(ans_start, ans_end - ans_start + 1);\n }\n \n void expand(string s, int start, int end) {\n int N = s.size();\n while(start-1>= 0 && end+1 < N && s[start-1] == s[end+1]) {\n start--;\n end++;\n }\n cout << \"current=\" << s.substr(start, end-start+1) << endl;\n if ((ans_end - ans_start < end - start) ) {\n cout << \"Updated ans\" << endl;\n ans_start = start;\n ans_end = end;\n }\n }\n};", "memory": "113809" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string longestPalindrome(string s) {\n\n auto isPalindrome = [](string_view sv) {\n for (size_t i = 0; i < sv.size() / 2; ++i) {\n if (sv[i] != sv[sv.size() - i - 1]) {\n return false;\n }\n }\n return true;\n };\n\n vector<string_view> palendromes;\n palendromes.reserve(1000);\n \n for (size_t i = 0; i < s.size(); ++i) {\n for (size_t j = s.size(); j > i; --j) {\n string_view sv{&s[i], &s[j]};\n if (isPalindrome(sv)) {\n palendromes.push_back(sv);\n }\n }\n }\n\n return string{*ranges::max_element(palendromes, [](auto &a, auto &b) { return a.size() < b.size(); })};\n }\n};", "memory": "119855" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int getPalindromFromMiddle(string s, int start, int end)\n {\n if (s == \"\" || start > end) \n return 0;\n auto size = s.length();\n \n while (start >= 0 && end < size && s[start] == s[end])\n {\n \n start--;\n end++;\n }\n return end - start -1;\n }\n string longestPalindrome(string s) {\n \n int i = 0;\n int size = s.length();\n\n int start = 0, end = 0;\n int len1 = 0;\n int len2 = 0;\n int current_len = 0;\n while (i<size-1)\n {\n if (size -i < current_len/2)\n break;\n len1 = getPalindromFromMiddle(s, i,i );\n len2 = getPalindromFromMiddle(s, i,i+1 );\n \n len1 = max(len1, len2);\n if (len1 > current_len)\n {\n start = i - (len1-1)/2;\n end = i + len1/2;\n current_len = end -start +1;\n }\n \n \n \n i++;\n }\n return s.substr(start, end-start+1);\n }\n};\n", "memory": "119855" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int expandAroundCenter(const string &s, int left, int right) {\n while (left >= 0 && right < s.length() && s[left] == s[right]) {\n left--;\n right++;\n }\n return right - left - 1;\n}\nbool isPalindrome(const string &s) {\n int l = 0, r = s.length() - 1;\n while (l < r) {\n if (s[l] != s[r]) return false;\n l++;\n r--;\n }\n return true;\n}\n string longestPalindrome(string str) {\n int n = str.length();\n int start = 1, end = n;\n int ans = INT_MIN;\n string ret=\"\";\n while (start <= end) {\n int mid = (start + end) / 2;\n int mid2 = mid + 1;\n bool foundPalindrome = false;\n\n for (int i = 0; i <= n - mid; ++i) {\n string t = str.substr(i, mid);\n if (t.length() == mid && isPalindrome(t)) {\n ret=t;\n foundPalindrome = true;\n }\n }\n\n for (int i = 0; i <= n - mid2; ++i) {\n string t = str.substr(i, mid2);\n if (t.length() == mid2 && isPalindrome(t)) {\n ret=t;\n foundPalindrome = true;\n }\n }\n\n if (foundPalindrome) {\n start = mid + 1;\n } else {\n end = mid - 1;\n }\n }\n return ret;\n }\n};", "memory": "125901" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string longestPalindrome(string s) {\n int res = 0;\n\n for (int i = 0; i < s.size(); i++) {\n res = max(res, lengthPalindrome(s, i).second);\n }\n for (int i = 0; i < s.size(); i++) {\n pair<int, int> length = lengthPalindrome(s, i);\n if (length.second == res)\n return s.substr(length.first, length.second);\n }\n return \"\";\n }\n\n pair<int, int> lengthPalindrome(string s, int index) {\n int expand = 0;\n\n while (index - expand >= 0 && index + expand < s.size() && s[index - expand] == s[index + expand])\n expand++;\n\n int expandP = 0;\n while (index - expandP >= 0 && index + 1 + expandP < s.size() && s[index - expandP] == s[index + 1 + expandP])\n expandP++;\n if (expand <= expandP)\n return {index - expandP + 1, expandP * 2};\n else\n return {index - expand + 1, expand * 2 - 1};\n }\n};", "memory": "125901" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n // vector<string> ans;\n\n bool isPalin(string s) {\n\n int n = s.size();\n int i = 0, j = n - 1;\n\n while (i <= j) {\n if (s[i] != s[j])\n return false;\n i++;\n j--;\n }\n\n return true;\n }\n\n // void solve(int i, int j, string s) {\n // int n = s.size();\n // int i = 0, j = n - 1;\n // while (i <= j) {\n\n // if (isPalin())\n // }\n // }\n string longestPalindrome(string s) {\n\n int n = s.size();\n int len = 0;\n string ans = \"\";\n\n int dp[1005][1005];\n memset(dp, -1, sizeof(dp));\n\n for (int i = 0; i < n; i++) {\n string str = \"\";\n // str+=s[i];\n // if (isPalin(str) && str.size() > len) {\n // len = str.size();\n // ans = str;\n // }\n for (int j = i; j < n; j++) {\n\n str += s[j];\n // if (isPalin(str) && str.size() > len) {\n // len = str.size();\n // ans = str;\n // }\n if (dp[i][j] == 0)\n continue;\n if (dp[i][j] != -1 && dp[i][j] == 1) {\n\n if (str.size() > len) {\n len = str.size();\n ans = str;\n }\n } else {\n\n int a = i;\n int b = j;\n bool f = true;\n\n while (a <= b) {\n\n if (dp[a][b] == -1) {\n if (s[a] == s[b]) {\n a++;\n b--;\n } else {\n f = false;\n break;\n }\n } else if (dp[a][b] == 1)\n break;\n else if (dp[a][b] == 0) {\n f = false;\n break;\n }\n }\n if (f) {\n dp[i][j] = 1;\n if (str.size() > len) {\n len = str.size();\n ans = str;\n }\n } else\n dp[i][j] = 0;\n }\n }\n }\n\n return ans;\n }\n};", "memory": "131948" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int dp[1001][1001];\n bool isPalin(int i,int j,string &s){\n if(i>=j) return true;\n if(dp[i][j]!=-1) return dp[i][j];\n\n bool ans=true;\n while(i<j){\n if(s[i]!=s[j]){\n ans=false;\n break;\n }\n i++;\n j--; \n }\n \n return dp[i][j]=ans;\n }\n string longestPalindrome(string s) {\n memset(dp,-1,sizeof(dp));\n int max_len=1,si=0,n=s.size();\n\n for(int i=0;i<n;i++){\n string temp=\"\";\n for(int j=i;j<n;j++){\n temp+=s[j];\n if(isPalin(i,j,s)){\n if(j-i+1>max_len){\n max_len=j-i+1;\n si=i;\n }\n }\n }\n }\n\n return s.substr(si,max_len);\n }\n};", "memory": "131948" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string spread(string s,int l,int r){\n while(l >= 0 && r < s.length() && s[l] == s[r]){\n l--;\n r++;\n }\n return s.substr(l+1,r-l-1);\n }\n string longestPalindrome(string s){\n string res;\n for(int i = 0;i < s.length();i++){\n string odd = spread(s,i,i);\n string even = spread(s,i,i+1);\n if(odd.length() < even.length()){\n odd = even;\n }\n if(res.length() < odd.length()){\n res = odd;\n }\n if(res == s) return s;\n }\n return res;\n }\n};", "memory": "137994" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string spread(string s,int l,int r){\n while(l >= 0 && r < s.length() && s[l] == s[r]){\n l--;\n r++;\n }\n return s.substr(l+1,r-l-1);\n }\n string longestPalindrome(string s){\n string res;\n for(int i = 0;i < s.length();i++){\n string odd = spread(s,i,i);\n string even = spread(s,i,i+1);\n if(odd.length() < even.length()){\n odd = even;\n }\n if(res.length() < odd.length()){\n res = odd;\n }\n if(res == s) return s;\n }\n return res;\n }\n};", "memory": "137994" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string longestPalindrome(string s) {\n int n=s.size();\n vector<vector<bool>>dp(n+1,vector<bool>(n+1,false));\n for(int i=0;i<=n;i++){\n dp[n][i]=true;\n dp[i][i]=true;\n }\n for(int i=n-2;i>=0;i--){\n for(int j=i+1;j<=n-1;j++){\n if(s[i]!=s[j]){\n dp[i][j]=false;\n }else{\n if(j-1<=i+1){\n dp[i][j]=true;\n }else{\n dp[i][j]=dp[i+1][j-1];\n }\n }\n }\n }\n string s1=\"\";\n for(int i=0;i<n;i++){\n string res=\"\";\n for(int j=i;j<n;j++){\n res+=s[j];\n if(dp[i][j]==true){\n if(s1.size()<res.size()){\n s1=res;\n }\n }\n }\n }\n return s1;\n \n }\n};", "memory": "144040" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string longestPalindrome(string s) {\n int n=s.size();\n vector<vector<bool>> dp(n, vector<bool> (n,0));\n string ans=\"\"; int cnt=0;\n for(int i=0;i<n;i++){dp[i][i]=1;}\n for(int i=n-2;i>=0;i--){\n string temp=\"\";\n temp+=s[i];\n for(int j=i+1;j<n;j++){\n temp+=s[j];\n if(s[i]==s[j]){\n if((i+1)<=(j-1)){\n dp[i][j]=dp[i+1][j-1];\n }else{\n dp[i][j]=1;\n }\n if(dp[i][j] && (j-i+1)>cnt){\n cnt=j-i+1;\n ans=temp;\n }\n }\n }\n }\n if(cnt==0){return ans+s[0];}\n return ans;\n }\n};", "memory": "144040" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string longestPalindrome(string s) \n {\n int longest=0,start=0;\n int explored=0;\n queue<pair<int,int>> q;\n q.push(make_pair(0,s.length()));\n while(!q.empty())\n {\n int left=q.front().first;\n int right=q.front().second;\n //cout<<left<<\" \"<<right<<endl;\n q.pop();\n if(explored==s.length())\n break;\n if(ispalindrome(s,left,right))\n {\n longest=right-left;\n start=left;\n break;\n }\n if(right==s.length())\n {\n explored++;\n left=0;\n right=s.length()-explored;\n }\n else\n {\n left++;\n right++;\n }\n q.push(make_pair(left,right));\n }\n return s.substr(start,longest);\n }\n bool ispalindrome(string & s,int left,int right)\n {\n for(int i=0;i<(right-left)/2;i++)\n if(s[i+left]!=s[right-i-1])\n return false;\n return true;\n }\n};", "memory": "150086" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n \n bool solve( int i ,string &p , int n){\n if(i>=n/2) return true;\n \n if(p[i] != p[n-i-1]) return false;\n return solve(i+1,p,n);\n }\n\n string solve(string &s){\n int n = s.size();\n int maxlen = INT_MIN;\n string str = \"\";\n unordered_map<string ,int>umpp;\n \n for(int i =0 ;i<n;i++){\n string p=\"\";\n for(int j =i;j<n;j++){\n p+=s[j];\n if( solve(0 ,p , p.size())){\n int x = p.size();\n maxlen = max(maxlen ,x);\n umpp[p] = x;\n }\n }\n }\n \n for(auto it : umpp){\n if(it.second == maxlen){\n str = it.first;\n break;\n }\n }\n return str;\n }\n\n string longestPalindrome(string s) {\n return solve(s);\n \n }\n};", "memory": "150086" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string longestPalindrome(string s) {\n int n=s.size();\n string s1=s;\n reverse(s1.begin(),s1.end());\n if(s1==s)\n return s;\n vector<vector<int>> dp(n,vector<int>(n,0));\n int maxi=1;\n string ans=\"\";\n ans+=s[0];\n for(int i=0;i<n;i++)\n dp[i][i]=1;\n for(int i=1;i<n;i++)\n {\n if(s[i-1]==s[i])\n {\n dp[i-1][i]=1;\n maxi=2;\n ans=s.substr(i-1,2);\n }\n }\n for (int length = 3; length <= n; length++) { // Length from 3 to n\n for (int i = 0; i <= n - length; i++) { // Start index\n int j = i + length - 1; // End index\n if (s[i] == s[j] && dp[i + 1][j - 1]) {\n dp[i][j] = 1;\n if (length > maxi) {\n maxi = length;\n ans = s.substr(i, length);\n }\n }\n }\n }\n // for(int i=0;i<n;i++)\n // {\n // for(int j=0;j<n;j++)\n // {\n // cout<<dp[i][j]<<\" \";\n // }\n // cout<<endl;\n // }\n return ans;\n }\n};", "memory": "156133" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int isPalindrome(string& s, int i, int j, vector<vector<tuple<bool,bool>>>& cache) {\n if (i>j) return true;\n auto& [isSet, value] = cache[i][j];\n if (isSet) return value;\n\n if (s[i] == s[j]) {\n value = isPalindrome(s, i+1, j-1, cache);\n } else {\n value = false;\n }\n\n isSet = true;\n return value;\n }\n\n string longestPalindrome(string s) {\n vector<vector<tuple<bool,bool>>> cache(s.size(), vector<tuple<bool,bool>>(s.size(), { false, false }));\n\n int maxlen = 1, maxi=0, maxj=0;\n for(int i=0;i<s.size();i++) {\n for(int j=i+1;j<s.size();j++) {\n if (isPalindrome(s, i, j, cache)) {\n if (j-i+1 > maxlen) {\n maxlen = j-i+1;\n maxi = i;\n maxj = j;\n }\n }\n }\n }\n\n return s.substr(maxi, maxj-maxi+1);\n }\n};", "memory": "156133" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n string longestPalindrome(string s) {\n int n = s.size(), begin = 0, sz = 1;\n std::vector<std::vector<short>> dp(n, std::vector<short>(n, false));\n for (int i = 0; i < n; ++i) {\n dp[i][i] = true;\n }\n for (int i = n - 2; i >= 0; --i) {\n if (s[i] == s[i + 1]) {\n begin = i;\n sz = 2;\n dp[i][i + 1] = true;\n }\n }\n for (int len = 3; len <= n; ++len) {\n for (int i = 0, j = len - 1; j < n; ++i, ++j) {\n if (s[i] == s[j] && dp[i + 1][j - 1]) {\n begin = i;\n sz = len;\n dp[i][j] = true;\n }\n }\n }\n return s.substr(begin, sz);\n }\n};", "memory": "162179" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int isPalindrome(string& s, int i, int j, vector<vector<tuple<bool,bool>>>& cache) {\n if (i>j) return true;\n if (i==j) return true;\n auto& [isSet, value] = cache[i][j];\n\n if (isSet) return value;\n\n if (s[i] == s[j]) {\n value = isPalindrome(s, i+1, j-1, cache);\n } else {\n value = false;\n }\n\n isSet = true;\n return value;\n }\n\n\n string longestPalindrome(string s) {\n vector<vector<tuple<bool,bool>>> cache(s.size(), vector<tuple<bool,bool>>(s.size(), { false, false }));\n\n int maxlen = 1, maxi=0, maxj=0;\n for(int i=0;i<s.size();i++) {\n for(int j=i+1;j<s.size();j++) {\n if (isPalindrome(s, i, j, cache)) {\n if (j-i+1 > maxlen) {\n maxlen = j-i+1;\n maxi = i;\n maxj = j;\n }\n }\n }\n }\n\n return s.substr(maxi, maxj-maxi+1);\n }\n};", "memory": "162179" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
3
{ "code": "class Solution {\n int ml= -1;\n int mr = -1;\n int mlen = -1;\npublic:\n void getPalindromFromMiddle(string s, int start, int end)\n {\n if (s == \"\" ) \n return;\n int tmpr = start+end;\n int tmpl = start;\n while (tmpl >= 0 && tmpr < s.size() )\n {\n if (s[tmpl] == s[tmpr]){\n if (mlen < tmpr -tmpl +1) {\n mlen = tmpr-tmpl+1;\n mr = tmpr;\n ml= tmpl;\n }\n tmpl--;\n tmpr++;\n } else {\n break;\n }\n }\n // cout << \"end\" << end << \"start\" << start << \"mlen\" << mlen << \"ml\" << ml << \"mr\" << mr << endl;\n \n }\n string longestPalindrome(string s) {\n \n int i = 1;\n \n s.insert(s.begin(), '#');\n while (i<s.size())\n {\n // if (size -i < mlen/2)\n // break;\n getPalindromFromMiddle(s, i,0 );\n getPalindromFromMiddle(s, i,1 );\n \n i++;\n }\n return s.substr(ml, mr - ml +1);\n }\n};\n", "memory": "168225" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxi=0;\n string ans;\n void palin(string s, int i, int j)\n {\n\n while(i>=0 && j<s.length())\n {\n if(s[i]!=s[j])\n {\n if(j-i-1>maxi)\n {\n maxi=j-i-1;\n ans.clear();\n for(int k=i+1;k<j;k++)\n ans.push_back(s[k]);\n }\n break;\n }\n i--;\n j++;\n }\n if(s[i+1]==s[j-1])\n {\n if(j-i-1>maxi)\n {\n maxi=j-i-1;\n ans.clear();\n for(int k=i+1;k<j;k++)\n ans.push_back(s[k]);\n }\n }\n }\n string longestPalindrome(string s) {\n int i=0;\n int j=0;\n for(int i=0;i<s.length();i++)\n {\n palin(s,i,i);\n palin(s,i,i+1);\n }\n return ans;\n }\n};", "memory": "168225" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string longestPalindrome(string s) {\n if(s.size() == 0) {\n return \"\";\n }\n int ls = 0;\n vector<int> res;\n for(int i = 0; i < s.size(); i ++) {\n vector<int> ind1 = find_palin(i, i, s);\n vector<int> ind2 = find_palin(i, i+1, s);\n int l1 = ind1[1] - ind1[0] +1;\n int l2 = ind2[1] - ind2[0] +1;\n if(l1 < l2) {\n if(l2 >= ls) {\n ls = l2;\n res = ind2;\n }\n } else {\n if(l1 >= ls) {\n ls = l1;\n res = ind1;\n }\n }\n }\n return s.substr(res[0], res[1] - res[0]+1);\n }\n \nvector<int> find_palin(int ind1, int ind2, string s) {\n if(ind2 >= s.size()) {\n return {ind1, ind1};\n }\n while(ind1 >=0 and ind2 < s.size() and s[ind1] == s[ind2]) {\n ind1--;\n ind2++;\n }\n return {ind1+1, ind2-1};\n}\n \n \n};", "memory": "174271" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> expand(string s,int left,int right)\n {\n vector<int> indexes(2,-1);\n while(left>=0 && right<s.length())\n {\n if(s[left]==s[right])\n {\n indexes.clear();\n indexes.push_back(left);\n indexes.push_back(right);\n left--;\n right++;\n }\n else\n {\n break;\n }\n }\n return indexes;\n }\n string longestPalindrome(string s) {\n int n=s.length();\n vector<int> oddL;\n vector<int> evenL;\n int length=0;\n vector<int> ans;\n int start,end;\n for(int i=0;i<n;i++)\n {\n oddL=expand(s,i,i);\n if(oddL[1]-oddL[0]+1>length)\n {\n length=oddL[1]-oddL[0]+1;\n ans.clear();\n ans=oddL;\n }\n evenL=expand(s,i,i+1);\n if(evenL[1]-evenL[0]+1>length)\n {\n length=evenL[1]-evenL[0]+1;\n ans.clear();\n ans=evenL;\n }\n }\n start=ans[0];\n end=ans[1];\n string temp=\"\";\n for(int i=start;i<=end;i++)\n {\n temp+=s[i];\n }\n return temp;\n }\n};", "memory": "174271" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int extend(int a, int b, int N, string s){\n int i = 0; \n while(a-i >= 0 && b+i < N && s[a-i] == s[b+i]) i++; \n return i; \n }\n string longestPalindrome(string s) {\n string sm = \"#\";\n for(int i = 0; i < s.length(); i++){\n sm += s[i];\n sm += '#'; \n }\n // cout << sm << '\\n';\n int sm_len = sm.length();\n int z[1020*2] = {0};\n z[0] = 1; \n int L = 0, R = 0; \n \n for(int i = 1; i < sm_len; i++){\n int ii = L - (i-L); // i 的映射位置\n // int ii; \n int n = R+1-i; // 已知回文的右範圍\n\n if(i > R){\n z[i] = extend(i, i, sm_len, sm);\n L = i; // L 回文的中間點\n R = i+z[i]-1; \n }else if(z[ii] == n){ //能找到更大的回文\n z[i] = n + extend(i-n, i+n, sm_len, sm);\n L = i; \n R = i+z[i]-1; \n }else{ // 沒有更大的回文或已知這邊也是最大的回文\n z[i] = min(z[ii], n); // TODO: unknown\n // z[i] = z[ii];\n } \n }\n\n int n = 0, p = 0; \n for(int i = 0; i < sm_len; i++){\n if(z[i] > n){\n n = z[i];\n p = i; \n }\n }\n\n string ans = \"\";\n // cout << p-z[p]+1 << ' ' << p+z[p] << '\\n';\n for(int i = p-z[p]+1; i < p+z[p]; i++){\n if(i&1) ans += sm[i]; \n }\n return ans; \n }\n};", "memory": "216595" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int extend(int a, int b, int N, string s){\n int i = 0; \n while(a-i >= 0 && b+i < N && s[a-i] == s[b+i]) i++; \n return i; \n }\n string longestPalindrome(string s) {\n string sm = \"#\";\n for(int i = 0; i < s.length(); i++){\n sm += s[i];\n sm += '#'; \n }\n // cout << sm << '\\n';\n int sm_len = sm.length();\n int z[1020*2] = {0};\n z[0] = 1; \n int L = 0, R = 0; \n \n for(int i = 1; i < sm_len; i++){\n int ii = L - (i-L); // 最左邊 -\n // int ii; \n int n = R+1-i; // 已知回文的右範圍\n\n if(i > R){\n z[i] = extend(i, i, sm_len, sm);\n L = i; \n R = i+z[i]-1; \n }else if(z[ii] == n){\n z[i] = n + extend(i-n, i+n, sm_len, sm);\n L = i; \n R = i+z[i]-1; \n }else{\n z[i] = min(z[ii], n); // TODO: unknown\n } \n }\n\n int n = 0, p = 0; \n for(int i = 0; i < sm_len; i++){\n if(z[i] > n){\n n = z[i];\n p = i; \n }\n }\n\n string ans = \"\";\n // cout << p-z[p]+1 << ' ' << p+z[p] << '\\n';\n for(int i = p-z[p]+1; i < p+z[p]; i++){\n if(i&1) ans += sm[i]; \n }\n return ans; \n }\n};", "memory": "216595" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string longestPalindrome(string s) {\n uint32_t startIndex = 0;\n uint32_t endIndex = 0;\n for (uint32_t i=0; i<s.size(); i++) {\n for (uint32_t j=i; j<s.size(); j++) {\n if (isPalindrome(s, i, j)) {\n const auto candidateLength = j - i;\n const auto longestLength = endIndex - startIndex;\n if (candidateLength > longestLength) {\n startIndex = i;\n endIndex = j;\n }\n }\n }\n }\n\n std::string result;\n for (uint32_t i=startIndex; i<=endIndex; i++) {\n result += s[i];\n }\n\n return result;\n }\n\nprivate:\n std::unordered_set<uint64_t> knownPalindromes;\n\n bool isPalindrome(const string& str, uint32_t startIndex, uint32_t endIndex) {\n const auto key = cacheKey(startIndex, endIndex);\n if (startIndex == endIndex) {\n return true;\n } else if (knownPalindromes.contains(key)) {\n return true;\n } else if (str[startIndex] == str[endIndex]) {\n const auto result = (endIndex > startIndex + 1)\n ? isPalindrome(str, startIndex+1, endIndex-1)\n : true;\n if (result) {\n knownPalindromes.insert(key);\n }\n\n return result;\n } else {\n return false;\n }\n }\n\n uint64_t cacheKey(uint32_t startIndex, uint32_t endIndex) {\n return (static_cast<uint64_t>(startIndex) << 32) | endIndex;\n }\n};", "memory": "222641" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string longestPalindrome(string s) {\n uint32_t startIndex = 0;\n uint32_t endIndex = 0;\n for (uint32_t i=0; i<s.size(); i++) {\n for (uint32_t j=i; j<s.size(); j++) {\n if (isPalindrome(s, i, j)) {\n const auto candidateLength = j - i;\n const auto longestLength = endIndex - startIndex;\n if (candidateLength > longestLength) {\n startIndex = i;\n endIndex = j;\n }\n }\n }\n }\n\n std::string result;\n for (uint32_t i=startIndex; i<=endIndex; i++) {\n result += s[i];\n }\n\n return result;\n }\n\nprivate:\n std::unordered_set<uint64_t> knownPalindromes;\n\n bool isPalindrome(const string& str, uint32_t startIndex, uint32_t endIndex) {\n const auto key = cacheKey(startIndex, endIndex);\n if (startIndex == endIndex) {\n return true;\n } else if (knownPalindromes.contains(key)) {\n return true;\n } else if (str[startIndex] == str[endIndex]) {\n const auto result = (endIndex > startIndex + 1)\n ? isPalindrome(str, startIndex+1, endIndex-1)\n : true;\n if (result) {\n knownPalindromes.insert(key);\n }\n\n return result;\n } else {\n return false;\n }\n }\n\n uint64_t cacheKey(uint32_t startIndex, uint32_t endIndex) {\n return (static_cast<uint64_t>(startIndex) << 32) | endIndex;\n }\n};", "memory": "222641" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n\n string math(int l, int r, string maxstr, string s){\n while(l >= 0 && r < s.size() && s[l] == s[r]){\n if((r - l + 1) > maxstr.size()){\n maxstr = s.substr(l, (r - l + 1));\n }\n l--;\n r++;\n }\n return maxstr;\n }\n\n string longestPalindrome(string s) {\n string maxstr = \"\";\n\n for(int i = 0;i < s.size();i++){\n //odd\n maxstr = math(i, i, maxstr, s);\n //even\n maxstr = math(i, i+1, maxstr, s);\n }\n return maxstr;\n }\n};", "memory": "228688" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
3
{ "code": "class Solution {\nprivate:\n string expandUntilPalindrome(string s, string max_str, int l, int r) {\n while (l >= 0 && r < s.size() && s[l] == s[r]) {\n if((r - l + 1) > max_str.size()) {\n max_str = s.substr(l, r - l + 1);\n }\n l -= 1;\n r += 1;\n }\n return max_str;\n }\n\npublic:\n string longestPalindrome(string s) {\n string str = \"\";\n for (int i = 0; i < s.size(); i++) {\n str = expandUntilPalindrome(s, str, i, i);\n str = expandUntilPalindrome(s, str, i, i + 1);\n cout << i << \": \" << str << \"\\n\";\n }\n return str;\n }\n};", "memory": "228688" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string longestPalindrome(string s) {\n string s1=s;\n reverse(s.begin(),s.end()); \n string ans;\n int n=s.size();\n int res=0;\n int dp[n+1][n+1];\n for(int i=0;i<=n;i++){\n for(int j=0;j<=n;j++){\n if(i==0||j==0)\n dp[i][j]=0;\n else if(s[i-1]==s1[j-1]){\n dp[i][j]=dp[i-1][j-1]+1;\n \n \n }\n else\n dp[i][j]=0;\n \n if(dp[i][j]>res)\n {\n string temp=s.substr(i-dp[i][j],dp[i][j]);\n string rev=temp;\n reverse(rev.begin(),rev.end());\n if(temp==rev){ \n ans=temp;\n res=dp[i][j];\n }\n \n }\n }\n \n }\n return ans;\n }\n};", "memory": "234734" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool ispal(string a,int n){\n int s=0,e=n-1;\n while(e>s){\n if(a[s]!=a[e]){return false;}\n s++;e--;\n }\n return true;\n }\n string longestPalindrome(string s) {\n int n=s.length();\n string b=\"\";\n for(int i=n-1;i>-1;i--){\n b+=s[i];\n }\n int dp[1005][1005];\n for(int i=0;i<n+1;i++){\n dp[i][0]=0;\n }\n for(int i=0;i<n+1;i++){\n dp[0][i]=0;\n }\n int p=0,in=0;\n string ans=\"\";\n for(int i=1;i<n+1;i++){\n for(int j=1;j<n+1;j++){\n if(b[j-1]==s[i-1]){\n dp[i][j]= 1 + dp[i-1][j-1];\n }\n else{\n dp[i][j]=0;\n }\n if(dp[i][j]>p){\n int t=dp[i][j];\n string temp=s.substr(i-t,t);\n if(ispal(temp,t)){ans=temp;p=dp[i][j];}\n }\n }\n }\n \n // cout<<dp[n][n]<<endl;\n return ans;\n\n }\n};", "memory": "234734" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\nbool isPalin(string s)\n{\n bool palin=true;\n int left=0;\n int right = s.size()-1;\n while(left<right)\n {\n if(s[left]!=s[right]){palin=false; break;}\n left++;right--;\n }\n return palin;\n}\n string longestPalindrome(string s)\n{\n int left=0;\n int right=1;\n string res=\"\";\n string finalstr =\"\";\n string maxsizestr =s.substr(0,1) ;\n int size =0;\n int maxsize =0;\n bool threechar=false;\n if(s.size()<=1){return s;}\n // if(s.size()==2){\n // if(isPalin(s)){return s;}\n // else return s.substr(0,1); \n // }\n while(left>=0 && right<s.size())\n {\n //ba\n res=s.substr(left,(right-left+1));//e\n\n if(!isPalin(res))\n {\n if(!threechar)\n {\n right++;\n threechar=true;\n }\n else\n {\n left++;\n threechar=false;\n }\n }\n else\n { \n //palin\n cout<<\"YES\"<<endl;\n //give left right, find longest,,after that, left right as it is\n int start = left;\n int end=right;\n cout<<\"res = \"<<res<<\" left = \"<<left<<\", right = \"<<right<<\", maxszie = \"<<maxsize<<endl;\n while(start>=0 && end< s.size())\n { \n if(!isPalin(s.substr(start,(end-start+1))))\n { \n break;\n } \n\n size = end-start+1; \n finalstr = s.substr(start,size); \n start--;end++;\n }\n if(maxsize<=size)\n {\n cout<<\"maxsize = \"<<maxsize<<\" maxsizestr = \"<<maxsizestr<<endl;\n maxsize = size;\n maxsizestr=finalstr;\n }\n\n right++; \n \n }\n \n cout<<\"maxsize = \"<<maxsize<<\" left = \"<<left<<\", right = \"<<right<<\", maxszie = \"<<maxsize<<endl;\n }\n return maxsizestr;\n\n}\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n// bool ispalin(string s)\n// {\n// int start=0;\n// int end=s.size()-1;\n// //cout<<\"s = \"<<s<<endl;\n// while(start<end)\n// {\n// if(s[start]!=s[end]){return false;}\n// start++;end--;\n// }\n// return true;\n// }\n\n\n// string longestPalindrome(string s)\n// {\n// int size = s.size();\n// int index=0;\n// int max=0;\n// string res;\n// string maxString = s.substr(0,1);;\n// if(size<=1){return s;}\n// while(index<size)\n// {\n// //even cabbak\n// int left=index;\n// int right=index+1;\n// if(left>=0 && right<size && s[left]==s[right])\n// { \n// while(left>=0 && right<size && ispalin(s.substr(left,right-left+1)))\n// {\n// left--;right++;\n// }\n// left++;right--;\n// res = s.substr(left,right-left+1);\n// if(res.size()>max)\n// {\n// max = res.size();\n// maxString=res;\n// } \n// }\n\n// //odd cbdba\n// if(index>0)\n// {\n// left=index-1;\n// right=index+1;\n// if(left>=0 && right<size && s[left]==s[right])\n// { \n// while(left>=0 && right<size && ispalin(s.substr(left,right-left+1)))\n// {\n// left--;right++;\n// }\n// left++;right--;\n// res = s.substr(left,right-left+1);\n// if(res.size()>max)\n// {\n// max = res.size();\n// maxString=res;\n// } \n// }\n// }\n \n// index++;\n// }\n\n// return maxString;\n\n\n\n\n\n\n\n\n\n// // //go each and expand\n// // int mid=0;\n// // int left=0;\n// // int right=0;\n// // int max=0;\n// // char prev; \n// // string repeat; \n// // string res =s.substr(0,1);\n// // if(s.size()<=1){return s;}\n\n\n// // while(mid<s.size())\n// // {\n// // left=mid;\n// // right=mid;\n// // repeat.clear();\n// // while(left>=0 && right<s.size()&& s[left]==s[right])\n// // {\n// // left--;right++; \n// // }\n// // left++;right--;\n// // cout<<\", left = \"<<left<<\", right = \"<<right<<endl;\n// // cout<<\", right-left+1 = \"<<right-left+1<<endl;\n// // if(right-left+1>1)\n// // { \n// // string snew=s.substr(left,right-left+1);\n// // if( ispalin(snew))\n// // {\n// // if(max<snew.size())\n// // {\n// // max=snew.size(); \n// // res=snew;\n// // }\n// // }\n// // }\n// // prev=s[mid];\n// // repeat.push_back(prev);\n// // mid++;\n// // while(s[mid]==prev)\n// // {\n// // prev=s[mid];\n// // mid++; \n// // repeat.push_back(prev);\n// // if(max<repeat.size()){max=res.size(); res=repeat;} \n// // }\n// // }\n\n \n \n\n// // return res;\n// }\n\n\n\n\n\n\n\n\n\n\n\n\n// bool IsPalindrome(string s)\n// {\n// int left=0; int right=s.size()-1;\n// while(left<right)\n// {\n// if(s[left]!=s[right]){return false;}\n// left++; right--;\n// }\n// return true;\n// }\n\n\n// string longestPalindrome(string s)\n// {\n// // yuabcbamnabhsdffds\n// //abcdafgpgfadcba\n// //p start-1 & start+1\n \n// //abcdafggfadcba\n// //g start start+1 , start-1, start+2\n\n\n// int start=0;\n// int maxsize=0;\n// int left=0;\n// int right = 0;\n// string maxstring =\"\";\n// if(s.size()>1)\n// { \n// maxstring = s.substr(0,1); \n// while(start<=s.size()-1)\n// {\n// left=start-1;\n// right = start+1;\n// while(left>=0 && right<=s.size()-1)\n// {\n// string test=s.substr(left,right-left+1);\n// if(IsPalindrome(test))\n// {\n// if(maxsize<=test.size()){maxsize=test.size();maxstring=test;}\n// //cout<<\"left = \"<<left<<\" , test = \"<<test<<endl;\n// }\n// left--, right++;\n// //cout<<\"1condition = \"<<endl;\n// }\n\n// left=start;\n// right = start+1;\n// while(left>=0 && right<=s.size()-1)\n// {\n// string test=s.substr(left,right-left+1);\n// if(IsPalindrome(test))\n// {\n// if(maxsize<=test.size()){maxsize=test.size();maxstring=test;}\n// //cout<<\"2left = \"<<left<<\" , test = \"<<test<<\"maxstring = \"<<maxstring<<endl;\n// }\n// left--, right++;\n// //cout<<\"2condition = \"<<endl;\n \n// }\n// //cout<<\"start = \"<<start<<\", maxstring = \"<<maxstring<<endl;\n// start++;\n \n\n// }\n// return maxstring;\n// }\n// else if(s.size()>=1 )\n// {\n// maxstring =s;\n// } \n// return maxstring;\n \n\n\n\n\n\n\n\n// // // find next letter same\n// // // find next of that until end\n// // // check start end is palin\n// // vector<string> vecstr;\n// // int start=0;\n// // int nextLetterPos=0;\n// // int maxsize=0;\n// // string maxstring;\n// // string out;\n// // if(s.size()>1)\n// // {\n// // while(start<=s.size()-1)\n// // {\n// // while(nextLetterPos!=string::npos)\n// // {\n// // int pos = s.find(s[start], nextLetterPos+1);\n// // //cout<<\"pos = \"<<pos<<\", start = \"<<start<<endl;\n// // nextLetterPos = pos;\n// // if(pos!=string::npos)\n// // { \n// // out = s.substr(start,pos-start+1);\n// // if(IsPalindrome(out)) \n// // {\n// // //cout<<\" str = \"<<out<<endl; \n// // if(maxsize<out.size()){ maxsize = out.size(); maxstring = out;}\n\n// // }\n\n// // }\n// // }\n// // start++;\n// // nextLetterPos=start; \n// // }\n// // if(maxsize==0){maxstring = s[0];}\n// // }\n// // else { return s;}\n// // string res=\"\";\n\n// // return maxstring;\n\n\n\n\n// // //substr\n// // // outside\n// // // right in, \n// // // not same\n// // // left in, \n// // // not same\n// // // when same, both left right move and check, if not same, update indexlongest\n// // // right in\n// // int indexstart=0;int maxindexstart=0;\n// // int indexend=0;int maxindexend=0;\n// // int left=0;\n// // bool switchflag=true;\n// // int right=s.size()-1;\n// // int maxs=0;\n// // while(left<right)\n// // {\n// // if(s[left]==s[right])\n// // {\n// // indexstart=left; indexend=right;\n// // left++; right--;\n// // }\n// // else\n// // {\n// // int nextright=right-1;\n// // int nextleft=left+1;\n// // if(s[left]==s[nextright]){right--;}\n// // else if(s[nextleft]==s[right]){left++;}\n// // else if(switchflag){right--;switchflag=false;}\n// // else {left++; switchflag=true;}\n// // }\n// // cout<<\"left = \"<<left<<\", s[left]= \"<<s[left]<<\", right =\"<<right<<\"s[right]=\"<<s[right]<<endl;\n// // if(maxs<(indexend-indexstart)){maxs = indexend-indexstart;maxindexstart =indexstart;maxindexend= indexend;}\n// // cout<<\"indexstart = \"<<indexstart<<\",indexend= \"<<indexend<<\", maxindexstart =\"<<maxindexstart<<\"maxindexend=\"<<maxindexend<<endl;\n// // }\n// // string res = s.substr(maxindexstart, maxindexend-maxindexstart+1);\n// // return res;\n \n// }\n\n\n// bool IsPalindrome(string s)\n// {\n// int left=0; int right=s.size()-1;\n// while(left<right)\n// {\n// if(s[left]!=s[right]){return false;}\n// left++; right--;\n// }\n// return true;\n// }\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n // string longestPalindrome(string s) {\n // //find if string palindromic\n // //abcdefedchybjar\n // // i i+1 are same --check\n // // i-1 i+1 same--check\n // //size- map- size string\n // int left=0;\n // int temp=0;\n // int right=0;\n // int palindsize=0;\n // int maxpalindrome=0;\n // map<int, string> palinMap;\n // int size = s.size();\n // for (int i=0; i<size-1;i++) \n // {\n // if(s[i]==s[i+1])\n // {\n // left=i;\n // right =i+1;\n // }\n // else if(i>0 && s[i-1]==s[i+1])\n // {\n // //go left and right and check if they equal\n // left=i-1;\n // right =i+1;\n // }\n \n // while(s[left]==s[right])\n // {\n // palindsize = right-left+1;\n // // if(left>0) {palindsize = right-left+1;}\n // // else {palindsize = right-left+2;}\n // temp=left;\n\n // if(left==0 || right == size-1){ break;} \n // left--; right++; \n // }\n // palinMap[palindsize] = s.substr(temp, palindsize);\n\n // cout<<\"i =\"<<i<<\", left, right, = \"<<left<<\", \"<<right<<\", palindsize = \"<<palindsize\n // <<\", s.substr(left, palindsize) = \"<< s.substr(left+1, palindsize)<<endl;\n // left=0;\n // right=0;\n // palindsize=0;\n // }\n // if(!palinMap.empty())\n // return (palinMap.rbegin())->second;\n // else\n // return s;\n \n // // for (int i=1; i<s.size()-2;i++) \n // // {\n // // if(s[i]==s[i+1])\n // // {\n // // //go left and right and check if they equal\n // // left=i;\n // // right =i+1;\n // // }\n // // else if(s[i-1]==s[i+1])\n // // {\n // // //go left and right and check if they equal\n // // left=i-1;\n // // right =i+1;\n // // }\n // // else\n // // {\n // // left=0;\n // // right=0;\n // // palindsize=0;\n // // continue;\n // // }\n // // while(left>=0 && right<=s.size()-1 && s[left]==s[right])\n // // {\n // // palindsize = right-left;\n // // if(left!=0 && right!=s.size()-1)\n // // {left--;\n // // right++;}\n // // }\n // // palinMap[palindsize] = s.substr(left, palindsize);\n // // cout<<\"i =\"<<i<<\", left, right, = \"<<left<<\", \"<<right<<\", palindsize = \"<<palindsize<<endl;//\", s[i]==s[i+1] = \"<<s.substr(left, palindsize)<<endl; \n // // left=0;\n // // right=0;\n // // palindsize=0;\n // // }\n \n // //int maxsize = (palinMap.rbegin())->second;\n // //return (palinMap.rbegin())->second;\n // return s;\n // }\n};", "memory": "240780" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string addPountSign(string s)\n {\n if (s.empty()) return \"#\";\n std::string res = \"#\";\n for (char c : s) {\n res += c;\n res += \"#\";\n }\n return res;\n }\n\n int calSizePalindrome(string s, int center)\n {\n int size = 0;\n int n = s.size();\n while (center - size >= 0 && center + size < n && s[center - size] == s[center + size]) {\n size++;\n }\n return size - 1;\n\n }\n\n string longestPalindrome(string s) {\n string t = addPountSign(s);\n vector<int> P = { 0 };\n int right = 0;\n int center = 0;\n int max_len = 0;\n int pos_max_len = 0;\n for (int i = 1; i < t.size(); i++)\n {\n if (i > right || (i == right && i < t.size() - 1) || (i < right && P[2 * center - i] * 2 + i >= right ))\n {\n int x = calSizePalindrome(t, i);\n if (x > max_len)\n {\n max_len = x;\n pos_max_len = i;\n }\n P.push_back(x);\n center = i;\n right = center + x;\n }\n else\n {\n P.push_back(P[2 * center - i]);\n }\n }\n\n string res;\n int start = (pos_max_len - max_len) / 2;\n int end = start + max_len;\n\n for (int i = start; i < end; i++) {\n res += s[i];\n }\n\n return res;\n }\n};", "memory": "240780" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string longestPalindrome(string s) {\n pair<int,int> p={0,0};\n for(int i=0;i<s.size();i++){\n string a,b;\n for(int j=i;j<s.size();j++){\n a+=s[j];\n b.insert(b.begin(),s[j]);\n if(a==b && (j-i)>=(p.second-p.first)){\n p.first=i;\n p.second=j;\n }\n }\n }\n return s.substr(p.first,p.second-p.first+1);\n }\n};", "memory": "246826" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string longestPalindrome(string s) {\n int maxLength = 0;\n string ans = \"\";\n for (int ptr = 0; ptr<s.size(); ptr++) {\n \n string reverse_str = \"\"; \n string check = \"\";\n\n for (int i = ptr; i < s.size(); i++) {\n char ch = s[i];\n reverse_str.insert(reverse_str.begin(), ch);\n check.push_back(ch);\n if (maxLength < check.size() && check == reverse_str) {\n maxLength = check.size();\n ans = check;\n }\n \n }\n }\n return ans ;\n }\n};", "memory": "246826" }
5
<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;babad&quot; <strong>Output:</strong> &quot;bab&quot; <strong>Explanation:</strong> &quot;aba&quot; is also a valid answer. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cbbd&quot; <strong>Output:</strong> &quot;bb&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consist of only digits and English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string longestPalindrome(string s) {\n int length = s.length();\n\n if (length == 0)\n return \"\";\n\n int maxl = 1; // Length of the longest palindrome\n int start = 0; // Starting index of the longest palindrome\n\n // Allocate memory for the DP matrix and initialize it to 0\n int *matrix = new int[length * length]{0};\n\n // Single character palindromes\n for (int i = 0; i < length; i++) {\n matrix[i * length + i] = 1;\n }\n\n for (int i = length - 1; i >= 0; i--) {\n for (int j = i + 1; j < length; j++) {\n if (s[i] == s[j]) {\n if (j - i == 1 || matrix[(i + 1) * length + (j - 1)] == 1) {\n matrix[i * length + j] = 1;\n if (j - i + 1 > maxl) {\n maxl = j - i + 1;\n start = i;\n }\n }\n }\n }\n }\n\n delete[] matrix; // Don't forget to free the allocated memory\n return s.substr(start, maxl);\n }\n\n};", "memory": "252873" }