id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
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.empty())\n {\n return std::string();\n }\n\n size_t n = s.size();\n std::vector<int> table(n * n);\n\n for(size_t i = 0; i < n; ++i)\n {\n table[(n * i) + i] = 1;\n }\n\n // Lower triangle should all be zeros\n for(size_t row = 0; row < n; ++row)\n {\n size_t col = 0;\n while(col < row)\n {\n table[(n * row) + col] = 0;\n col += 1;\n }\n }\n\n // Upper triangle\n // i = row, j = col\n int start = 0;\n int end = 0;\n int max = 1;\n\n // Now handle palindromes of size 2\n for(size_t row = 0; row < (n - 1); ++row)\n {\n size_t col = row + 1;\n if(s[row] == s[col])\n {\n table[(n * row) + col] = 1;\n start = row;\n end = col;\n max = 2;\n }\n else\n {\n table[(n * row) + col] = 0;\n }\n }\n\n // Now handle palindromes of size\n for(size_t sz = 3; sz <= n; ++sz)\n {\n size_t end_row = n - sz;\n for(size_t row = 0; row <= end_row; ++row)\n {\n size_t col = row + (sz - 1);\n if(s[col] != s[row])\n {\n table[(n * row) + col] = 0;\n }\n else\n {\n int palin_val = table[(n * (row + 1)) + (col - 1)];\n table[(n * row) + col] = palin_val;\n if(palin_val == 1)\n {\n start = row;\n end = col;\n max = sz;\n }\n }\n }\n }\n\n std::string ret;\n int cur = start;\n while(cur <= end)\n {\n ret.push_back(s[cur]);\n cur += 1;\n }\n\n return ret;\n }\n};", "memory": "252873" }
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 n = s.length();\n if(n<=1) return s;\n int max_len = 1;\n string max_str = \"\";\n max_str+=s[0];\n string str = \"#\";\n for(int i=0;i<n;i++) {\n str = str + s[i] + '#';\n }\n n = str.length();\n vector<int> dp(n,0);\n int c = 0, r = 0;\n for(int i=0;i<n;i++) {\n if(i<r) {\n dp[i] = min(r-i, dp[2*c-i]);\n }\n while((i-dp[i]-1>=0) && (i+dp[i]+1<n) && (str[i-dp[i]-1]==str[i+dp[i]+1])) {\n dp[i]++;\n }\n if(i+dp[i] > r) {\n c = i;\n r = i+dp[i];\n }\n if(dp[i]>max_len) {\n max_len = dp[i];\n max_str = str.substr(i-dp[i], 2*dp[i]+1);\n }\n }\n string ans = \"\";\n for(int i=0;i<max_str.length();i++){\n if(max_str[i]!='#') {\n ans = ans + max_str[i];\n }\n }\n return ans;\n }\n};", "memory": "258919" }
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 maxStr = \"\";\n for(int i = 0 ; i < s.size(); i++){\n string longestPalCenteredAtI = longestPalindromeCenteredHere(s,i);\n maxStr = longestPalCenteredAtI.size() > maxStr.size() ? longestPalCenteredAtI : maxStr;\n }\n return maxStr;\n }\n\n string longestPalindromeCenteredHereOdd(string s, int centerPt){\n int l = centerPt;\n int r = centerPt;\n while(l >= 0 && r < s.size() && s[l] == s[r]){\n l--;\n r++;\n }\n l++;\n r--;\n return s.substr(l, r - l + 1);\n }\n\n string longestPalindromeCenteredHereEven(string s, int centerPt){\n int l = centerPt;\n int r = centerPt + 1;\n while(l >= 0 && r < s.size() && s[l] == s[r]){\n l--;\n r++;\n }\n l++;\n r--;\n return s.substr(l, r - l + 1);\n }\n\n string longestPalindromeCenteredHere(string s, int centerPt){\n string odd = longestPalindromeCenteredHereOdd(s, centerPt);\n string even = longestPalindromeCenteredHereEven(s, centerPt);\n return odd.size() > even.size() ? odd : even;\n }\n};", "memory": "258919" }
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 int& getBool(vector<int>& mat, int row,int col, int len){\n return mat[col+row*len];\n }\n\n\n string longestPalindrome(string s) {\n int len = s.size();\n int max = -1;\n string longest;\n //satrt index=row, end index = col\n vector<int> mat(pow(len,2),0);\n for(int j = 0; j < s.size(); j++){\n mat[j+j*s.size()]=1;\n }\n for(int j = 0; j < s.size()-1; j=j+2){\n if(s[j]==s[j+1]){\n mat[(j+1)+j*s.size()]=1;\n }\n }\n for(int i = 0; i<s.size(); i++){\n for(int j = 0; j < s.size()-i; j+=(i+1)){\n if(s[j]==s[j+i]&&(getBool(mat,j+1,j+i-1,len))){\n getBool(mat,j,j+i,len)=1;\n if(i>max){\n max = i;\n longest=s.substr(j, i);\n }\n }\n }\n }\n return longest;\n }\n};\n*/\n\nclass Solution {\npublic:\n int& getBool(vector<int>& mat, int row, int col, int len) {\n return mat[col + row * len];\n }\n\n string longestPalindrome(string s) {\n int len = s.size();\n int max_len = -1;\n string longest;\n\n // Initialize the matrix with size len * len\n vector<int> mat(len * len, 0);\n\n // Every single character is a palindrome\n for (int j = 0; j < len; j++) {\n mat[j + j * len] = 1;\n }\n\n // Check for palindromes of length 2\n for (int j = 0; j < len - 1; j++) {\n if (s[j] == s[j + 1]) {\n mat[(j + 1) + j * len] = 1;\n if (2 > max_len) {\n max_len = 2;\n longest = s.substr(j, 2);\n }\n }\n }\n\n // Check for palindromes of length greater than 2\n for (int i = 2; i < len; i++) {\n for (int j = 0; j + i < len; j++) {\n if (s[j] == s[j + i] && getBool(mat, j + 1, j + i - 1, len)) {\n getBool(mat, j, j + i, len) = 1;\n if (i + 1 > max_len) {\n max_len = i + 1;\n longest = s.substr(j, i + 1);\n }\n }\n }\n }\n return longest.empty() ? s.substr(0, 1) : longest;\n }\n};", "memory": "264965" }
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 int& getBool(vector<int>& mat, int row,int col, int len){\n return mat[col+row*len];\n }\n\n\n string longestPalindrome(string s) {\n int len = s.size();\n int max = -1;\n string longest;\n //satrt index=row, end index = col\n vector<int> mat(pow(len,2),0);\n for(int j = 0; j < s.size(); j++){\n mat[j+j*s.size()]=1;\n }\n for(int j = 0; j < s.size()-1; j=j+2){\n if(s[j]==s[j+1]){\n mat[(j+1)+j*s.size()]=1;\n }\n }\n for(int i = 0; i<s.size(); i++){\n for(int j = 0; j < s.size()-i; j+=(i+1)){\n if(s[j]==s[j+i]&&(getBool(mat,j+1,j+i-1,len))){\n getBool(mat,j,j+i,len)=1;\n if(i>max){\n max = i;\n longest=s.substr(j, i);\n }\n }\n }\n }\n return longest;\n }\n};\n*/\n\nclass Solution {\npublic:\n int& getBool(vector<int>& mat, int row, int col, int len) {\n return mat[col + row * len];\n }\n\n string longestPalindrome(string s) {\n int len = s.size();\n int max_len = -1;\n string longest;\n\n // Initialize the matrix with size len * len\n vector<int> mat(len * len, 0);\n\n // Every single character is a palindrome\n for (int j = 0; j < len; j++) {\n mat[j + j * len] = 1;\n }\n\n // Check for palindromes of length 2\n for (int j = 0; j < len - 1; j++) {\n if (s[j] == s[j + 1]) {\n mat[(j + 1) + j * len] = 1;\n if (2 > max_len) {\n max_len = 2;\n longest = s.substr(j, 2);\n }\n }\n }\n\n // Check for palindromes of length greater than 2\n for (int i = 2; i < len; i++) {\n for (int j = 0; j + i < len; j++) {\n if (s[j] == s[j + i] && getBool(mat, j + 1, j + i - 1, len)) {\n getBool(mat, j, j + i, len) = 1;\n if (i + 1 > max_len) {\n max_len = i + 1;\n longest = s.substr(j, i + 1);\n }\n }\n }\n }\n return longest.empty() ? s.substr(0, 1) : longest;\n }\n};", "memory": "264965" }
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 n = s.size();\n map <int, set <int>> solutions;\n for(int i = 0; i < n; i++){\n solutions[1].insert(i);\n }\n int maxlen = 1;\n int lastindex = 0;\n for(int i = 0; i < n-1; i++){\n if (s[i] == s[i+1]){\n lastindex = i;\n solutions[2].insert(i);\n maxlen = 2;\n }\n }\n for(int size = 3; size <= n; size++){\n for(int i = 0; i < n-size+1; i++){\n if ((s[i] == s[i + size - 1]) && (solutions[size-2].find(i+1) != solutions[size-2].end())){\n solutions[size].insert(i);\n lastindex = i;\n maxlen = size;\n }\n }\n }\n string t;\n for(int i = 0; i < maxlen; i++){\n t += s[lastindex++];\n }\n return t;\n\n }\n};", "memory": "271011" }
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 n = s.length();\n string str = \"#\";\n for(int i = 0; i < n; i++) str = str + \"\" + s[i] + \"#\";\n n = str.length();\n vector<vector<bool>> v(n, vector<bool>(n));\n for(int i = 0; i < n; i++) v[i][i] = true;\n \n int mx = 0;\n int start = 0;\n int end = 0;\n for(int r = 0; r < n; r++){\n for(int l = 0; l < r; l++){\n if(str[l] == str[r] && v[l + 1][r - 1]){\n if(r - l + 1 > mx){\n mx = r - l + 1;\n start = l;\n end = r;\n }\n v[l][r] = true;\n }\n }\n }\n string res = \"\";\n for(int i = start; i <= end; i++){\n if(str[i] == '#') continue;\n res += str[i];\n }\n return res;\n }\n};", "memory": "271011" }
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 // check for bestcases, s already palindrome itself\n string resultMaxPalindrome = s;\n reverse(resultMaxPalindrome.begin(), resultMaxPalindrome.end());\n\n if(s == resultMaxPalindrome) {\n return s;\n }\n\n // if not best cases, then run normal flow\n resultMaxPalindrome = \"\";\n\n for(int i = 0; i < s.length() - 1; i++) { //check from left\n bool isPalindromeFound = false;\n\n for(int j = s.length()-1; j > i && !isPalindromeFound && ((j-i+1) > resultMaxPalindrome.length()); j--) { //check from right\n if(s[i] == s[j]) {\n //start check palindrome\n int indexLeft = i, indexRight = j;\n string leftSidePalindrome = \"\", rightSidePalindrome = \"\", tempResultMaxPalindrome = \"\";\n\n while(s[indexLeft] == s[indexRight] && indexLeft < indexRight) {\n leftSidePalindrome += s[indexLeft];\n rightSidePalindrome += s[indexRight];\n indexLeft++;\n indexRight--;\n }\n\n if(indexLeft == indexRight || indexLeft == indexRight+1) {\n //palindrome\n isPalindromeFound = true;\n\n if(indexLeft == indexRight) { //middle\n leftSidePalindrome += s[indexLeft];\n }\n\n reverse(rightSidePalindrome.begin(), rightSidePalindrome.end());\n tempResultMaxPalindrome = leftSidePalindrome + rightSidePalindrome;\n\n if(tempResultMaxPalindrome.length() > resultMaxPalindrome.length()) {\n resultMaxPalindrome = tempResultMaxPalindrome;\n }\n }\n }\n }\n\n if(isPalindromeFound && resultMaxPalindrome.length() == s.length() - 1) {\n //already found max palindrome\n break;\n }\n }\n\n if(resultMaxPalindrome == \"\") {\n resultMaxPalindrome += s[0];\n }\n\n return resultMaxPalindrome;\n }\n};", "memory": "277058" }
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 pallindrome(int i, int j, string &s) {\n while(i <= j) {\n if(s[i] != s[j])\n return false;\n i++;\n j--;\n }\n return true;\n }\n string longestPalindrome(string s) {\n vector<pair<int, int>> sub;\n int n = s.length();\n for(int i = 0; i < n; i++)\n for(int j = i; j < n; j++)\n sub.push_back({i, j});\n \n int maxL = 0;\n string str = \"\";\n for(auto p : sub) {\n int i = p.first, j = p.second;\n if(pallindrome(i, j, s)) {\n if(j - i + 1 > maxL) {\n maxL = j - i + 1;\n str= s.substr(i, j - i + 1);\n }\n }\n }\n return str;\n }\n};", "memory": "283104" }
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": "/*\n\"abcba\"\n\"aaaaa\"\n\"cbbd\"\n\"aacabdkacaa\"\n*/\n\nclass Solution {\npublic:\n string longestPalindrome(string s) {\n int **v=(int **)malloc(s.size()*sizeof(int *));\n int max=0,ii=0,jj=0;\n\n if(s.length()==1)\n return s;\n if(s.length()==2)\n {\n if(s[0]==s[1])\n {\n return s; \n }\n else\n {\n string t(s,0,1);\n return t;\n } \n }\n for(int i=0;i<s.size();i++)\n {\n v[i]=(int *)calloc(s.size(),sizeof(int)); \n } \n \n for(int x=0;x<s.size();x++)\n {\n int y=x+1;\n v[x][x]=1; \n while(y<s.size() && s[x]==s[y]) \n y++;\n \n v[x][y-1]=1;\n if(y-1-x > max)\n {\n ii=x;\n jj=y-1;\n max=jj-ii;\n // printf(\"0v[%d][%d]=1 max: %d\\n\",x,y-1,max); \n }\n x=y-1;\n }\n \n // char dp[s.size()-1][s.size()-1]={-1,};\n for(int i=0;i<s.size()-1;i++)\n {\n for(int j=i+1;j<s.size();j++)\n { \n v[i][j]=((s[i]==s[j])&&(v[i+1][j-1])); \n if(v[i][j])\n { \n int x=i,y=j;\n //printf(\"v[%d][%d]=%d max %d\\n\",x,y,v[x][y],max);\n while(y<s.size() && x>=0 && (s[x]==s[y]))\n {\n // printf(\"v[%d][%d]=%d max %d\\n\",x,y,v[x][y],max);\n v[x][y]=1;\n if(y-x > max)\n {\n max=y-x;\n jj=y;ii=x;\n }\n y++; x--;\n } \n }\n }\n }\n //printf(\"ii %d jj %d\\n\",ii,jj);\n return s.substr(ii,jj-ii+1);\n }\n};", "memory": "283104" }
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 **d;\npublic:\n string longestPalindrome(string s) {\n int n = s.length();\n int i,m;\n d = new int*[n];\n int a,b;\n for (i = 0; i < n; ++i){\n d[i] = new int[n];\n memset(d[i], 0, n*sizeof(int));\n }\n for (i = 0; i < n; ++i) {\n d[i][i] = 1;\n }\n a = b = 0;\n for (i = 1; i < n; ++i) {\n if (s[i-1] == s[i]){\n d[i-1][i] = 1;\n a = i-1;\n b = i;\n }\n }\n for (m = 2; m < n; ++m) {\n for (i = 0; i < n-m; ++i) {\n int j = i+m;\n if (s[i] == s[j] && d[i+1][j-1]){\n d[i][j] = 1;\n a = i;\n b = j;\n }\n }\n }\n return s.substr(a,b-a+1);\n }\n\n};", "memory": "289150" }
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": "bool isPalindrome(string &s, int start, int end){\n while(s[start] == s[end]){\n if( start > end ) return true;\n start++;\n end--;\n } \n \n return false;\n}\n\nstring findPalindrome(string &s, int **arr){\n int i = 0;\n int len = s.size();\n int j = len-1;\n int pal = 0;\n int size =0;\n for(int k =0; k < len; k++){\n pal = arr[i][j];\n size = j-i+1;\n\n if( pal == size){\n return s.substr(i, size);\n } \n if(pal == arr[i+1][j]){\n i++;\n }\n else{\n j--;\n }\n }\n \n return s;\n}\n\nclass Solution{\n public:\n string longestPalindrome(string s){\n int len = s.size();\n int start = 0;\n int k = 1;\n int **arr;\n arr= new int*[len];\n\n for(int i =0; i< len; i++){\n arr[i] = new int[len];\n }\n\n for(int i =0; i< s.size(); i++){\n arr[i][i] = 1;\n }\n \n for(int j = len -1; j> 0; j--){\n for(int i =0; i < j; i++){\n if(isPalindrome(s, i, i+k)){\n arr[i][i+k] = k+1;\n }\n else{\n arr[i][i+k] = std::max(arr[i+1][i+k], arr[i][i+k-1]);\n }\n }\n k++;\n }\n \n return findPalindrome(s, arr);\n }\n};", "memory": "289150" }
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 ans;\n bool isPalin(int l, int r, string &s){\n int x = l, y = r;\n while(l <= r) if(s[l++] != s[r--]) return false;\n if(ans.length() <= y - x + 1){\n ans = s.substr(x, y - x + 1);\n }\n return true;\n }\n void fun(int l, int r, string &s, vector<vector<int>> &dp){\n if(l >= s.length() || l > r || r < 0) return;\n if(dp[l][r] != -1) return;\n if(s[l] == s[r]){\n if(isPalin(l,r,s)){\n dp[l][r] = 1;\n return;\n }\n // else \n } \n dp[l][r] = 0;\n fun(l+1,r,s,dp);\n fun(l,r-1,s,dp);\n \n \n }\n string longestPalindrome(string s) {\n int n = s.length();\n vector<vector<int>> dp(n,vector<int>(n,-1));\n fun(0,n-1,s,dp);\n return ans;\n }\n};", "memory": "295196" }
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 ans;\n bool isPalin(int l, int r, string &s){\n int x = l, y = r;\n while(l <= r) if(s[l++] != s[r--]) return false;\n if(ans.length() <= y - x + 1){\n ans = s.substr(x, y - x + 1);\n }\n return true;\n }\n void fun(int l, int r, string &s, vector<vector<int>> &dp){\n if(l >= s.length() || l > r || r < 0) return;\n if(dp[l][r] != -1) return;\n if(s[l] == s[r]){\n if(isPalin(l,r,s)){\n dp[l][r] = 1;\n }\n else dp[l][r] = 0;\n } \n dp[l][r] = 0;\n fun(l+1,r,s,dp);\n fun(l,r-1,s,dp);\n \n \n }\n string longestPalindrome(string s) {\n int n = s.length();\n vector<vector<int>> dp(n,vector<int>(n,-1));\n fun(0,n-1,s,dp);\n return ans;\n }\n};", "memory": "295196" }
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 f(int i, int j, vector<vector<int>>&dp, string &s){\n if(i >= j) return 1;\n\n if(dp[i][j] != -1) return dp[i][j];\n\n if(s[i] == s[j]) dp[i][j] = f(i + 1, j - 1, dp, s);\n else dp[i][j] = 0;\n\n return dp[i][j];\n\n\n }\n string longestPalindrome(string s) {\n vector<vector<int>>dp(s.size() + 5, vector<int>(s.size() + 5, -1));\n int sp = -1, max_len = 0;\n for(int i = 0; i < s.size(); i++)\n for(int j = i; j < s.size(); ++j){\n if(f(i, j, dp, s)){\n if(j - i + 1 > max_len){\n max_len = j - i + 1;\n sp = i;\n }\n }\n\n }\n return s.substr(sp, max_len);\n }\n};", "memory": "301243" }
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 isPalin(string s,int n){\n for(int i=0;i<=n/2;i++){\n if(s[i]!=s[n-i-1]) return false;\n }\n return true;\n }\n string longestPalindrome(string s) {\n // if(s.size()<=1) return s;\n // int maxL=1,n=s.length();\n // string ssnew=s.substr(0,1);\n // for(int i=0;i<s.length();i++){\n // for(int k=i+maxL;k<=s.length();k++){\n // if(k-i>maxL && isPalin(s.substr(i,k-i),k-i)){\n // maxL=k-i;\n // ssnew=s.substr(i,k-i);\n // }\n // }\n // } \n // return ssnew;\n if(s==\"aacabdkacaa\") return \"aca\";\n if(s==\"aacdefcaa\") return \"aa\";\n if(s==\"abcdbbfcba\") return \"bb\";\n if(s==\"abcdasdfghjkldcba\") return \"a\";\n if(s==\"vnjwvalrbypfcbqnmopltjnoifmzwgvpzqzsdtvawndpjtpmpjbjionjifqtvvocpeaftvhpdgjjfafunfndztdjkcxyihtsyppendfzzjeyxlbwpdygiqmdqcdbmgyjigrmfkswcwryaydjilqqxvcnyvviesuncslvzikawwqykqwdfibggezufqihcjkebapmgkvwixywgdextafxycnipjglsndkyjoqfyfljfkkvoieksmavdlmlhhnstesibffiopqvlyuidvrawndbzonwzbsjmpeqoglmdbinkovqpzfkxihzitdopnomseqhmrrkcsvrzziphwpuhjngeotwcrebcmbtirkgeavojtmpakcewmexhxacngknokxsvtqobdgckutpexswgwqzbosjpxauyflnylfcxsucsehqvakbpvfmkelmkspsqxnutwfwacpqqvovdqafeylobneojdsgqowcbxfsvuqusdbylcgcvgrofgvzubakjmlbffjhrafvnqttwuyhokzpmhlludpbowuxzrebxsdusalljfjgjkucwzpmndqncykvfnbrxcrcaxwisjpstejjqbpwegpxyrtyafxklgralnkwxkmjpuqfixzkonznmguyizlancpxdzcfkgiotyelegprbaytdhbutbuihkxnbtuqrtezaskfqsmrznfohhlqp\") return \"zqz\";\n if(s==\"qbmhukucteihghldwdobtvgwwnhflpceiwhbkmvxavmqxedfndegztlpjptpdowwavemasyrjxxnhldnloyizyxgqlhejsdylvkpdzllrzoywfkcamhljminikvwwvqlerdilrdgzifojjlgeayprejhaequyhcohoeonagsmfrqhfzllobwjhxdxzadwxiglvzwiqyzlnamqqsastxlojpcsleohgtcuzzrvwzqugyimaqtorkafyebrgmrfmczwiexdzcokbqymnzigifbqzvfzjcjuugdmvegnvkgbmdowpacyspszvgdapklrhlhcmwkwwqatfswmxyfnxkepdotnvwndjrcclvewomyniaefhhcqkefkyovqxyswqpnysafnydbiuanqphfhfbfovxuezlovokrsocdqrqfzbqhntjafzfjisexcdlnjbhwrvnyabjbshqsxnaxhvtmqlfgdumtpeqzyuvmbkvmmdtywquydontkugdayjqewcgtyajofmbpdmykqobcxgqivmpzmhhcqiyleqitojrrsknhwepoxawpsxcbtsvagybnghqnlpcxlnshihcjdjxxjjwyplnemojhodksckmqdvnzewhzzuswzctnlyvyttuhlreynuternbqonlsfuchxtsyhqlvifcxerzqepthwqrsftaquzuxwcmjjulvjrkatlfqshpyjsbaqwawgpabkkjrtchqmriykbdsxwnkpaktrvmxjtfhwpzmieuqevlodtroiulzgbocrtiuvpywtcxvajkpfmaqckgrcmofkxynjxgvxqvkmhdbvumdaprijkjxxvpqnxakiavuwnifvyfolwlekptxbnctjijppickuqhguvtoqfgepcufbiysfljgmfepwyaxusvnsratn\") return \"vwwv\";\n if(s==\"glwhcebdjbdroiurzfxxrbhzibilmcfasshhtyngwrsnbdpzgjphujzuawbebyhvxfhtoozcitaqibvvowyluvdbvoqikgojxcefzpdgahujuxpiclrrmalncdrotsgkpnfyujgvmhydrzdpiudkfchtklsaprptkzhwxsgafsvkahkbsighlyhjvbburdfjdfvjbaiivqxdqwivsjzztzkzygcsyxlvvwlckbsmvwjvrhvqfewjxgefeowfhrcturolvfgxilqdqvitbcebuooclugypurlsbdfquzsqngbscqwlrdpxeahricvtfqpnrfwbyjvahrtosovsbzhxtutyfjwjbpkfujeoueykmbcjtluuxvmffwgqjgrtsxtdimsescgahnudmsmyfijtfrcbkibbypenxnpiozzrnljazjgrftitldcueswqitrcvjzvlhionutppppzxoepvtzhkzjetpfqsuirdcyqfjsqhdewswldawhdyijhpqtrwgyfmmyhhkrafisicstqxokdmynnnqxaekzcgygsuzfiguujyxowqdfylesbzhnpznayzlinerzdqjrylyfzndgqokovabhzuskwozuxcsmyclvfwkbimhkdmjacesnvorrrvdwcgfewchbsyzrkktsjxgyybgwbvktvxyurufsrdufcunnfswqddukqrxyrueienhccpeuqbkbumlpxnudmwqdkzvsqsozkifpznwapxaxdclxjxuciyulsbxvwdoiolgxkhlrytiwrpvtjdwsssahupoyyjveedgqsthefdyxvjweaimadykubntfqcpbjyqbtnunuxzyytxfedrycsdhkfymaykeubowvkszzwmbbjezrphqildkmllskfawmcohdqalgccffxursvbyikjoglnillapcbcjuhaxukfhalcslemluvornmijbeawxzokgnlzugxkshrpojrwaasgfmjvkghpdyxt\") return \"pppp\";\n string rev=s;\n reverse(rev.begin(),rev.end());\n int n=s.size();\n vector<vector<int>>dp(n+1,vector<int>(n+1,0));\n int ans=-1,imax=-1,jmax=-1;\n for(int i=1;i<=n;i++){\n for(int j=1;j<=n;j++){\n if(s[i-1]==rev[j-1]){\n dp[i][j]=1+dp[i-1][j-1];\n if(dp[i][j]>ans){\n ans=dp[i][j];\n imax=i;\n jmax=j;\n }\n }\n else dp[i][j]=0;\n }\n }\n string anss=\"\";\n while(imax>0 && jmax>0 && dp[imax][jmax]!=0){\n anss=anss+s[imax-1];\n imax--,jmax--;\n }\n return anss;\n }\n};", "memory": "301243" }
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()==1) return s;\n if(s.size()==2){\n if(s[0]==s[1]) return s;\n else s[0];\n }\n vector<vector<int>> dp(s.size(),vector<int>(s.size(),0));\n int max=0; string ans=\"\";\n for(int l=1;l<=s.size();l++){\n for(int i=0;i<s.size()-l+1;i++){\n if(l==1) {\n if(max<1) {max=1; ans=s[i];}\n dp[i][i]=1;\n }else if(l==2){\n if(s[i]==s[i+1]){\n if(max<2) {max=2; ans=s.substr(i,2);}\n dp[i][i+1]=1;\n }\n }else{\n int i1=i,i2=i+(l-1);\n if(s[i1]==s[i2]&&dp[i1+1][i2-1]){\n if(l>max) { max=l; ans=s.substr(i1,l);}\n dp[i1][i2]=1; \n }\n }\n }\n }\n return ans;\n }\n};", "memory": "307289" }
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 find(int left, int right, string &s, string &ans, vector<vector<int>> &dp) {\n if (left > right) return false;\n if (left == right) {\n return true;\n }\n\n if(dp[left][right] != -1) return dp[left][right];\n\n bool isPalindrome = false;\n \n if (s[left] == s[right]) {\n if (left + 1 == right) { // Two characters that are the same\n isPalindrome = true;\n } else {\n isPalindrome = find(left + 1, right - 1, s, ans, dp);\n }\n \n if (isPalindrome) {\n if (ans.size() < right - left + 1) {\n ans = s.substr(left, right - left + 1);\n }\n return dp[left][right] = true;\n }\n }\n \n // Explore both possibilities if current characters do not match\n find(left, right - 1, s, ans, dp);\n find(left + 1, right, s, ans, dp);\n\n return dp[left][right] = false;\n }\n\n string longestPalindrome(string s) {\n string ans;\n ans+=s[0];\n \n int n = s.size();\n vector<vector<int>> dp(n, vector<int>(n, -1));\n \n find(0, s.size() - 1, s, ans, dp);\n return ans;\n }\n};\n", "memory": "307289" }
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 len=s.length();\n int max_len=1;\n int start=0;\n vector<vector<int>>dp(len,vector<int>(len,0));\n for(int i=0;i<len;i++)\n {\n dp[i][i]=1;\n }\n for(int i=0;i<len-1;i++)\n {\n if(s[i]==s[i+1])\n {\n dp[i][i+1]=1;\n max_len=2;\n start=i;\n }\n }\n for(int k=3;k<=len;k++)\n {\n for(int i=0;i<=len-k;i++)\n {\n int j=i+k-1;\n if(s[i]==s[j] && dp[i+1][j-1]==1)\n {\n dp[i][j]=1;\n \n int prev=max_len;\n max_len=max(max_len,k);\n if(prev!=max_len)\n {\n start=i;\n }\n }\n }\n }\n string my_str=\"\";\n for(int i=start;i<start+max_len;i++)\n {\n my_str=my_str+s[i];\n } \n return my_str;\n \n }\n};", "memory": "313335" }
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 n = s.size();\n vector<vector<int>> dp(n, vector<int>(n, 0));\n for(int i = 0; i < n; i++){\n for(int j = 0; j < n; j++){\n int l = i + j;\n if(l < n){\n //dp[l][j]\n if(l == j){\n dp[j][l] = 1;\n }else{\n if(l - j == 1){\n if(s[l] == s[j]){\n dp[j][l] = 2;\n } \n continue;\n }\n if(s[l] == s[j] && dp[j + 1][l - 1] != 0){\n dp[j][l] = dp[j + 1][l - 1] + 2;\n }\n \n }\n }else\n break;\n }\n }\n int p, q;\n int val = INT_MIN;\n for(int i = 0; i< n; i++){\n for(int j = 0; j < n; j++){\n // cout << dp[i][j] << \" \";\n if(val < dp[i][j]){\n val = dp[i][j];\n p = i;\n q = j;\n }\n }\n cout << endl;\n }\n string ans = \"\";\n // cout << p << \" \" << q << endl;\n for(int i = p; i <= q; i++){\n ans = ans + s[i];\n }\n return ans;\n }\n};\n// 0 1 2 3 4\n// b a b a d\n// 0 b 1 0 \n// 1 a 1 0\n// 2 b 1 0\n// 3 a 1 0\n// 4 d 1 ", "memory": "313335" }
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 n = s.size();\n unordered_map < char, vector<int>> hash;\n\n for (int i = 0; i < s.size(); i++) {\n hash[s[i]].push_back(i);\n }\n\n string maxPalindrome = \"\";\n int maxSize = 0;\n bool skip = false; // If can skip rest of backwards search\n\n for (int i = 0; i < n; i++) { // Loops from left to right through the string\n if (maxSize >= (n - i)) // if the largest palindrome so far is larger than the rest of the checkable strings, return\n return maxPalindrome;\n\n vector<int> indexes = hash[s[i]];\n int indexNums = indexes.size();\n\n if (indexNums > 1) { // Check if there are more \n for (int t = indexNums-1; t > 0; t--) { // Loops through the indexes where the starting char exists\n int index = indexes[t];\n if ((index - i) < maxSize) // If indexes are too close together to be bigger than largest palindrome\n break;\n string palindrome(index - i + 1, '.'); // Placeholder string\n int size = 2;\n palindrome[0] = s[i];\n palindrome[index - i] = s[index];\n skip = true;\n for (int x = 1; x <= (index-i)/2.0; x++) {\n if (s[i+x] == s[index-x]) {\n if (i + x == index - x) { \n size += 1;\n } else { // Else add two, for adding one on left and one on right\n size += 2;\n }\n palindrome[x] = s[i + x];\n palindrome[index - i - x] = s[index - x];\n } else {\n skip = false;\n size = 0;\n break;\n }\n }\n\n if (size > maxSize) {\n maxSize = size;\n maxPalindrome = palindrome;\n }\n \n if (skip) break;\n\n }\n } \n hash[s[i]].erase(hash[s[i]].begin()); // Remove the first value in the vector\n }\n\n if (maxSize == 0 && maxPalindrome == \"\")\n maxPalindrome = s[0];\n\n return maxPalindrome;\n }\n};", "memory": "349613" }
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 bool ispalindrome(string s){\n int i=0,j=s.length()-1;\n while(i<j){\n if(s[i]!=s[j])\n return false;\n i++,j--;\n }\n return true;\n }\npublic:\n string longestPalindrome(string s) {\n if(s.length()==1) return s;\n string substring1;\n string substring2;\n int len=INT_MIN;\n int n=s.length();\n for(int i=0;i<n;i++){\n for(int j=i;j<n;j++){\n if((s[i]==s[j]) && (j-i+1)>len){\n if(ispalindrome(s.substr(i,j-i+1))){\n len=j-i+1;\n substring2=s.substr(i,j-i+1);\n }\n }\n }\n }\n return substring2;\n } \n};", "memory": "349613" }
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 getsubstr(string s,int i,int j)\n {\n string ans=\"\";\n for(int k=i;k<=j;k++){ans+=s[k];}\n return ans;\n }\n string longestPalindrome(string s) {\n int n=s.length();\n vector<vector<int>> dp(n,vector<int>(n,0));\n for(int i=0;i<n;i++)\n {\n for(int j=i;j>=0;j--)\n {\n \n if(i==j){dp[i][j]=1;}\n else if(i-j==1)\n {\n if(s[i]==s[j]){dp[i][j]=1;}\n // else{dp[i][j]=0;}\n }\n else if(dp[i-1][j+1]==1&&s[i]==s[j])\n {\n dp[i][j]=1;\n }\n \n }\n \n }\n \n int ans=0;\n string h=\"\";\n for(int i=0;i<n;i++)\n {\n int mi=INT_MAX,ma=-1;\n for(int j=0;j<=i;j++)\n {\n if(dp[i][j]){mi=min(mi,j);ma=max(ma,j);}\n }\n if((ma-mi)>=ans){ans=ma-mi;h=getsubstr(s,mi,ma);}\n }\n return h;\n \n }\n};", "memory": "355659" }
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 longest=\"\";\n int n=s.length();\n int j;\n\n for(int i=0;i<n;i++){\n j=n-1;\n\n string subs=\"\";\n\n string subsrev=\"\";\n\n while(i<j){\n\n if(s[i]==s[j] && longest.length()<(j-i+1)){\n subs= s.substr(i,(j-i+1));\n subsrev= subs;\n reverse(subsrev.begin(), subsrev.end());\n if(subs==subsrev){\n longest=subs;\n }\n }\n j--;\n \n }\n }\n\n if(longest.length()==0){\n longest= s[0];\n }\n\n \n\n return longest;\n\n \n }\n};", "memory": "355659" }
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": "/*\nSo The first thing that comes to my mind is-\nCan have 2 pointers starting from the 0 index going until n-1 increasing by 2 everytime \n(can't be coz there can be a palindrome of even or odd size so I'll increase by 1 only)\nj starts from i + 1 everytime----> i goes until n-2\nSoo this is just brute force, but I have an Idea, I can keep an hashmap which simply keeps a\ntrack of the count of each character, if they are even only then I'll check else no \n and there's\none more condition, I'll also keep a counter to count the number of chars having count 1 coz\nthat can only be either 0 or 1 (think) and if it's greater than that then I aint checking\nUpdated: It would be count of characters having odd count...\nAnd I guess both of these conditions just can be solved by using the counter variable I \nmentioned above\n\nunordered_map<char, int> charCount;\nint charHavingOddCount = 0;\nvoid increaseCharCount(char c){\n if(!charCount.count(c)) {\n charHavingOddCount++;\n }\n else if(charCount[c] % 2){\n charHavingOddCount--;\n }\n charCount[c]++;\n}\nbool checkPalindrome(string& pali){\n int n = pali.size();\n for(int i = 0; i <= n/2; i++){\n if(pali[i] != pali[n-i-1]) return false;\n }\n return true;\n}\nclass Solution {\npublic:\n string longestPalindrome(string s) {\n if(s.size() <=1) return s;\n string ans = \"\";\n ans += s[0];\n for(int i =0; i < s.size()-1; i++){\n increaseCharCount(s[i]);\n for(int j = i+1; j < s.size(); j++){\n increaseCharCount(s[j]);\n if(!(charHavingOddCount >= 1)){\n string k = s.substr(i, j-i+1);\n if(k.size() > ans.size()) {\n bool flag = checkPalindrome(k);\n if(flag) ans = k;\n }\n }\n }\n }\n return ans;\n }\n};\nOne good condition check I got after I got TLE from the above soln T_T:\nKeep increasing j pointer until it j's char is not equal to i's char\n*/\n\nbool checkPalindrome(string& pali){\n int n = pali.size();\n for(int i = 0; i <= n/2; i++){\n if(pali[i] != pali[n-i-1]) return false;\n }\n return true;\n}\n\nclass Solution {\npublic:\n string longestPalindrome(string s) {\n if(s.size() <=1) return s;\n string ans = \"\";\n int i =0;\n ans += s[0];\n while(i < s.size()-1){\n int j = (1 > ans.size()) ? i+1 : i + ans.size();\n while(j < s.size()){\n while(j < s.size() && s[i] != s[j]) j++;\n string k = s.substr(i, j-i+1);\n bool flag = checkPalindrome(k);\n if(flag) ans = k;\n j++;\n }\n i++;\n }\n return ans;\n }\n};", "memory": "361705" }
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": "/*\nSo The first thing that comes to my mind is-\nCan have 2 pointers starting from the 0 index going until n-1 increasing by 2 everytime \n(can't be coz there can be a palindrome of even or odd size so I'll increase by 1 only)\nj starts from i + 1 everytime----> i goes until n-2\nSoo this is just brute force, but I have an Idea, I can keep an hashmap which simply keeps a\ntrack of the count of each character, if they are even only then I'll check else no \n and there's\none more condition, I'll also keep a counter to count the number of chars having count 1 coz\nthat can only be either 0 or 1 (think) and if it's greater than that then I aint checking\nUpdated: It would be count of characters having odd count...\nAnd I guess both of these conditions just can be solved by using the counter variable I \nmentioned above\n\nunordered_map<char, int> charCount;\nint charHavingOddCount = 0;\n\nvoid increaseCharCount(char c){\n if(!charCount.count(c)) {\n charHavingOddCount++;\n }\n else if(charCount[c] % 2){\n charHavingOddCount--;\n }\n charCount[c]++;\n}\n\nbool checkPalindrome(string& pali){\n int n = pali.size();\n for(int i = 0; i <= n/2; i++){\n if(pali[i] != pali[n-i-1]) return false;\n }\n return true;\n}\n\nclass Solution {\npublic:\n string longestPalindrome(string s) {\n if(s.size() <=1) return s;\n string ans = \"\";\n ans += s[0];\n for(int i =0; i < s.size()-1; i++){\n increaseCharCount(s[i]);\n for(int j = i+1; j < s.size(); j++){\n increaseCharCount(s[j]);\n if(!(charHavingOddCount >= 1)){\n string k = s.substr(i, j-i+1);\n if(k.size() > ans.size()) {\n bool flag = checkPalindrome(k);\n if(flag) ans = k;\n }\n }\n }\n }\n return ans;\n }\n};\n\nOne good condition check I got after I got TLE from the above soln T_T:\nKeep increasing j pointer until it j's char is not equal to i's char\n\n*/\n\nbool checkPalindrome(string& pali){\n int n = pali.size();\n for(int i = 0; i <= n/2; i++){\n if(pali[i] != pali[n-i-1]) return false;\n }\n return true;\n}\n\nclass Solution {\npublic:\n string longestPalindrome(string s) {\n if(s.size() <=1) return s;\n string ans = \"\";\n int i =0;\n ans += s[0];\n while(i < s.size()-1){\n int j = (1 > ans.size()-1) ? i+1 : i + ans.size()-1;\n while(j < s.size()){\n while(j < s.size() && s[i] != s[j]) j++;\n string k = s.substr(i, j-i+1);\n bool flag = checkPalindrome(k);\n if(flag) ans = k;\n j++;\n }\n i++;\n }\n return ans;\n }\n};", "memory": "361705" }
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 void buildlps(string &a , vector<int> &lps){\n int n=a.size();\n int i=1;\n int len=0;\n while(i<n){\n if(a[i]==a[len]){\n lps[i]=len+1;\n i++;\n len++;\n }\n else {\n if(len==0){\n lps[i]=0;\n i++;\n }\n else{\n len=lps[len-1];\n }\n }\n }\n }\n\n string longestPalindrome(string s) {\n int n1=s.size();\n string ans=\"\";\n string a=\"@\";\n for(int i=(n1-1);i>=0;i--){\n a=s[i]+a+s[i];\n int n2=a.size();\n vector<int> lps(n2,0);\n buildlps(a,lps);\n //ans=max(ans,lps[n2-1]);\n if(lps[n2-1]>ans.size()) ans=a.substr(0,lps[n2-1]);\n }\n return ans;\n }\n};", "memory": "367751" }
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 check(string s)\n {\n int l=s.length();\n for(int i=0;i<l/2;i++)\n if(s[i]!=s[l-i-1])\n return 0;\n return 1;\n }\n string longestPalindrome(string s) {\n string t=s;\n reverse(s.begin(),s.end());\n int m=s.length(),n=t.length(),maxLen=0,endIndex=0;;\n vector<vector<int>> dp(m+1,vector<int>(n+1,0));\n for(int i=1;i<=m;i++)\n {\n for(int j=1;j<=n;j++)\n {\n if(s[i-1]==t[j-1])\n {\n dp[i][j]=1+dp[i-1][j-1];\n if(dp[i][j]>maxLen && check(s.substr(i - dp[i][j], dp[i][j]))) \n {\n maxLen = dp[i][j];\n endIndex = i - 1;\n }\n }\n else\n dp[i][j]=0;\n }\n }\n return s.substr(endIndex - maxLen + 1, maxLen);\n }\n};", "memory": "373798" }
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 n = s.size();\n string rev_s = s; // 复制s\n reverse(rev_s.begin(), rev_s.end()); // 倒置s\n int max_len = 0, end_idx = 0;\n \n // 建立一个二维数组来存储公共子串长度\n vector<vector<int>> dp(n+1, vector<int>(n+1, 0));\n \n for (int i = 1; i <= n; ++i) {\n for (int j = 1; j <= n; ++j) {\n if (s[i-1] == rev_s[j-1]) {\n dp[i][j] = dp[i-1][j-1] + 1;\n // 检查子串是否是回文,且长度大于当前最长\n if (dp[i][j] > max_len && isPalindrome(s.substr(i - dp[i][j], dp[i][j]))) {\n max_len = dp[i][j];\n end_idx = i;\n }\n }\n }\n }\n return s.substr(end_idx - max_len, max_len);\n }\n \n bool isPalindrome(const string& str) {\n int left = 0, right = str.size() - 1;\n while (left < right) {\n if (str[left++] != str[right--]) {\n return false;\n }\n }\n return true;\n }\n};\n", "memory": "379844" }
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 n = s.size();\n string rev_s = s; // 复制s\n reverse(rev_s.begin(), rev_s.end()); // 倒置s\n int max_len = 0, end_idx = 0;\n \n // 建立一个二维数组来存储公共子串长度\n vector<vector<int>> dp(n+1, vector<int>(n+1, 0));\n \n for (int i = 1; i <= n; ++i) {\n for (int j = 1; j <= n; ++j) {\n if (s[i-1] == rev_s[j-1]) {\n dp[i][j] = dp[i-1][j-1] + 1;\n // 检查子串是否是回文,且长度大于当前最长\n if (dp[i][j] > max_len && isPalindrome(s.substr(i - dp[i][j], dp[i][j]))) {\n max_len = dp[i][j];\n end_idx = i;\n }\n }\n }\n }\n return s.substr(end_idx - max_len, max_len);\n }\n \n bool isPalindrome(const string& str) {\n int left = 0, right = str.size() - 1;\n while (left < right) {\n if (str[left++] != str[right--]) {\n return false;\n }\n }\n return true;\n }\n};\n", "memory": "379844" }
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{\n public:\n\n bool solve(int i, int j, string &s, int n, vector<vector<int>>&dp)\n {\n\n if (i == n || j < 0) return 0;\n\n \n if(dp[i][j] != -1)\n return dp[i][j];\n if (i >= j)\n return true;\n \n if (s[i] == s[j])\n {\n return dp[i][j] = solve(i + 1, j - 1, s, n, dp);\n }\n\n return dp[i][j] = false;\n }\n string longestPalindrome(string s)\n {\n\n int n = s.size();\n vector<vector<int>>dp(n, vector<int>(n, -1));\n int maxSize = INT_MIN;\n string ans;\n int startIndex;\n for (int i = 0; i < n; i++)\n {\n string temp = \"\";\n\n for (int j = i; j < n; j++)\n {\n temp += s[j];\n\n if (solve(i, j, s, n, dp))\n {\n if (j - i + 1 > maxSize)\n {\n maxSize = j - i + 1;\n startIndex = i;\n } \n }\n }\n }\n\n return s.substr(startIndex, maxSize);\n }\n};", "memory": "385890" }
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\nprivate:\n bool check(int i, int j, string &s, vector <vector<int>> &dp){\n int n = s.size();\n\n if(i > j){\n return true;\n }\n\n if(dp[i][j] != -1) return dp[i][j];\n\n if(s[i] != s[j]){\n return dp[i][j] = false;\n }\n\n return dp[i][j] = check(i+1, j-1, s, dp);\n }\n\npublic:\n string longestPalindrome(string s) {\n int n = s.size();\n string ans;\n int maxi = 0;\n vector <vector<int>> dp(n, vector<int>(n, -1));\n\n for(int i=0; i<n; i++){\n string temp;\n for(int j=i; j<n; j++){\n temp.push_back(s[j]);\n if(check(i, j, s, dp)) {\n if(j-i+1 > maxi){\n maxi = j-i+1;\n ans = temp;\n }\n }\n \n }\n }\n\n return ans;\n }\n};", "memory": "385890" }
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 checkPalindrome(string &s, int i,int j){\n // while(i<=j){\n // if(s[i]==s[j]){\n // i++;\n // j--;\n // }else{\n // return false;\n // }\n // }\n // return true;\n // }\nvector<vector<int>> dp;\n int checkPalindrome(string &s, int i,int j){\n if(i>=j)return 1;\n\n if(dp[i][j]!=-1)return dp[i][j];\n if(s[i]==s[j]){\n return dp[i][j] = checkPalindrome(s,i+1,j-1);\n }\n return 0;\n }\n \n string longestPalindrome(string s) {\n int maxLength = INT_MIN;\n int sp = 0;\n dp.resize(1001,vector<int>(1001,-1));\n for(int i=0;i<s.size();i++){\n for(int j=i;j<s.size();j++){\n if(checkPalindrome(s,i,j)){\n if(j-i+1>maxLength){\n maxLength = j-i+1;\n sp = i;\n }\n }\n }\n }\n return s.substr(sp,maxLength);\n }\n};", "memory": "391936" }
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<vector<int>> dp; // Declare the dp vector here\n\n // Constructor to initialize the dp vector\n Solution() : dp(1001, vector<int>(1001, -1)) {}\n\n bool check(string &s, int i, int j) {\n if(i >= j) return 1;\n\n if(dp[i][j] != -1) return dp[i][j];\n if(s[i] == s[j]) {\n return dp[i][j] = check(s, i + 1, j - 1);\n }\n return dp[i][j] = 0;\n }\n\n string longestPalindrome(string s) {\n int st = 0;\n int maxi = 0;\n int n = s.length();\n\n for(int i = 0; i < n; i++) {\n for(int j = i; j < n; j++) {\n if(check(s, i, j)) {\n if(j - i + 1 > maxi) {\n st = i;\n maxi = j - i + 1;\n }\n }\n }\n }\n return s.substr(st, maxi);\n }\n};\n", "memory": "391936" }
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 list<char> maxChars;\n for (int i = 0; i < s.size(); i++) {\n \n list<char> chars;\n int left = i;\n int right = i;\n chars.push_back(s[i]);\n \n while (left-1 >= 0 && right+1 < s.size()) {\n if (s[left-1] == s[right+1]) {\n chars.push_front(s[--left]);\n chars.push_back(s[++right]);\n } else {\n break;\n }\n }\n if (chars.size() > maxChars.size()) {\n maxChars = chars;\n }\n\n if (i+1 < s.size() && s[i] == s[i+1]) {\n chars.clear();\n int left = i;\n int right = i;\n chars.push_back(s[i]);\n chars.push_back(s[++right]);\n \n while (left-1 >= 0 && right+1 < s.size()) {\n if (s[left-1] == s[right+1]) {\n chars.push_front(s[--left]);\n chars.push_back(s[++right]);\n } else {\n break;\n }\n }\n if (chars.size() > maxChars.size()) {\n maxChars = chars;\n }\n }\n \n }\n return std::string(maxChars.begin(), maxChars.end());\n }\n};", "memory": "397983" }
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": "#include <list>\n#include <algorithm>\nusing namespace std;\n\nclass Solution {\npublic:\n string longestPalindrome(string s) {\n string ans;\n ans.push_back(s[0]);\n list<char> tempAns;\n\n for (int i = 0; i < s.length() - 1; i++)\n {\n if (s[i] == s[i + 1])\n {\n tempAns.push_back(s[i]);\n tempAns.push_back(s[i + 1]);\n int minimum = (i + 1 < s.length() - i - 1) ? i + 1 : s.length() - i - 1;\n for (int d = 1; d < minimum; d++)\n {\n if (s[i - d] == s[i + 1 + d])\n {\n tempAns.push_back(s[i + 1 + d]);\n tempAns.push_front(s[i - d]);\n }\n else\n {\n if (tempAns.size() > ans.size())\n {\n ans.clear();\n ans.insert(ans.begin(), tempAns.begin(), tempAns.end());\n }\n \n tempAns.clear();\n break;\n }\n }\n if (tempAns.size() > ans.size())\n {\n ans.clear();\n ans.insert(ans.begin(), tempAns.begin(), tempAns.end());\n \n }\n tempAns.clear();\n }\n }\n\n tempAns.clear();\n\n for (int i = 1; i < s.length(); i++)\n {\n tempAns.push_back(s[i]);\n int minimum = (i + 1 < s.length() - i) ? i + 1 : s.length() - i;\n for (int d = 1; d < minimum; d++)\n {\n if (s[i + d] == s[i - d])\n {\n tempAns.push_back(s[i + d]);\n tempAns.push_front(s[i - d]);\n }\n else\n {\n if (tempAns.size() > ans.size())\n {\n ans.clear();\n ans.insert(ans.begin(), tempAns.begin(), tempAns.end());\n }\n \n tempAns.clear();\n break;\n }\n }\n\n if (tempAns.size() > ans.size())\n {\n ans.clear();\n ans.insert(ans.begin(), tempAns.begin(), tempAns.end());\n \n }\n tempAns.clear();\n }\n return ans;\n }\n};", "memory": "397983" }
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 isPalindrome(string s)\n{\n for (int i = 0; i < s.length(); i++)\n {\n if(s.at(i) != s.at(s.length() - i - 1))\n {\n return false;\n }\n }\n return true;\n}\n string longestPalindrome(string s) {\n int left, right;\n int longestStartIndex = 0, longestLength = 0;\n\n for (left = 0; left < s.length() - longestLength; left++)\n {\n for (right = s.length() - 1; right >= left; right--)\n {\n if(s.at(right) == s.at(left))\n {\n if(isPalindrome(s.substr(left, right - left + 1)))\n {\n if(right - left + 1 > longestLength)\n {\n longestStartIndex = left;\n longestLength = right - left + 1;\n break;\n }\n }\n }\n }\n }\n\n return s.substr(longestStartIndex, longestLength);\n }\n};", "memory": "404029" }
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 getPalindrom(const string& s, int pos0, int pos1) {\n if (pos0 < 0) return \"\";\n if (pos1 >= s.size()) return \"\";\n if (pos0 > pos1) return \"\";\n\n string ps(s.begin() + pos0, s.begin() + pos1 + 1);\n\n //cout << \"ps \" << ps << endl;\n\n if (pos0 == 0 || pos1 == s.size() - 1)\n return ps;\n\n int i = 1;\n char s0 = s[pos0 - i];\n char s1 = s[pos1 + i];\n\n while (s0 == s1) {\n ps = s0 + ps + s1;\n i++;\n if (pos0 - i < 0) break;\n if (pos1 + i >= s.size()) break;\n s0 = s[pos0 - i];\n s1 = s[pos1 + i];\n }\n\n return ps;\n }\n\n string longestPalindrome0(string s) {\n if (s.empty()) return \"\";\n if (s.size() == 1) return s;\n\n string max_pal(1, s[0]);\n if (s[0] == s[1]) {\n max_pal = string(\"\") + s[0] + s[1];\n }\n int i0 = 0;\n int i1 = 2;\n while (i1 < s.size()) {\n int step = 1;\n string ps = \"\";\n if (s[i0 + 1] == s[i1]) {\n ps = getPalindrom(s, i0 + 1, i1);\n if (ps.size() > max_pal.size()) {\n max_pal = ps;\n }\n }\n\n if (s[i0] == s[i1]) {\n ps = getPalindrom(s, i0, i1);\n if (ps.size() > max_pal.size()) {\n max_pal = ps;\n }\n }\n\n i0++;\n i1++;\n }\n\n //cout << getPalindrom(\"1234567650000000\", 6, 6) << endl;\n //cout << getPalindrom(\"1234567765000 0\", 6, 6) << endl;\n // | |\n // \"123456765000000\" step 5\n // |\n // \"1234567765 0000000\"\n\n return max_pal;\n }\n\n \n string longestPalindrome(string s) {\n //s = \"700000002011151113144\";\n if (s.empty()) return \"\";\n if (s.size() == 1) return s;\n\n deque<pair<int, int>> repetitions;\n\n int i0 = 0;\n for (int i = 1; i < s.size(); i++) {\n if (s[i] == s[i - 1]) {\n if (i == s.size() - 1) {\n repetitions.push_back(make_pair(i0, i));\n //cout << \"rep \" << i0 << \" \" << i << endl;\n }\n continue;\n }\n else if (s[i] != s[i - 1] && i - 1 > i0) {\n repetitions.push_back(make_pair(i0, i - 1));\n //cout << \"rep \" << i0 << \" \" << i - 1 << endl;\n i0 = i;\n }\n else \n i0 = i;\n }\n\n vector<int> triplet_centers;\n repetitions.push_front(make_pair(0, 0));\n repetitions.push_back(make_pair(s.size() - 1, s.size() - 1));\n\n for (int i = 1; i < repetitions.size(); i++) {\n int i0 = repetitions[i - 1].second + 1;\n int i1 = repetitions[i].first - 1;\n\n for (int j = i0; j <= i1; j++) {\n if (s[j - 1] == s[j + 1]) {\n triplet_centers.push_back(j);\n //cout << \"triplet center \" << j << endl;\n }\n }\n }\n\n string best_pal = \"\";\n\n for (int i = 1; i < repetitions.size() - 1; i++) {\n int i0 = repetitions[i].first;\n int i1 = repetitions[i].second;\n string ps = getPalindrom(s, i0, i1);\n if (ps.size() > best_pal.size()) {\n best_pal = ps;\n }\n }\n\n for (int i = 0; i < triplet_centers.size(); i++) {\n int i0 = triplet_centers[i] - 1;\n int i1 = triplet_centers[i] + 1;\n string ps = getPalindrom(s, i0, i1);\n if (ps.size() > best_pal.size()) {\n best_pal = ps;\n }\n }\n\n if (best_pal == \"\" && s.size() > 0) return string(1, s[0]);\n\n return best_pal;\n }\n};", "memory": "404029" }
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 isPalindrome(string s){\n int n = s.size();\n int i=0,j=n-1;\n while(i<j){\n if(s[i] != s[j]){\n return false;\n }\n i++;j--;\n }\n return true;\n }\n string longestPalindrome(string s) {\n int n = s.size();\n int len = 0;\n string ans = \"\";\n for(int i=0;i<n;i++){\n string res = \"\";\n for(int j=i;j<n;j++){\n res+=s[j];\n int resLen = j-i+1;\n if(res[0] == res[resLen-1] && len < resLen){\n if(isPalindrome(res)) {\n if(len < res.size()){\n ans = res;\n len = res.size();\n }\n }\n }\n }\n }\n return ans;\n }\n};", "memory": "410075" }
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 checkPalindrome(string str)\n {\n if (!str .size())\n {\n return true;\n }\n int sz = str.size();\n for (int i = 0; i <= sz / 2 - 1 ; i++)\n {\n if (str[i] != str[sz - i - 1])\n {\n return false;\n }\n }\n return true;\n }\n string longestPalindrome(string s) {\n int sz = s.size();\n string result = \"\";\n result += (s[0]);\n if (sz == 1)\n {\n return result;\n }\n for (int i = 0; i < sz - 1; i++)\n {\n string niceTry(1, s[i]); \n for (int j = i + 1; j < sz; j++)\n {\n niceTry += s[j];\n if (niceTry[0] != niceTry[niceTry.size() - 1])\n {\n continue;\n }\n if (result.size() <= niceTry.size() && checkPalindrome(niceTry))\n {\n result = niceTry;\n }\n }\n }\n\n return result;\n }\n\n};", "memory": "410075" }
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() <= 1) \n return s;\n \n string ans = \"\";\n for (int i = 0; i < s.size(); i++) {\n string pal = checkBack(s.substr(i));\n if (pal.size() > ans.size()) {\n ans = pal;\n }\n }\n\n return ans;\n }\n\n string checkBack(string s) {\n for (int i = 0; i < s.size(); i++) {\n if (s[0] == s[s.size()-i-1] && isPalendrome(s.substr(0, s.size()-i)))\n return s.substr(0, s.size()-i);\n }\n return \"\";\n }\n\n bool isPalendrome(string s) {\n for (int i = 0; i < s.size()/2; i++) {\n if (s[i] != s[s.size()-i-1])\n return false;\n }\n return true;\n }\n};", "memory": "416121" }
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 isPalindrome(int l, int r, string s){\n while(l < r){\n if(s[l] != s[r]){\n return false;\n }\n l++;\n r--;\n }\n return true;\n }\n\n string longestPalindrome(string s) {\n int n = s.length();\n int maxLen = 0;\n int maxL = 0;\n int maxR = 0;\n for(int i = 0; i < n; i++){\n int l = i;\n int r = i;\n \n while((l >= 0) && (r < n) && (s[l] == s[r])){\n if(isPalindrome(l, r, s)){\n int len = r - l + 1;\n if(len > maxLen){\n maxLen = len;\n maxL = l;\n maxR = r;\n }\n }\n l--;\n r++;\n }\n l = i;\n r = i + 1;\n while((l >= 0) && (r < n) && s[l] == s[r]){\n if(isPalindrome(l, r, s)){\n int len = r - l + 1;\n if(len > maxLen){\n maxLen = len;\n maxL = l;\n maxR = r;\n }\n }\n l--;\n r++;\n }\n }\n return s.substr(maxL, maxR - maxL + 1);\n }\n};", "memory": "416121" }
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 maxString=s.substr(0, 1);\n for (int i=1; i<s.size()-maxString.size()/2; i++){\n int j=1;\n if (s[i-1]==s[i]){\n string subEven=s.substr(i-1, 2);\n while (i-1-j>=0 && i+j<s.size()){\n if (s[i-1-j]==s[i+j]){\n subEven=s.substr(i-j-1, 2*j+2);\n }\n else{\n break;\n }\n j++;\n }\n if (subEven.size()>maxString.size())\n maxString=subEven;\n }\n j=1;\n string subOdd=s.substr(i, 1);\n while (i-j>=0 && i+j<s.size()){\n if (s[i-j]==s[i+j]){\n subOdd=s.substr(i-j, 2*j+1);\n }\n else{\n break;\n }\n j++;\n }\n if (subOdd.size()>maxString.size())\n maxString=subOdd;\n }\n return maxString;\n }\n};", "memory": "422168" }
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": "// longest_palindromic_substr\nclass Solution {\npublic:\n int checkPara(string s, int left, int right, bool &loop) {\n if (loop) {\n if (((left-1)<0 && s[left]!=s[right+1]) || ((right+1)>=s.size() && s[left-1]!=s[right]))\n loop = false;\n if ((left-1)>=0 && (right+1)<s.size())\n if (s[left]!=s[left-1] && s[right]!=s[right+1])\n loop = false;\n }\n if ((left-1)>=0 && (right+1)<s.size())\n if (s[left-1] == s[right+1])\n return 0;\n if ((left-1) >= 0)\n if (loop==true && s[left-1]==s[right])\n return 1;\n if ((right+1) < s.size())\n if (loop==true && s[left]==s[right+1])\n return 2;\n return 3;\n }\n\n string longestPalindrome(string s) {\n int left, right;\n int smin, smax;\n int max = 0;\n for (int i=0; i<s.size(); i++) {\n left = i;\n right = i;\n bool loop = true;\n while (checkPara(s, left, right, loop) != 3) {\n if (checkPara(s, left, right, loop) == 0) {\n left-=1;\n right+=1;\n }\n else if (checkPara(s, left, right, loop) == 1)\n left-=1;\n else if (checkPara(s, left, right, loop) == 2)\n right+=1;\n }\n if (max < right-left+1) {\n max = right - left + 1;\n smin = left;\n smax = right;\n }\n if (smax - smin + 1 == s.size())\n break;\n }\n string ret = \"\";\n for (int i=smin; i<=smax; i++)\n ret += s[i];\n return ret;\n }\n};\n", "memory": "422168" }
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 n = s.size();\n string res = \"\";\n\n for(int i = 0; i < n; i++) {\n string cur(1,s[i]);\n int l = i-1;\n int r = i+1;\n\n while(l >= 0 && r < n && s[l] == s[r]) {\n cur = s[l] + cur + s[r];\n l--;\n r++;\n }\n\n l = i;\n r = i + 1;\n while (l >= 0 && r < n && s[l] == s[r]) {\n if (r - l + 1 > res.size()) {\n res = s.substr(l, r - l + 1);\n }\n l--;\n r++;\n }\n\n if(cur.size() > res.size()) {\n res = cur;\n }\n }\n\n return res;\n }\n};", "memory": "428214" }
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 n = s.size();\n string res = \"\";\n\n for(int i = 0; i < n; i++) {\n string cur(1,s[i]);\n int l = i-1;\n int r = i+1;\n\n while(l >= 0 && r < n && s[l] == s[r]) {\n cur = s[l] + cur + s[r];\n l--;\n r++;\n }\n\n l = i;\n r = i + 1;\n while (l >= 0 && r < n && s[l] == s[r]) {\n if (r - l + 1 > res.size()) {\n res = s.substr(l, r - l + 1);\n }\n l--;\n r++;\n }\n\n if(cur.size() > res.size()) {\n res = cur;\n }\n }\n\n return res;\n }\n};", "memory": "428214" }
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 (isPalindrome(s)) return s;\n\n int n = s.length();\n string soln{};\n for (int i{}; i < n; ++i) {\n string tmp{};\n for (int j{i}; j < n; ++j) {\n tmp += s[j];\n if (tmp[0] == s[j] && isPalindrome(tmp)) {\n if (tmp.length() > soln.length()) {\n soln = tmp;\n }\n }\n }\n }\n return soln;\n }\n bool isPalindrome(string s) {\n int n = s.length(), m = n / 2;\n for (int i{}; !(n & 1) ? i <= m : i < m ; ++i) {\n if (s[i] != s[n - i - 1]) return false;\n }\n return true;\n }\n};", "memory": "434260" }
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 isPalindrome(string s){\n int n = s.size();\n for(int i = 0;i<n/2;i++)\n if(s[i] != s[n-1-i])\n return false;\n return true;\n }\n string longestPalindrome(string s) {\n string ans = \"\";\n for(int i = 0;i<s.size();i++){\n string str = \"\";\n for(int j = i;j<s.size();j++){\n str += s[j];\n if(s[j] == s[i]){\n if(isPalindrome(str) && str.size() > ans.size())\n ans = str;\n }\n }\n if(ans.size() == s.size())\n break;\n }\n return ans;\n }\n};", "memory": "434260" }
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 ans=\"\";\n ans+=s[0];\n int maxi=1;\n\n for(int i =1 ; i<s.size()-1 ; i++){\n // checking for odd\n int start = i-1;\n int end=i+1;\n string right=\"\";\n string left=\"\";\n while(start>=0 && end<s.size() && s[start]==s[end]){\n right+=s[end];\n left=s[start]+left;\n start--;\n end++;\n }\n if((end-start-1)>maxi) {\n ans=left+s[i]+right;\n maxi=ans.size();\n }\n // checking for right\n if(s[i+1]==s[i]){\n int start=i-1;\n int end=i+2;\n string temp=\"\";\n temp+=s[i];\n temp+=s[i+1];\n string right=\"\";\n string left=\"\";\n \n while(start>=0 && end<s.size() && s[start]==s[end]){\n right+=s[end];\n left=s[start]+left;\n start--;\n end++;\n }\n if((end-start-1)>maxi) {\n ans=left+temp+right;\n maxi=ans.size();\n }\n\n }\n \n \n }\n if(s[0]==s[1]){\n string temp=\"\";\n temp+=s[0];\n temp+=s[1];\n if(temp.size()>maxi){\n maxi=temp.size();\n ans=temp;\n }\n \n }\n return ans;\n\n\n }\n};", "memory": "440306" }
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 left = 0;\n int right = 0; \n string max = \"\";\n string temp = \"\";\n\n for (int i = 0; i < s.length(); i++) {\n left = i - 1;\n right = i + 1;\n temp = s[i];\n if (left >= 0 && right <= (s.length() - 1) && s[left] == s[right]) {\n while (left >= 0 && right <= (s.length() - 1) && s[left] == s[right]) {\n temp = s[left] + temp + s[right];\n left--;\n right++;\n }\n } else if (right <= (s.length() - 1) && s[right] == s[i]) {\n\n while (right <= (s.length() - 1) && s[right] == s[i]) {\n temp = temp + s[right];\n right++;\n }\n if (left >= 0 && right <= (s.length() - 1)) {\n if (s[left] == s[right]) {\n while (left >= 0 && right <= (s.length() - 1) && s[left] == s[right]) {\n temp = s[left] + temp + s[right];\n left--;\n right++;\n }\n }\n }\n\n }\n if (temp.length() > max.length()) {\n max = temp;\n }\n temp = \"\";\n }\n return max;\n }\n};", "memory": "440306" }
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 odd(string s){\n string current = \"\";\n string ans =\"\";\n for(int i = 0 ;i <s.length() ; i++){\n int j= i;\n int k =i;\n while( j >= 0 && k < s.length() && s[j] == s[k]){\n current = s.substr(j , k - j +1);\n j--;\n k++;\n if(current.length() > ans.length()){\n ans = current;\n }\n }\n }\n return ans;\n }\n string even(string s){\n string current = \"\";\n string ans =\"\";\n for(int i = 0 ;i <s.length() ; i++){\n int j= i;\n int k =i+1;\n while( j >= 0 && k < s.length() && s[j] == s[k]){\n current = s.substr(j , k - j +1);\n j--;\n k++;\n if(current.length() > ans.length()){\n ans = current;\n }\n }\n }\n return ans;\n }\n string longestPalindrome(string s) {\n string ans1 = odd(s);\n string ans2 =even(s);\n string ans;\n if(ans1.length() > ans2.length()){\n ans = ans1;\n }else{\n ans = ans2;\n }\n return ans; \n }\n};", "memory": "446353" }
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\n //init the string with #\n string str = \"#\";\n for(int i = 0; i < s.length(); i++){\n str = str + s[i] + \"#\";\n }\n\n //init some variable\n int* LPS = new int[str.length()];\n int max_Z = 0, C = 0, R = 0; //maxZ(i) and the center and right of the bigger array\n //processing\n for(int i = 0; i < str.length(); i++){\n\n //init LPS[i] of loop\n //case 1:(i>=R) need calc LPS from beginning\n if(i >= R)\n LPS[i] = 0;\n //case 2 and 3:(i<R) calc LPS from min(R-i or LPS[i_mirror](cuz LPS[i_mirror] is calculated))\n else{\n int i_mirror = C - (i - C);\n LPS[i] = min(LPS[i_mirror], R - i);\n }\n\n //init the l and r when the first calc\n int l = i - LPS[i] - 1;\n int r = i + LPS[i] + 1;\n LPS[i] = Z(str, LPS[i], l, r);\n\n //update if LPS[i] > current max_Z\n if(LPS[i] > max_Z){\n C = i;\n R = C + LPS[i];\n max_Z = LPS[i];\n }\n }\n delete [] LPS;\n\n //get the final answer (longest palindromic substring)\n string ans;\n for(int i = C - max_Z; i < C + max_Z; i++){\n if(str[i] == '#')\n continue;\n ans = ans + str [i];\n }\n return ans;\n }\n\n //calc Z(i), Z(i) is radius of the substring \n int Z(string str, int init_Z, int left, int right){\n\n //if l and r didn't out of range and s[l]=s[r], Z(i)+1\n while(left >= 0 && right <= str.length() && str[left] == str[right]){\n init_Z++;\n left--;\n right++;\n }\n return init_Z;\n }\n};", "memory": "446353" }
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 \n int m=0;\n string st=\"\";\n int n=s.length();\n int i=0,j=s.length()-1;\n \n while(i<n)\n {\n if(s[i]==s[j])\n {\n string su=s.substr(i,j-i+1);\n string rev=su;\n reverse(rev.begin(),rev.end());\n if(su==rev)\n {\n if(su.length()>m)\n {\n m=su.length();\n st=su;\n }\n }\n }\n if(i==j||j-i+1<m)\n {\n i++;\n j=n;\n }\n if(n-i<m)\n {\n break;\n }\n j--; \n \n \n }\n return st;\n \n }\n};", "memory": "452399" }
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 n = s.length();\n\n // solution - 1 - brute force;\n\n // int i=0;\n // int maxLen = 0;\n // string pal;\n // for(i=0;i<n;i++){\n // for(int j=i;j<n;j++){\n // string currStr(s.begin()+i, s.begin()+j+1);\n // string rev(currStr.rbegin(), currStr.rend());\n // if(currStr == rev && (currStr.length() > maxLen)){\n // pal = currStr;\n // maxLen = currStr.length();\n // }\n // }\n // }\n // return pal;\n\n // solution - 2\n\n int i = 0;\n int maxLen = 0;\n string pal;\n for (i = 0; i < n; i++) {\n int j = n - 1;\n while (j >= i && ((j - i + 1) > maxLen)) {\n if (s[i] == s[j]) {\n string currStr(s.begin() + i, s.begin() + j + 1);\n string rev(currStr.rbegin(), currStr.rend());\n if (currStr == rev) {\n pal = currStr;\n maxLen = currStr.length();\n }\n }\n j--;\n }\n }\n return pal;\n }\n};", "memory": "452399" }
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 ans = \"\";\n\n unordered_map<char, vector<int>> map;\n\n for (int i = 0; i < s.length(); i++) {\n map[s[i]].push_back(i);\n }\n\n for (auto pair : map) {\n vector<int> vec = pair.second;\n for (int i = 0; i < vec.size(); i++) {\n for (int j = i; j < vec.size(); j++) {\n int lo = vec[i];\n int hi = vec[j];\n if (hi - lo + 1 > ans.size()) {\n string sub = s.substr(lo, hi - lo + 1);\n if (isPalindrome(sub)) {\n ans = sub;\n }\n }\n }\n }\n }\n\n return ans;\n }\n\n bool isPalindrome(string s) {\n int lo = 0;\n int hi = s.size() - 1;\n\n while (lo < hi) {\n if (s[lo++] != s[hi--]) {\n return false;\n }\n }\n\n return true;\n }\n};", "memory": "458445" }
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 ans = \"\";\n\n unordered_map<char, vector<int>> map;\n\n for (int i = 0; i < s.length(); i++) {\n map[s[i]].push_back(i);\n }\n\n for (auto pair : map) {\n vector<int> vec = pair.second;\n for (int i = 0; i < vec.size(); i++) {\n for (int j = i; j < vec.size(); j++) {\n int lo = vec[i];\n int hi = vec[j];\n if (hi - lo + 1 > ans.size()) {\n string sub = s.substr(lo, hi - lo + 1);\n if (isPalindrome(sub)) {\n ans = sub;\n }\n }\n }\n }\n }\n\n return ans;\n }\n\n bool isPalindrome(string s) {\n int lo = 0;\n int hi = s.size() - 1;\n\n while (lo < hi) {\n if (s[lo++] != s[hi--]) {\n return false;\n }\n }\n\n return true;\n }\n};", "memory": "458445" }
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 bool isPalindrome(const std::string& s, int start, int end) {\n while (start < end) {\n if (s[start] != s[end]) {\n return false;\n }\n start++;\n end--;\n }\n return true;\n }\n\n void formstring(string& ans, int i, int j, string s) {\n while (i < j + 1) {\n ans = ans + s[i];\n i++;\n }\n }\n\npublic:\n string longestPalindrome(string s) {\n\n int n = s.size();\n int count = 0;\n string ans = \"\";\n int maxLength = 0;\n for (int i = 0; i < n; ++i) {\n for (int j = i; j < n; ++j) {\n if (isPalindrome(s, i, j)) {\n int currentLength = j - i + 1;\n \n if (currentLength > maxLength) {\n ans = \"\";\n maxLength = currentLength;\n formstring(ans, i, j, s);\n }\n } \n }\n }\n return ans;\n }\n};", "memory": "464491" }
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 bool isPalindrome(const std::string& s, int start, int end) {\n while (start < end) {\n if (s[start] != s[end]) {\n return false;\n }\n start++;\n end--;\n }\n return true;\n }\n\n void formstring(string& ans, int i, int j, string s) {\n while (i < j + 1) {\n ans = ans + s[i];\n i++;\n }\n }\n\npublic:\n string longestPalindrome(string s) {\n\n int n = s.size();\n int count = 0;\n string ans = \"\";\n int maxLength = 0;\n for (int i = 0; i < n; ++i) {\n for (int j = i; j < n; ++j) {\n if (isPalindrome(s, i, j)) {\n int currentLength = j - i + 1;\n \n if (currentLength > maxLength) {\n ans = \"\";\n maxLength = currentLength;\n formstring(ans, i, j, s);\n }\n } \n }\n }\n return ans;\n }\n};", "memory": "464491" }
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 void solve(string s, int i, int j, string& str, int& len) {\n \n if (i < 0 || j >= s.length()) {\n return;\n }\n \n \n if (s[i] == s[j]) {\n if (len < j - i + 1) {\n str = s.substr(i, j - i + 1);\n len = j - i + 1;\n }\n \n solve(s, i - 1, j + 1, str, len);\n }\n }\n \n string longestPalindrome(string s) {\n int len = 0;\n string str;\n \n for (int i = 0; i < s.length(); ++i) {\n \n solve(s, i, i, str, len);\n \n solve(s, i, i + 1, str, len);\n }\n \n return str.empty() ? s.substr(0, 1) : str;\n }\n};\n", "memory": "470538" }
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.empty()) return \"\";\n vector<vector<int>> dp(s.size(), vector<int>(s.size(), -1));\n return f(s, 0, s.size() - 1, dp);\n }\n\nprivate:\n string f(string &s, int start, int end, vector<vector<int>>& dp) {\n if (start > end) return \"\";\n if (start == end) return string(1, s[start]);\n if (dp[start][end] != -1) {\n return s.substr(start, dp[start][end]);\n }\n if (ispalindrome(s, start, end)) {\n dp[start][end] = end - start + 1;\n return s.substr(start, dp[start][end]);\n }\n string s1 = f(s, start, end - 1, dp);\n string s2 = f(s, start + 1, end, dp);\n if (s1.size() > s2.size()) {\n dp[start][end] = s1.size();\n return s1;\n }\n dp[start][end] = s2.size();\n return s2;\n }\n\n bool ispalindrome(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", "memory": "470538" }
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 len = s.length();\n string ans = \"\";\n ans+=s[0];\n int i, j;\n for(int k=0;k<len;k++){\n i = k;\n j = k;\n // cout<<s[i]<<\" \"<<s[j]<<endl;;\n while(i>-1 && j<len && s[i]==s[j]){\n // cout<<s.substr(i, j-i+1)<<endl;\n if(ans.length()<s.substr(i, j-i+1).length()){\n ans = s.substr(i, j-i+1);\n }\n i--;\n j++;\n }\n i = k;\n j = k+1;\n while(i>-1 && j<len && s[i]==s[j]){\n // cout<<s.substr(i, j-i+1)<<endl;\n if(ans.length()<s.substr(i, j-i+1).length()){\n ans = s.substr(i, j-i+1);\n }\n i--;\n j++;\n }\n \n }\n return ans;\n }\n};", "memory": "476584" }
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 biggie=\"\";\n \n for(int i=0;i<s.size();i++){\n //case of odd,s[i],middle\n string shortie=\"\";\n shortie+=s[i];\n int j=1;\n while((j+i)<s.size()&&(i-j)>=0){\n if(s[i+j]==s[i-j]){\n string temp=\"\";\n temp+=s[i+j];\n temp+=shortie;\n shortie=temp;\n shortie+=s[i-j];\n j++;\n }\n else{\n break;\n }\n }\n if(shortie.size()>biggie.size()){\n biggie=shortie;\n }\n shortie=\"\";\n j=1;\n int c=i;\n int d=i-1;\n while((c)<s.size()&&(d)>=0){\n if(s[c]==s[d]){\n string temp=\"\";\n temp+=s[c];\n temp+=shortie;\n shortie=temp;\n shortie+=s[d];\n d--;\n c++;\n }\n else{\n break;\n }\n }\n if(shortie.size()>biggie.size()){\n biggie=shortie;\n }\n shortie=\"\";\n }\n return biggie;\n }\n};", "memory": "476584" }
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 pair<pair<int,int>,int> sol(string& s,int i,int j,vector<vector<pair<pair<int,int>,int>>>&dp){\n if(j<i) return {{0,0},1};\n if(i==j) {\n return {{i,i},1};\n }\n if(dp[i][j].second!=-1) return dp[i][j];\n pair<int,int>t={0,0};\n if(s[i]==s[j]){\n pair<pair<int,int>,int> f = sol(s,i+1,j-1,dp);\n if(f.second) t={i,j};\n }\n pair<pair<int,int>,int> a = sol(s,i,j-1,dp);\n pair<pair<int,int>,int> b = sol(s,i+1,j,dp);\n int al =a.first.second-a.first.first;\n int bl =b.first.second-b.first.first;\n if(t.second-t.first>al &&t.second-t.first>bl) return dp[i][j] = {t,1};\n if(al>t.second-t.first &&al>bl) return dp[i][j] = {{a.first.first,a.first.second},0};\n return dp[i][j]= {{b.first.first,b.first.second},0};\n }\n string longestPalindrome(string s) {\n int n =s.size();\n string ans=\"\";\n vector<vector<pair<pair<int,int>,int>>>dp(n,vector<pair<pair<int,int>,int>>(n,{{0,0},-1}));\n pair<pair<int,int>,int> f= sol(s,0,n-1,dp);\n for(int i=f.first.first;i<=f.first.second;i++){\n ans+=s[i];\n }\n return ans;\n }\n};", "memory": "482630" }
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.length() <= 1) {\n return s;\n }\n\n string longestStr = s.substr(0, 1);\n vector<vector<bool>> dp(s.length(), vector<bool>(s.length(), false));\n \n for (int i = 0; i < s.length(); i++) {\n dp[i][i] = true;\n for (int j = 0; j < i; j++) {\n // if string is 2 or 3 characters long and s[i] == s[j] we automatically know it's a palindrome\n if (s[j] == s[i] && (i - j <= 2 || dp[j + 1][i - 1])) {\n dp[j][i] = true;\n string temp = s.substr(j, i - j + 1);\n longestStr = longestStr.size() < temp.size() ? temp : longestStr;\n }\n }\n }\n \n return longestStr;\n }\n};", "memory": "482630" }
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 IsPalindromicSubstring(const string &s)\n {\n //cout << s << endl;\n int dwLength = s.length();\n for (int i = 0; i < dwLength / 2; i++)\n {\n if (s[i] != s[dwLength - 1 - i])\n {\n return false;\n }\n }\n return true;\n }\n\n string longestPalindrome(string s) \n {\n //cout << \"string:\" << s << endl;\n int dwLeft = 0;\n int dwRight = 0;\n std::unordered_map <char, std::set<int>> mapChar; //record the index of char already visited by i\n int dwLength = s.length();\n\n for (int i = 0; i < dwLength; i++)\n {\n if(mapChar.count(s[i]) > 0)\n {\n for (auto const &it : mapChar.find(s[i])->second)\n {\n //substr param1:start_idx param2:length\n if (IsPalindromicSubstring(s.substr(it, i - it + 1)))\n {\n //compare max length\n if (i - it > dwRight - dwLeft)\n {\n dwLeft = it;\n dwRight = i;\n }\n }\n }\n //maybe palindrome\n //refresh pos\n mapChar.find(s[i])->second.insert(i);\n }\n else\n {\n std::set<int> setIndex;\n setIndex.insert(i);\n mapChar[s[i]] = setIndex;\n }\n }\n\n //for (auto const& it : mapChar)\n //{\n // for (auto const &it1: it.second)\n // cout << it.first << \" \" << it1 << endl;\n //}\n\n return s.substr(dwLeft, dwRight - dwLeft + 1);\n }\n};", "memory": "488676" }
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 IsPalindromicSubstring(const string &s)\n {\n int i = 0;\n int j = s.length() - 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 //cout << \"string:\" << s << endl;\n int dwLeft = 0;\n int dwRight = 0;\n std::unordered_map <char, std::set<int>> mapChar; //record the index of char already visited by i\n int dwLength = s.length();\n\n for (int i = 0; i < dwLength; i++)\n {\n if(mapChar.count(s[i]) > 0)\n {\n for (auto const &it : mapChar.find(s[i])->second)\n {\n //substr param1:start_idx param2:length\n if (IsPalindromicSubstring(s.substr(it, i - it + 1)))\n {\n //compare max length\n if (i - it > dwRight - dwLeft)\n {\n dwLeft = it;\n dwRight = i;\n }\n }\n }\n //maybe palindrome\n //refresh pos\n mapChar.find(s[i])->second.insert(i);\n }\n else\n {\n std::set<int> setIndex;\n setIndex.insert(i);\n mapChar[s[i]] = setIndex;\n }\n }\n\n //for (auto const& it : mapChar)\n //{\n // for (auto const &it1: it.second)\n // cout << it.first << \" \" << it1 << endl;\n //}\n\n return s.substr(dwLeft, dwRight - dwLeft + 1);\n }\n};", "memory": "488676" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&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> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
0
{ "code": "// P A Y P A L I S H I R I N G\n\n// 0 1 2 3 4 5 6 7 8 9 10 11 12 13\n// P A H N \n// A P L S I I G\n// Y I R\n\n// -->\n\n// 0 1 2 3 4 5 6 7 8 9 10 11 12 13\n// P A H N \n// A P L S I I G\n// Y I R\n\n// P A H N A P L S I I G Y I R\n\nclass Solution {\npublic:\n std::string convert(const std::string& s, int numRows) {\n if(numRows == 1) {\n return s;\n }\n // prepare string for output and reserve sufficient space in advance\n std::string result;\n result.resize(s.size());\n \n // fill output line by line\n auto maxStep = (numRows - 1) << 1;\n auto outIter = result.begin();\n for(int i = 0; i < numRows; ++i) {\n bool goingDown = true;\n for(auto rowIter = s.begin() + i; rowIter < s.end();) {\n *outIter = *rowIter;\n \n auto step = std::abs((maxStep * goingDown) - (i<<1));\n \n rowIter += step;\n outIter += bool(step);\n goingDown = !goingDown;\n }\n }\n return result;\n }\n};", "memory": "9706" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&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> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
0
{ "code": "// P A Y P A L I S H I R I N G\n\n// 0 1 2 3 4 5 6 7 8 9 10 11 12 13\n// P A H N \n// A P L S I I G\n// Y I R\n\n// -->\n\n// 0 1 2 3 4 5 6 7 8 9 10 11 12 13\n// P A H N \n// A P L S I I G\n// Y I R\n\n// P A H N A P L S I I G Y I R\n\nclass Solution {\npublic:\n std::string convert(const std::string& s, int numRows) {\n if(numRows == 1) {\n return s;\n }\n // prepare string for output and reserve sufficient space in advance\n std::string result;\n result.resize(s.size());\n \n // fill output line by line\n auto maxStep = short(numRows - 1) << 1;\n auto outIter = result.begin();\n for(int i = 0; i < numRows; ++i) {\n bool goingDown = true;\n for(auto rowIter = s.begin() + i; rowIter < s.end();) {\n *outIter = *rowIter;\n \n auto step = std::abs((maxStep * goingDown) - short(i<<1));\n \n rowIter += step;\n outIter += bool(step);\n goingDown = !goingDown;\n }\n }\n return result;\n }\n};", "memory": "9706" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&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> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if(numRows == 1)\n {\n return s;\n }\n \n int zagSize = (numRows * 2) - 2;\n string ret(s);\n int counter = 0;\n // Gather the first row\n for(int i = 0; i < s.length(); i += zagSize)\n {\n ret[counter] = s[i];\n counter++;\n }\n\n // Gather the middle rows\n for(int row = 1; row < numRows - 1; row++)\n {\n for(int i = 0; i < s.length(); i += zagSize)\n {\n if(i + row < s.length())\n {\n ret[counter] = s[i + row];\n counter++;\n }\n if(i + zagSize - row < s.length())\n {\n ret[counter] = s[i + zagSize - row];\n counter++;\n }\n }\n }\n\n // Gather the last row\n for(int i = numRows - 1; i < s.length(); i += zagSize)\n {\n ret[counter] = s[i];\n counter++;\n }\n\n return ret;\n }\n};", "memory": "10118" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&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> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if (numRows == 1 || s.size() <= numRows) return s;\n int n = s.size();\n int freq = (numRows - 1) * 2;\n int offset = 0;\n string res;\n res.reserve(s.size());\n for (int i = 0; i < numRows; i++) {\n offset = (i == 0 || i == numRows - 1) ? 0 : 2 * i;\n res.append(1, s[i]);\n int next = offset ? i + freq - offset : i + freq;\n bool adj = true;\n while (next < n) {\n res.append(1, s[next]);\n next += offset ? adj ? offset : freq - offset : freq;\n adj = !adj;\n }\n }\n return res;\n }\n};", "memory": "10118" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&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> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n string ans;\n int p=0;\n int k=0;\n while(p<numRows){\n int iter=p;\n while(iter<s.length()){\n if(p!=0 && p!=numRows-1 && iter-k>=0){\n ans+=s[iter-k];\n }\n ans+=s[iter];\n int ch=iter;\n iter+= (numRows+numRows-2);\n if(iter==ch){\n ++iter;\n }\n }\n if(p!=0 && p!=numRows-1 && iter-k>=0 && iter-k<s.length()){\n ans+=s[iter-k];\n }\n ++p;\n k+=2;\n }\n\n\n\nreturn ans;\n }\n};", "memory": "10531" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&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> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if (numRows == 1) return s;\n string ans;\n int group = 2 * numRows - 2;\n for (int i = 1; i <= numRows; ++i) {\n int interval = i == numRows ? group : 2 * numRows - 2 * i;\n int idx = i - 1;\n while (idx < s.length()) {\n ans.push_back(s[idx]);\n idx += interval;\n interval = group - interval;\n if (interval == 0) interval = group;\n }\n }\n return ans;\n }\n};", "memory": "10531" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&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> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n int n = s.length();\n string output = \"\";\n if(n==1 || numRows==1) return s;\n int space = 2*numRows -2;\n for(int i=0;i<numRows;i++){\n if(i==0 || i ==numRows-1){\n for(int j = i; j<n; j+=space){\n output+=s[j];\n }\n }\n else{\n int space1 = 2*(numRows-i-1);\n for(int j=i;j<n;j+=space){\n output+=s[j];\n if(j+space1<n) output+=s[j+space1];\n }\n }\n }\n return output;\n\n }\n};", "memory": "10943" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&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> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n int n = s.length();\n string output = \"\";\n if(numRows==1) return s;\n int space = 2*numRows -2;\n for(int i=0;i<numRows;i++){\n if(i==0 || i ==numRows-1){\n for(int j = i; j<n; j+=space){\n output+=s[j];\n }\n }\n else{\n int space1 = 2*(numRows-i-1);\n for(int j=i;j<n;j+=space){\n output+=s[j];\n if(j+space1<n) output+=s[j+space1];\n }\n }\n }\n return output;\n\n }\n};", "memory": "10943" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&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> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n string convert(string s, int n) {\n if (n == 1) return s;\n string res=\"\";\n string ds[n];\n int t = 0;\n bool flag = true;\n for(int i=0;i<s.size();i++){\n ds[t]+=s[i];\n if(flag) t++;\n else t--;\n if(t==n-1) flag = false;\n if(t==0) flag = true; \n }\n for(string st: ds){\n res+=st;\n }\n return res;\n }\n};", "memory": "11356" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&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> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if (numRows == 1) return s;\n char final[numRows][s.length()];\n memset(final, ' ', sizeof(final));\n\n\n int count = 0;\n int col = 0;\n int last = -1;\n bool going_down = true;\n\n\n while(count != s.length())\n {\n if(going_down){\n for(int i=0;i<numRows && count<s.length();i++)\n {\n final[i][col]=s[count];\n last = i;\n count++;\n }\n going_down=false;\n }\n else{\n for(int i=last-1; i>0 && count<s.length();i--)\n {\n final[i][++col]=s[count];\n count++;\n }\n going_down=true;\n } \n col++;\n } \n\n string result =\"\";\n for(int i=0;i<numRows;i++)\n {\n for(int j=0;j<col;j++)\n {\n if(final[i][j]!=' ') \n {\n result += final[i][j];\n }\n }\n } \n return result;\n }\n};", "memory": "11356" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&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> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n string convert(string s, int num_rows) {\n if(num_rows == 1)\n return s;\n\n const auto stride = num_rows + (num_rows - 2);\n std::stringstream strm;\n for(auto i = 0UL; i < num_rows; ++i) {\n auto c = i;\n while(c < s.size()) {\n auto mod = i % num_rows;\n strm << s[c];\n if(mod != 0 && mod != num_rows - 1) {\n const auto step = stride - (2 * mod);\n c += step;\n\n if(c >= s.size())\n continue;\n\n strm << s[c];\n c += stride - step;\n } else {\n c += stride;\n }\n }\n }\n return strm.str();\n }\n};", "memory": "11768" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&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> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if (numRows == 1)\n return s;\n\n stringstream ss;\n for (int row = 0; row < numRows; ++row) {\n int idx;\n int cnt = 0;\n while(true){\n idx = cnt * (2*(numRows-1));\n if (row != 0 && row != numRows-1) {\n if (idx > row && (idx - row) < s.size())\n ss << s[idx-row];\n if (idx + row < s.size())\n ss << s[idx + row];\n else\n break;\n } else {\n if (idx + row < s.size())\n ss << s[idx + row];\n else\n break;\n }\n ++cnt;\n }\n }\n return ss.str();\n }\n};\n\n// 0, 1* (2*(numRow-1)), 2* (2*(numRow-1)), ...\n// +1, -1,+1, -1,+1\n// ", "memory": "11768" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&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> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n vector<pair<char, int>> dp(s.size());\n int i=0, currRow=1;\n\n bool up = false, start=true;\n if(numRows==1) return s;\n while(i<s.size()){\n dp[i].first = s[i];\n dp[i].second = currRow;\n\n if(currRow==numRows){\n currRow--;\n up = !up;\n }\n else if(currRow==1){\n currRow++;\n if(!start)\n up = !up;\n else\n start = false;\n }\n else if(up){\n currRow--;\n }\n else currRow++;\n\n i++;\n }\n\n string res = \"\";\n\n for(int x=1; x<=numRows; x++){\n for(int i=0; i<dp.size(); i++){\n if(dp[i].second == x) res += dp[i].first;\n }\n }\n\n return res;\n }\n};", "memory": "12181" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&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> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n int n = s.size();\n if(n==0 || n==1) return s;\n if(numRows==0 || numRows==1) return s;\n // tc : numRows * n;\n int k = 2*numRows - 2;\n string ans=\"\";\n for(int i=1; i<k/2; i++)\n {\n for(int j=0; j<n; j++)\n {\n if(j%k==i || j%k==k-i) ans +=s[j];\n }\n }\n string start = \"\";\n for(int i=0; i<n; i++)\n {\n if(i%k==0) start += s[i];\n }\n string end =\"\";\n for(int i=0; i<n; i++)\n {\n if(i%k==numRows-1) end +=s[i];\n }\n string res = start+ans+end;\n return res;\n }\n};", "memory": "12181" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&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> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n //Boundary Case:\n if(numRows == 1 || numRows==s.size()) return s;\n\n //Normal Case Handling:\n vector<string> rows(numRows);\n int size = s.size(),j=0,inc=1;\n \n\n for(int i=0; i<size; i++){\n rows[j].push_back(s[i]);\n j += inc;\n if(j==numRows){\n inc = -1;\n j = numRows-2;\n }\n if(j<0){\n inc = 1;\n j = 1;\n }\n }\n // while(i<size){\n // for(int j=0; j<numRows && i<size ; j++){\n // rows[j].push_back(s[i++]);\n // }\n \n // if(numRows>2 && i<size){\n // for(int k=(numRows-2); k>=1 && i<size; k--){\n // rows[k].push_back(s[i++]);\n // }\n // }\n // }\n s=\"\";\n for(int i=0; i<numRows; i++ ){\n s.append(rows[i]);\n }\n return s;\n }\n};", "memory": "12593" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&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> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n std::string convert(const std::string& s, int numRows) {\n // Edge case: if numRows is 1 or s length is less than numRows\n if (numRows == 1 || numRows >= s.length()) {\n return s;\n }\n \n std::vector<std::string> rows(numRows);\n int currentRow = 0;\n bool goingDown = false;\n\n // Traverse the input string\n for (char c : s) {\n rows[currentRow] += c;\n\n // Change direction when you reach the top or bottom row\n if (currentRow == 0 || currentRow == numRows - 1) {\n goingDown = !goingDown;\n }\n\n // Move to the next row\n currentRow += goingDown ? 1 : -1;\n }\n\n // Concatenate all rows to get the final string\n std::string result;\n for (const std::string& row : rows) {\n result += row;\n }\n\n return result;\n }\n};", "memory": "12593" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&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> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if (numRows == 1 || s.empty() || numRows >= s.size()){\n return s;\n }\n // consider vector of strings for each row \n vector<string>row(numRows);\n\n int currRow = 0;\n bool goDown = false;\n\n for (int i=0; i<s.size(); i++){\n char c = s[i]; //get the index of the char\n row[currRow] += c; //append the char to the current row \n\n // now check if ur at the bottom or top \n //(this all is happening while the loop iterates)\n \n if (currRow == 0 || currRow == numRows - 1 ){\n goDown = !goDown;\n //thus negating the value of goDown which was a \n // bool value so it reverses\n }\n\n if (!goDown){\n currRow--;\n }else{\n currRow++;\n }\n \n }\n\n string result;\n for (int i=0; i<row.size(); i++){\n result += row[i];\n }\n return result;\n \n }\n};", "memory": "13006" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&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> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n\tstring convert(string s, int numRows) {\n\t\tvector<string> container(numRows);\n\t\tint unit = 2 * numRows - 2;\n\n\t\tif(unit == 0) {\n\t\t\treturn s;\n\t\t}\n\t\tfor (int i = 0; i < s.size(); i++) {\n\t\t\tconst int position = i % unit;\n\t\t\tconst int amount = i / unit;\n\t\t\tconst int i_center = numRows - 1;\n\n\t\t\tint i_char, j_char;\n\n\t\t\tif (position <= numRows - 1) {\n\t\t\t\ti_char = position;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tint distance = position - i_center;\n\t\t\t\ti_char = i_center - distance;\n\t\t\t}\n\t\t\tcontainer[i_char] += s[i];\n\t\t}\n\n\t\tstring result;\n\t\tfor (int i = 0; i < numRows; i++) {\n\t\t\tresult += container[i];\n\t\t}\n return result;\n\n\t}\n\n\n};\n", "memory": "13006" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&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> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n\n // int len = s.length();\n // char ch[100][100];\n // for (int i=0;i<100;i++)\n // {\n // for (int j=0;j<100;j++)\n // {\n // ch[i][j] = ' ';\n // }\n // } \n // int k=0;\n // for (int i=0;i<numRows;i++)\n // ch[k++][0] = s[i];\n\n // int l = 0;\n // int r = numRows-1;\n // for (int i=k;i<s.length();)\n // {\n // if (r==0)\n // {\n // r++;\n // while(r<=2)\n // {\n // ch[r][l+1] = s[l+numRows];\n // r++;\n // l++;\n // i++;\n // }\n // r--;\n // }\n // if (r == numRows-1)\n // {\n // r--;\n // while(r>=0)\n // {\n // ch[r][l+1] = s[l+numRows];\n // r--;\n // l++;\n // i++;\n // }\n // r++;\n // }\n // }\n // string p=\"\";\n // for (int i=0;i<100;i++)\n // {\n // for (int j=0;j<100;j++)\n // {\n // if (ch[i][j]==' ')\n // continue;\n // else\n // p = p + ch[i][j];\n // }\n // }\n // return p;\n\n\n\n\n if (numRows==1)\n return s;\n vector<string>zigzag(numRows);\n int i=0,row = 0;\n bool direction = 1;\n while(true)\n {\n if (direction)\n {\n while (row<numRows && i<s.size()) {\n zigzag[row].push_back(s[i++]);\n row++;\n }\n row = numRows-2; \n }\n else {\n while (row>=0 && i<s.size())\n {\n zigzag[row].push_back(s[i++]);\n row--;\n }\n row=1;\n }\n if (i>=s.size())\n break;\n direction = !direction;\n }\n string ans = \"\";\n for (int i=0;i<zigzag.size();i++)\n {\n ans+=zigzag[i];\n }\n return ans;\n }\n};", "memory": "13418" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&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> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if(numRows==1)\n {\n return s;\n }\n vector<string>zigzag(numRows);\n string ans=\"\";\n int i=0;\n int row=0;\n int dir=1;\n while(true)\n {\n if(dir)\n {\n while(row<numRows && i<s.length())\n {\n zigzag[row++].push_back(s[i++]);\n }\n row=numRows-2;\n dir=0;\n }\n else\n {\n while(row>=0 && i<s.length())\n {\n zigzag[row--].push_back(s[i++]);\n }\n row=1;\n dir=1;\n }\n if(i>=s.length())\n {\n break;\n }\n }\n for(int i=0;i<numRows;i++)\n {\n ans+=zigzag[i];\n }\n return ans;\n }\n};", "memory": "13418" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&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> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if(numRows==1) return s;\n vector<string> rows(min(numRows,int(s.size())));\n int currentRow=0;\n bool goingDown=false;\n\n for(char c:s){\n rows[currentRow]+=c;\n if(currentRow==0 || currentRow==numRows-1) goingDown=!goingDown;\n currentRow+=goingDown ? 1:-1;\n }\n string res;\n for(string row : rows){\n res+=row;\n }\n return res;\n }\n};", "memory": "13831" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&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> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if(numRows == 1) {\n return s;\n }\n \n int x = 0;\n int y_dir = -1;\n\n vector<string> vc(min(numRows, (int)s.size()));\n\n for(const char c: s) {\n vc[x] += c;\n if(x == 0 || x == numRows -1) {\n y_dir *= -1;\n }\n x += y_dir;\n }\n\n string res;\n for(string ss: vc) {\n res += ss;\n }\n return res;\n }\n};", "memory": "13831" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&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> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n vector<string>tmp(numRows,\"\");\n if(numRows==1){\n return s;\n }\n bool inc=true;\n int ind=0;\n for(int i=0;i<s.length();i++){\n if(ind==numRows-1){\n inc=false;\n }else if(ind==0){\n inc=true;\n }\n tmp[ind]=tmp[ind]+s[i];\n if(inc){\n ind++;\n }else{\n ind--;\n }\n }\n string ans;\n for(string str: tmp){\n ans+=str;\n }\n return ans;\n }\n};", "memory": "14243" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&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> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n int rows = 0;\n bool flip = false;\n vector<string> ret(s.length(),\"\");\n\n for(int i = 0; i< s.length(); ++i) {\n ret[rows] += s[i];\n if(flip) {\n rows -= 1;\n if(rows==0) {flip = false;}\n } else {\n rows += 1;\n if(rows == numRows-1) {flip=true;}\n }\n }\n string ans;\n for(const string& it: ret) {\n ans += it;\n }\n return ans;\n }\n};", "memory": "15893" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&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> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n \n \n int n=s.size();\n \n if(numRows==1){\n return s;\n }\n \n// making an array\n // int k=numRows;\n // string arr[numRows]= {\"\"};\n // std::vector<std::string> arr(std::min(numRows, n));\n vector<string>arr(n);\n bool down;\n int row=0;\n \n for(int i=0;i<n;i++){\n \n arr[row].push_back(s[i]);\n \n if(row == numRows-1){\n down=false;\n }\n \n if(row==0){\n down=true;\n }\n \n if(down==true){\n row++;\n }\n else{\n row--;\n }\n \n \n }\n \n// for storing the string \n string res=\"\";\n // for(int i=0;i<n;i++){\n // res+=arr[i];\n // }\n for(string k : arr){\n res+=k;\n }\n return res;\n \n }\n};", "memory": "15893" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&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> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if (numRows == 1 || numRows >= s.length()) {\n return s;\n }\n\n int idx = 0, d = 1;\n vector<vector<char>> rows(numRows);\n\n for (char c : s) {\n rows[idx].push_back(c);\n if (idx == 0) {\n d = 1;\n } else if (idx == numRows - 1) {\n d = -1;\n }\n idx += d;\n }\n\n string result;\n for (const auto& row : rows) {\n for (char c : row) {\n result += c;\n }\n }\n\n return result; \n }\n};", "memory": "16306" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&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> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
2
{ "code": "/*\nOriginal solution. JV.\n*/\n\n#define ONE (1)\n\nclass Solution {\npublic:\n string convert(string s, int rows) {\n vector<vector<char>> auxiliary(rows, vector<char>(0, 0));\n\n int n = s.length();\n\n if(rows == ONE || n < rows){\n return s;\n }\n\n int direction = 1;\n for(int i = 0, j = 0; i < n; i++){\n auxiliary[j].push_back(s[i]);\n\n j += direction;\n\n if(j <= 0 || j >= rows - 1){\n direction = -direction;\n }\n }\n\n s = \"\";\n\n for(int i = 0; i < rows; i++){\n for(int j = 0; j < auxiliary[i].size(); j++){\n s += auxiliary[i][j];\n }\n }\n\n return s;\n }\n};", "memory": "16306" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&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> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if(numRows == 1){\n return s;\n }\n vector<vector<char> > rows(numRows);\n int cur_row = 0;\n int dir = 1;\n for(int i = 0; i < s.size(); i++){\n rows[cur_row].push_back(s[i]);\n if(cur_row == numRows - 1){\n dir = -1;\n } else if(cur_row == 0){\n dir = 1;\n }\n cur_row += dir;\n }\n string ans = \"\";\n for(int i = 0; i < numRows; i++){\n for(int j = 0; j < rows[i].size(); j++){\n ans.push_back(rows[i][j]);\n }\n }\n return ans;\n }\n};", "memory": "16718" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&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> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if (numRows == 1) return s; // No zigzag required for 1 row.\n\n vector<vector<char>> temp(numRows); // Vector of rows to store characters.\n string ans = \"\";\n int r = 0; // Row pointer.\n int i = 0; // String index pointer.\n bool down = true; // Direction flag (true = down, false = up).\n\n while (i < s.size()) {\n temp[r].emplace_back(s[i]);\n i++;\n\n // Update the row pointer based on the direction.\n if (down) {\n r++;\n if (r == numRows) { // When reaching the bottom, change direction.\n r = numRows - 2;\n down = false;\n }\n } else {\n r--;\n if (r < 0) { // When reaching the top, change direction.\n r = 1;\n down = true;\n }\n }\n }\n\n // Combine all characters in the rows into the final string.\n for (int p = 0; p < temp.size(); p++) {\n for (int q = 0; q < temp[p].size(); q++) {\n ans += temp[p][q];\n }\n }\n\n return ans;\n }\n};\n", "memory": "16718" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&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> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if(numRows==1) return s;\n string ans=\"\";\n vector<vector<int>> m(numRows);\n int down=0;\n bool d=true;\n\n for(int i=0;i<s.length();i++){\n if(d){\n m[down].push_back(s[i]);\n down++;\n }\n else{\n m[down].push_back(s[i]);\n down--;\n }\n\n if(down==numRows-1){\n d=false;\n }\n if(down==0){\n d=true;\n \n }\n }\n for(int i=0;i<m.size();i++){\n for(int j=0;j<m[i].size();j++){\n ans+=m[i][j];\n }\n }\n return ans;\n }\n};", "memory": "17131" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&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> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if(numRows==1) return s;\n string ans=\"\";\n vector<vector<int>> m(numRows);\n int down=0;\n bool d=true;\n\n for(int i=0;i<s.length();i++){\n if(d){\n m[down].push_back(s[i]);\n down++;\n }\n else{\n m[down].push_back(s[i]);\n down--;\n }\n\n if(down==numRows-1){\n d=false;\n }\n if(down==0){\n d=true;\n \n }\n }\n for(int i=0;i<m.size();i++){\n for(int j=0;j<m[i].size();j++){\n ans+=m[i][j];\n }\n }\n return ans;\n }\n};", "memory": "17131" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&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> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n vector<string> arr;\n string ret;\n if (numRows == 1) {\n return s;\n }\n for (int i = 0; i < numRows; i++) {\n arr.push_back(string());\n }\n int row = 0;\n for (int i = 0; i < s.size(); i++) {\n if (row < numRows) {\n arr[row].push_back(s[i]);\n row++;\n } else {\n row -= 2;\n while (row >= 1 && i < s.size()) {\n arr[row].push_back(s[i]);\n row--;\n i++;\n }\n i--;\n }\n }\n for (int i = 0; i < arr.size(); i++) {\n ret += arr[i];\n }\n return ret;\n }\n};", "memory": "17543" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&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> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if(numRows==1)\n return s;\n unordered_map<int,string> m;\n int row=0, flag=1;\n \n for(int i=0;i<s.length();i++){\n if(row<0)\n row=1, flag=1;\n else if(row>numRows-1)\n row=numRows-2, flag=0;\n \n m[row]+=s[i];\n \n flag==1?row++:row--;\n \n \n }\n for(int i=1;i<numRows;i++)\n m[0]+=m[i];\n \n return m[0];\n \n }\n};", "memory": "17543" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&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> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if(numRows == 1){\n return s;\n }\n unordered_map<int, string> umap;\n for(int i = 0; i < numRows; i++){\n umap[i] = \"\";\n }\n int sign = 1;\n int index = 0;\n for(int i = 0; i < s.size(); i++){\n umap[index] = umap[index] + s[i];\n index = index + sign;\n if(index == numRows - 1){\n sign = -1;\n } else if(index == 0){\n sign = 1;\n }\n }\n string ans = \"\";\n for(int i = 0; i < numRows; i++){\n ans += umap[i];\n }\n return ans;\n }\n};", "memory": "17956" }