id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n int score = 0;\n\n string score_x( string s, int x ){\n\n string str = \"\";\n stack<char> st;\n\n for( int i=s.size()-1; i>=0; i-- ){\n\n if( !st.empty() && st.top() == 'b' && s[i] == 'a' ){ score += x; st.pop(); continue; }\n st.push(s[i]);\n\n }\n\n int size = st.size();\n\n for( int i=0; i<size; i++ ){ str += st.top(); st.pop(); }\n\n return str;\n\n }\n\n string score_y( string s, int y ){\n\n string str = \"\";\n stack<char> st;\n\n for( int i=s.size()-1; i>=0; i-- ){\n\n if( !st.empty() && st.top() == 'a' && s[i] == 'b' ){ score += y; st.pop(); continue; }\n st.push(s[i]);\n\n }\n\n int size = st.size();\n\n for( int i=0; i<size; i++ ){ str += st.top(); st.pop(); }\n\n return str;\n\n }\n\n int maximumGain(string s, int x, int y) {\n\n if( x > y ){ s = score_x(s,x); s = score_y(s,y); }\n else{ s = score_y(s,y); s = score_x(s,x); }\n\n return score;\n }\n};",
"memory": "34334"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nstring solve(string s,string str){\n stack<char>st;\n for(int i=0;i<s.size();i++){\n if(s[i]==str[1]&&!st.empty()&&st.top()==str[0])\n st.pop();\n else\n st.push(s[i]);\n }\n string a=\"\";\n while(!st.empty()){\n a+=st.top();\n st.pop();\n }\n reverse(a.begin(),a.end());\n return a;\n}\n int maximumGain(string s, int x, int y) {\n string maxstr=(x>y)?\"ab\":\"ba\";\n string minstr=(x<=y)?\"ab\":\"ba\";\n\n string ups=solve(s,maxstr);\n int l=(s.size()-ups.size())/2;\n int ans=l*max(x,y);\n\n string mins=solve(ups,minstr);\n int r=(ups.size()-mins.size())/2;\n ans+=r*min(x,y);\n return ans;\n }\n};",
"memory": "34753"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n string tryBA(string s, int& ans, int val){\n int cnt=0;\n stack<char>st;\n\n for(int i=0;i<s.length();i++)\n {\n if(s[i]=='a')\n {\n if(!st.empty() && st.top()=='b')\n {\n cnt++;\n st.pop();\n } else {\n st.push(s[i]);\n }\n } else {\n st.push(s[i]);\n }\n }\n\n ans+=cnt*val;\n string snew=\"\";\n\n while(!st.empty())\n {\n snew+=st.top();\n st.pop();\n }\n reverse(snew.begin(),snew.end());\n return snew;\n }\n\n string tryAB(string s, int& ans, int val){\n int cnt=0;\n stack<char>st;\n\n for(int i=0;i<s.length();i++)\n {\n if(s[i]=='b')\n {\n if(!st.empty() && st.top()=='a')\n {\n cnt++;\n st.pop();\n } else {\n st.push(s[i]);\n }\n } else {\n st.push(s[i]);\n }\n }\n\n ans+=cnt*val;\n string snew=\"\";\n\n while(!st.empty())\n {\n snew+=st.top();\n st.pop();\n }\n reverse(snew.begin(),snew.end());\n return snew;\n }\npublic:\n int maximumGain(string s, int x, int y) {\n int ans=0;\n if(x>y)\n {\n cout<<\"AB-> \";\n s = tryAB(s,ans,x);\n cout<<s<<\" \"<<ans<<endl;\n tryBA(s,ans,y);\n } else {\n cout<<\"BA-> \";\n s = tryBA(s,ans,y);\n cout<<s<<\" \"<<ans<<endl;\n tryAB(s,ans,x);\n }\n return ans;\n }\n};",
"memory": "34753"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nstring removeBA(string s, int &count)\n{\n stack<char> stacky;\n for (auto x : s)\n {\n if (x == 'a' && !stacky.empty() && stacky.top() == 'b')\n {\n count++;\n stacky.pop();\n }\n else\n {\n stacky.push(x);\n }\n }\n\n string answer = \"\";\n\n while (!stacky.empty())\n {\n answer.push_back(stacky.top());\n stacky.pop();\n }\n\n reverse(answer.begin(), answer.end());\n return answer;\n}\n\nstring removeAB(string s, int &count)\n{\n stack<char> stacky;\n for (auto x : s)\n {\n if (x == 'b' && !stacky.empty() && stacky.top() == 'a')\n {\n count++;\n stacky.pop();\n }\n else\n {\n stacky.push(x);\n }\n }\n\n string answer = \"\";\n\n while (!stacky.empty())\n {\n answer.push_back(stacky.top());\n stacky.pop();\n }\n\n reverse(answer.begin(), answer.end());\n return answer;\n}\n\nint maximumGain(string str, int x, int y)\n{\n if (x > y)\n {\n int countAB = 0;\n string s = removeAB(str, countAB);\n int countBA = 0;\n removeBA(s, countBA);\n return x * countAB + y * countBA;\n }\n\n int countBA = 0;\n string s = removeBA(str, countBA);\n\n // cout << \"count ba ixi \" << countBA << \"\\n\";\n // cout << \"string after removing ixi \" << s << \"\\n\";\n int countAB = 0;\n removeAB(s, countAB);\n // cout << \"count ab ixi \" << countAB << \"\\n\";\n\n return x * countAB + y * countBA;\n}\n\n};",
"memory": "35171"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n string remove_substring(string s,string pair,int point,int &score){\n ios::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n stack<char> st;\n for(int i=0;i<s.length();i++){\n if(s[i]==pair[1]&&!st.empty()&&st.top()==pair[0]){\n score+=point;\n st.pop();\n }\n else{\n st.push(s[i]);\n }\n }\n string ss=\"\";\n while(!st.empty()){\n ss.push_back(st.top());\n st.pop();\n }\n reverse(ss.begin(),ss.end());\n return ss;\n }\npublic:\n int maximumGain(string s, int x, int y) {\n int score=0;\n if(x>y){\n s=remove_substring(s,\"ab\",x,score);\n s=remove_substring(s,\"ba\",y,score);\n }\n else{\n s=remove_substring(s,\"ba\",y,score);\n s=remove_substring(s,\"ab\",x,score);\n }\n return score;\n \n }\n};",
"memory": "35171"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n stack<char>st;\n int ans=0;\n for(int i=0;i<s.length();i++)\n {\n if(!st.empty() && st.top()=='a' && s[i]=='b')\n {\n ans+=x;\n st.pop();\n }\n else\n st.push(s[i]);\n }\n string str=\"\"; \n while(!st.empty())\n {\n str+=st.top();\n st.pop();\n }\n reverse(str.begin(),str.end());\n for(int i=0;i<str.length();i++)\n {\n if(!st.empty() && st.top()=='b'&& str[i]=='a')\n {\n ans+=y;\n st.pop();\n }\n else\n st.push(str[i]);\n }\n\n\n\n stack<char>ste;\n int ans2=0;\n for(int i=0;i<s.length();i++)\n {\n if(!ste.empty() && ste.top()=='b' && s[i]=='a')\n {\n ans2+=y;\n ste.pop();\n }\n else\n ste.push(s[i]);\n }\n string str1=\"\"; \n while(!ste.empty())\n {\n str1+=ste.top();\n ste.pop();\n }\n reverse(str1.begin(),str1.end());\n for(int i=0;i<str1.length();i++)\n {\n if(!ste.empty() && ste.top()=='a'&& str1[i]=='b')\n {\n ans2+=x;\n ste.pop();\n }\n else\n ste.push(str1[i]);\n }\n cout<<ans<<\" \"<<ans2<<endl;\n return max(ans,ans2);\n\n\n\n }\n};",
"memory": "35590"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n stack<char> stk;\n string r;\n int points = 0;\n for (char ch : s) {\n stk.push(ch);\n if (stk.size() > 1) {\n char head = stk.top(); stk.pop();\n char prev = stk.top(); stk.pop();\n if (x >= y) {\n if (prev == 'a' and head == 'b') {\n points += x;\n } else {\n stk.push(prev); stk.push(head);\n }\n } else {\n if (prev == 'b' and head == 'a') {\n points += y;\n } else {\n stk.push(prev); stk.push(head);\n }\n }\n }\n }\n while (!stk.empty()) {\n char ch = stk.top(); stk.pop();\n r.push_back(ch);\n }\n reverse(r.begin(), r.end());\n for (char ch : r) {\n stk.push(ch);\n if (stk.size() > 1) {\n char head = stk.top(); stk.pop();\n char prev = stk.top(); stk.pop();\n if (x >= y) {\n if (prev == 'b' and head == 'a') {\n points += y;\n } else {\n stk.push(prev); stk.push(head);\n }\n } else {\n if (prev == 'a' and head == 'b') {\n points += x;\n } else {\n stk.push(prev); stk.push(head);\n }\n }\n }\n }\n return points;\n }\n};",
"memory": "35590"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n vector<bool> del(s.size(), 0);\n if (x > y) return delAB(s, x, del) + delBA(s, y, del);\n return delBA(s, y, del) + delAB(s, x, del);\n }\n int delAB(string& s, int& x, vector<bool>& del){\n int point = 0;\n stack<int> st;\n for (int i = 0; i < s.size(); i++){\n if (del[i]) continue;\n if (s[i] == 'b'){\n if (!st.empty() && s[st.top()] == 'a'){\n del[st.top()] = del[i] = 1;\n point += x;\n st.pop();\n } \n else st.push(i);\n }\n else st.push(i);\n }\n return point;\n }\n int delBA(string& s, int& y, vector<bool>& del){\n int point = 0;\n stack<int> st;\n for (int i = 0; i < s.size(); i++){\n if (del[i]) continue;\n if (s[i] == 'a'){\n if (!st.empty() && s[st.top()] == 'b'){\n del[st.top()] = del[i] = 1;\n point += y;\n st.pop();\n } \n else st.push(i);\n }\n else st.push(i);\n }\n return point;\n }\n};",
"memory": "36009"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n vector<bool> del(s.size(), 0);\n if (x > y) return delAB(s, x, del) + delBA(s, y, del);\n return delBA(s, y, del) + delAB(s, x, del);\n }\n int delAB(string& s, int& x, vector<bool>& del){\n int point = 0;\n stack<int> st;\n for (int i = 0; i < s.size(); i++){\n if (del[i]) continue;\n if (s[i] == 'b'){\n if (!st.empty() && s[st.top()] == 'a'){\n del[st.top()] = del[i] = 1;\n point += x;\n st.pop();\n } \n else st.push(i);\n }\n else st.push(i);\n }\n return point;\n }\n int delBA(string& s, int& y, vector<bool>& del){\n int point = 0;\n stack<int> st;\n for (int i = 0; i < s.size(); i++){\n if (del[i]) continue;\n if (s[i] == 'a'){\n if (!st.empty() && s[st.top()] == 'b'){\n del[st.top()] = del[i] = 1;\n point += y;\n st.pop();\n } \n else st.push(i);\n }\n else st.push(i);\n }\n return point;\n }\n};",
"memory": "36009"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n int process(string &s, int cost, string &target) {\n stack<int> st;\n int maxi = 0;\n for (int i = 0; i < s.size(); ++i) {\n if (!st.empty() && st.top() == target[0] && s[i] == target[1]) {\n st.pop();\n maxi += cost;\n } else {\n st.push(s[i]);\n }\n }\n s = \"\";\n while (!st.empty()) {\n s += st.top();\n st.pop();\n }\n reverse(s.begin(), s.end());\n return maxi;\n }\n\npublic:\n int maximumGain(string s, int x, int y) {\n string firstTarget = \"ab\", secondTarget = \"ba\";\n if (x < y)swap(firstTarget, secondTarget);\n stack<char> st;\n int maxi = process(s, max(x, y), firstTarget);\n maxi += process(s, min(x, y), secondTarget);\n return maxi;\n }\n};",
"memory": "36428"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n pair<int,string> solve(string s,string target,int c){\n stack<char> st;\n int ans=0;\n for(auto it:s){\n if(!st.empty() && st.top()==target[0] && it==target[1]){\n ans+=c;\n st.pop();\n }\n else st.push(it);\n }\n string temp=\"\";\n while(!st.empty()){\n temp+=st.top();\n st.pop();\n }\n reverse(temp.begin(),temp.end());\n return {ans,temp};\n }\n int maximumGain(string s, int x, int y) {\n string ab=\"ab\",ba=\"ba\";\n if(x<y){\n swap(x,y);\n swap(ab,ba);\n }\n auto it1= solve(s,ab,x);\n auto it2=solve(it1.second,ba,y);\n return it1.first+it2.first;\n }\n};",
"memory": "36846"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n pair<int,string> solve(string s,string target,int c){\n stack<char> st;\n int ans=0;\n for(auto it:s){\n if(!st.empty() && st.top()==target[0] && it==target[1]){\n ans+=c;\n st.pop();\n }\n else st.push(it);\n }\n string temp=\"\";\n while(!st.empty()){\n temp+=st.top();\n st.pop();\n }\n reverse(temp.begin(),temp.end());\n return {ans,temp};\n }\n int maximumGain(string s, int x, int y) {\n string ab=\"ab\",ba=\"ba\";\n if(x<y){\n swap(x,y);\n swap(ab,ba);\n }\n auto it1= solve(s,ab,x);\n auto it2=solve(it1.second,ba,y);\n return it1.first+it2.first;\n }\n};",
"memory": "37265"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n pair<int,string> solve(string s,string target,int c){\n stack<char> st;\n int ans=0;\n for(auto it:s){\n if(!st.empty() && st.top()==target[0] && it==target[1]){\n ans+=c;\n st.pop();\n }\n else st.push(it);\n }\n string temp=\"\";\n while(!st.empty()){\n temp+=st.top();\n st.pop();\n }\n reverse(temp.begin(),temp.end());\n return {ans,temp};\n }\n int maximumGain(string s, int x, int y) {\n string ab=\"ab\",ba=\"ba\";\n if(x<y){\n swap(x,y);\n swap(ab,ba);\n }\n auto it1= solve(s,ab,x);\n auto it2=solve(it1.second,ba,y);\n return it1.first+it2.first;\n }\n};",
"memory": "37684"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n pair<int, string> helper(string s, int val, string req){\n stack<char>st;\n string rem = \"\";\n int count = 0;\n for(int i=0; i<s.size(); i++){\n if(s[i] == req[1]){\n if(!st.empty() && st.top() == req[0]){\n st.pop();\n count += val;\n }else{\n st.push(s[i]);\n }\n }else{\n st.push(s[i]);\n }\n }\n while(!st.empty()){\n rem += st.top();\n st.pop();\n }\n reverse(rem.begin(), rem.end());\n return {count, rem};\n }\n int maximumGain(string s, int x, int y) {\n int ans = 0;\n if(x > y){\n pair<int, string>p = helper(s, x, \"ab\");\n ans += p.first;\n p = helper(p.second, y, \"ba\");\n ans += p.first;\n }else{\n pair<int, string>p = helper(s, y, \"ba\");\n ans += p.first;\n cout << p.first << \" \" << p.second << endl;\n p = helper(p.second, x, \"ab\");\n cout << p.first << \" \" << p.second << endl;\n ans += p.first;\n }\n return ans;\n }\n};",
"memory": "38103"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n //function for counting pairs\n string removevalid(string s,int ch)\n { stringstream ss;\n char b,a;\n b='b'-ch;\n a='a'+ch;\n stack<char>st;\n int n=s.size();\n for(int i=0;i<n;i++)\n {\n if(s[i]==b && !st.empty() && st.top()==a )\n {\n st.pop() ;\n }\n else\n st.push(s[i]);\n }\n \n while(st.size()!=0)\n { \n ss<<st.top();\n st.pop();\n // char i=st.top();\n // v+=i;\n // st.pop();\n }\n string v=ss.str();\n reverse(v.begin(),v.end());\n cout<<v<<\" string aftr removal \\n\";\n return v;\n }\n\npublic:\n int maximumGain(string s, int x, int y) {\n //required variable\n int pr=0,n=0,si=0,count=0;\n si=s.size();\n x>=y?pr=0:pr=1;\n string temp;\n\n //first pass\n temp=removevalid(s,pr);\n n=temp.size();\n if(n==0)\n { si/=2;\n x>=y?count=si*x:count=si*y;\n return count;\n }\n count=(si-n)/2;\n x>=y?count=count*x:count=count*y;\n //Second pass\n pr=!pr;\n temp=removevalid(temp,pr);\n si=temp.size();\n int t;\n t=(n-si)/2;\n x>=y?t*=y:t*=x;\n count+=t;\n return count;\n }\n};",
"memory": "38103"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int recur(string s, int idx, int x, int y) {\n if (idx >= s.length())\n return 0;\n int max1 = 0;\n if (idx + 1 < s.length()) {\n if (s[idx] == 'a' && s[idx + 1] == 'b') {\n max1 = max(max1, x + recur(s, idx + 2, x, y));\n } else if (s[idx] == 'b' && s[idx + 1] == 'a') {\n max1 = max(max1, y + recur(s, idx + 2, x, y));\n }\n // else\n }\n max1 = max(max1, recur(s, idx + 1, x, y));\n ;\n return max1;\n }\n int maximumGain(string s, int x, int y) {\n\n stack<int> st;\n\n st.push(0);\n int i = 1;\n int sum = 0;\n while (i < s.length()) {\n if (!st.empty()) {\n if (x > y) {\n if (s[i] == 'b' && s[st.top()] == 'a') {\n st.pop();\n sum += x;\n } else {\n st.push(i);\n }\n\n } else {\n if (s[i] == 'a' && s[st.top()] == 'b') {\n st.pop();\n sum += y;\n } else {\n st.push(i);\n }\n }\n\n } else\n st.push(i);\n // if(s[i]=='a' )\n\n i++;\n }\n string rem = \"\";\n while (!st.empty()) {\n rem += s[st.top()];\n st.pop();\n }\n reverse(rem.begin(), rem.end());\n i = 0;\n while (i < rem.length()) {\n\n // if(x>y)\n // {\n if (!st.empty()) {\n if (x > y) {\n if (rem[st.top()] == 'b' && rem[i] == 'a') {\n sum += y;\n st.pop();\n i++;\n continue;\n } else {\n st.push(i);\n i++;\n continue;\n }\n } else {\n\n // }\n // else\n // {\n if (rem[st.top()] == 'a' && rem[i] == 'b') {\n sum += x;\n st.pop();\n } else\n st.push(i);\n }\n } else\n st.push(i);\n i++;\n // }\n }\n return sum;\n }\n};",
"memory": "38521"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int update(string &s, int val, int point)\n {\n int points = 0;\n string k = s;\n stack<int>st;\n if(val==0)\n {\n for(int i=0;i<k.length();i++)\n {\n while(!st.empty() && k[st.top()] == 'a' && k[i] == 'b')\n {\n s.erase(st.size()-1,2);\n st.pop();\n i++;\n points += point;\n }\n st.push(i);\n }\n }\n else\n {\n for(int i=0;i<k.length();i++)\n {\n while(!st.empty() && k[st.top()] == 'b' && k[i] == 'a')\n {\n s.erase(st.size()-1,2);\n st.pop();\n i++;\n points += point;\n }\n st.push(i);\n }\n }\n return points;\n }\n int maximumGain(string s, int x, int y) {\n int a;\n if(x>=y)\n {\n a = update(s,0,x) + update(s,1,y);\n }\n else\n {\n a = update(s,1,y) + update(s,0,x);\n }\n return a;\n }\n\n};",
"memory": "38940"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int ans=0;\n\n string solvey(string s,int y)\n {\n stack<char> st;\n string ss;\n \n for(auto k:s)\n {\n if(st.size()==0) \n {\n st.push(k);\n }\n else if(k=='a')\n {\n if(st.size()>0 && st.top()=='b')\n {\n ans+=y;\n st.pop();\n }\n else\n {\n st.push(k);\n }\n }\n else\n {\n st.push(k);\n }\n\n } \n \n while(st.size()>0) \n {\n ss+=st.top();\n st.pop();\n }\n\n reverse(ss.begin(),ss.end());\n return ss;\n }\n\n\n string solvex(string s,int y)\n {\n stack<char> st;\n string ss;\n \n for(auto k:s)\n {\n if(st.size()==0) \n {\n st.push(k);\n }\n else if(k=='b')\n {\n if(st.size()>0 && st.top()=='a')\n {\n ans+=y;\n st.pop();\n }\n else\n {\n st.push(k);\n }\n }\n else\n {\n st.push(k);\n }\n\n } \n \n while(st.size()>0) \n {\n ss+=st.top();\n st.pop();\n }\n\n reverse(ss.begin(),ss.end());\n return ss;\n }\n int maximumGain(string s, int x, int y) {\n \n if(x<y) \n s=solvey(s,y),s=solvex(s,x);\n\n\n s=solvex(s,x),s=solvey(s,y);\n\n \n\n \n \n return ans;\n }\n};",
"memory": "39359"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n\n stack<pair<char,int>> st;\n int sum=0;\n char ch1='a',ch2='b';\n\n if(x<y){\n ch1='b';\n ch2='a';\n int w=x;\n x=y;\n y=w;\n }\n\n for(int j=0;j<s.size();j++){\n if(st.empty()){\n st.push({s[j],j});\n }else{\n if(st.top().first==ch1 && s[j]==ch2){\n sum=sum+x;\n s[j]='#';\n int index=st.top().second;\n s[index]='#';\n st.pop(); \n }\n else{\n st.push({s[j],j});\n }\n }\n }\n\n char w=ch1;\n ch1=ch2;\n ch2=w;\n\n stack<char> st1;\n\n for(int j=0;j<s.size();j++){\n if(s[j]=='#')continue;\n if(st1.empty()){\n st1.push(s[j]);\n }else{\n if(st1.top()==ch1 && s[j]==ch2){\n sum=sum+y;\n st1.pop(); \n }\n else{\n st1.push(s[j]);\n }\n }\n }\n \n\n return sum;\n \n }\n};",
"memory": "40615"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int removeSubstrings(string s, int x, int y) {\n // Function to calculate maximum points\n int maxPoints = 0;\n\n // Helper function to remove a specific substring and calculate points\n auto removeSubstr = [&](string &str, const string &sub, int points) {\n stack<char> st;\n for (char c : str) {\n st.push(c);\n if (st.size() >= 2) {\n char second = st.top(); st.pop();\n char first = st.top(); st.pop();\n if (first == sub[0] && second == sub[1]) {\n maxPoints += points;\n } else {\n st.push(first);\n st.push(second);\n }\n }\n }\n \n string result;\n while (!st.empty()) {\n result.push_back(st.top());\n st.pop();\n }\n reverse(result.begin(), result.end());\n str = result;\n };\n\n // Prioritize based on the points\n if (x >= y) {\n removeSubstr(s, \"ab\", x);\n removeSubstr(s, \"ba\", y);\n } else {\n removeSubstr(s, \"ba\", y);\n removeSubstr(s, \"ab\", x);\n }\n\n return maxPoints;\n}\n\n int maximumGain(string s, int x, int y) {\n return removeSubstrings(s, x, y);\n }\n};",
"memory": "41034"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int score(string& s, char first, char second, int points){\n stack<int> sk;\n int current = 0;\n for(char &c:s){\n if(!sk.empty() && sk.top() == first && c == second){\n current += points;\n sk.pop();\n }\n else{\n sk.push(c);\n }\n }\n string remaining;\n while(!sk.empty()){\n remaining += sk.top();\n sk.pop();\n }\n reverse(remaining.begin(), remaining.end());\n s = remaining;\n return current;\n }\n int maximumGain(string s, int x, int y) {\n int res = 0;\n if (x > y) {\n res += score(s, 'a', 'b', x);\n res += score(s, 'b', 'a', y);\n } else {\n res += score(s, 'b', 'a', y);\n res += score(s, 'a', 'b', x);\n }\n return res;\n }\n};",
"memory": "41453"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int ret;\n\n string reverseStringstr(string str) {\n string ret = \"\";\n int n = str.length();\n for (int i = n - 1; i >= 0; i--) {\n ret += str[i];\n }\n return ret;\n }\n\n string removeABPattern(string str, int val) {\n stack<char> st;\n int n = str.length();\n for (int i = 0; i < n; i++) {\n if (str[i] == 'b' && !st.empty() && st.top() == 'a') {\n ret += val;\n st.pop();\n }\n else {\n st.push(str[i]);\n }\n }\n string temp = \"\";\n while (!st.empty()) {\n temp += st.top();\n st.pop();\n }\n temp = reverseStringstr(temp);\n return temp;\n }\n\n string removeBAPattern(string str, int val) {\n stack<char> st;\n int n = str.length();\n for (int i = 0; i < n; i++) {\n if (str[i] == 'a' && !st.empty() && st.top() == 'b') {\n ret += val;\n st.pop();\n }\n else {\n st.push(str[i]);\n }\n }\n string temp = \"\";\n while (!st.empty()) {\n temp += st.top();\n st.pop();\n }\n temp = reverseStringstr(temp);\n return temp;\n }\n\n int maximumGain(string s, int x, int y) {\n ret = 0;\n if (x > y) {\n string temp = removeABPattern(s, x);\n temp = removeBAPattern(temp, y);\n }\n else {\n string temp = removeBAPattern(s, y);\n temp = removeABPattern(temp, x);\n }\n return ret;\n }\n};",
"memory": "41871"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int solve(string s,string &t)\n {\n int ans=0;\n\n //for \"ab\"\n // f=\"a\"=t[0]\n // s=\"b\"=t[1]\n stack<int> st;\n\n for(int i=0;i<s.size();i++)\n {\n if(s[i]==t[1] && st.size() && st.top()==t[0])\n {\n ans++;\n st.pop();\n }\n else{\n st.push(s[i]);\n }\n }\n\n t.pop_back();\n t.pop_back();\n\n while(st.size())\n {\n t.push_back(st.top());\n st.pop();\n }\n\n return ans;\n }\n\n int maximumGain(string s, int x, int y) {\n int ans=0;\n string temp=\"\";\n\n if(x>y)\n {\n temp=\"ab\";\n ans=solve(s,temp)*x;\n \n reverse(temp.begin(),temp.end());\n s=temp; //solve ne &temp return kelay bg jra\n temp=\"ba\";\n ans+=solve(s,temp)*y;\n }\n else\n {\n temp=\"ba\";\n ans=solve(s,temp)*y;\n \n reverse(temp.begin(),temp.end());\n s=temp; //solve ne &temp return kelay bg jra\n temp=\"ab\";\n ans+=solve(s,temp)*x;\n }\n\n return ans;\n }\n};",
"memory": "42290"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n stack<int>st;\n int ans=0;\n int n=s.size();\n string str=\"\";\n char a='a',b='b';\n if(y>x){\n swap(a,b);\n swap(x,y);\n }\n vector<int>vis(n+1,0);\n for(int i=0;i<n;i++)\n {\n if(s[i]==b){\n if(st.empty())continue;\n if(s[st.top()]==a){\n ans+=x;\n vis[st.top()]=1;\n vis[i]=1;\n st.pop();\n }\n }\n else st.push(i);\n }\n for(int i=0;i<n;i++)\n {\n if(!vis[i])str+=s[i];\n }\n st = stack<int>();\n for(int i=0;i<str.size();i++)\n {\n if(str[i]==a){\n if(st.empty())continue;\n if(str[st.top()]==b){\n ans+=y;st.pop();\n }\n }\n else st.push(i);\n }\n return ans;\n }\n};",
"memory": "42709"
} |
1,818 | <p>You are given a string <code>s</code> and two integers <code>x</code> and <code>y</code>. You can perform two types of operations any number of times.</p>
<ul>
<li>Remove substring <code>"ab"</code> and gain <code>x</code> points.
<ul>
<li>For example, when removing <code>"ab"</code> from <code>"c<u>ab</u>xbae"</code> it becomes <code>"cxbae"</code>.</li>
</ul>
</li>
<li>Remove substring <code>"ba"</code> and gain <code>y</code> points.
<ul>
<li>For example, when removing <code>"ba"</code> from <code>"cabx<u>ba</u>e"</code> it becomes <code>"cabxe"</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the maximum points you can gain after applying the above operations on</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cdbcbbaaabab", x = 4, y = 5
<strong>Output:</strong> 19
<strong>Explanation:</strong>
- Remove the "ba" underlined in "cdbcbbaaa<u>ba</u>b". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaa<u>ab</u>". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcb<u>ba</u>a". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbc<u>ba</u>". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aabbaaxybbaabb", x = 5, y = 4
<strong>Output:</strong> 20
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maximumGain(string s, int x, int y) {\n int topScore=0,botScore=0,maxi=0;\n string top,bot;\n if(x>y){\n top=\"ab\";\n topScore=x;\n bot=\"ba\";\n botScore=y;\n }else{\n top=\"ba\";\n topScore=y;\n bot=\"ab\";\n botScore=x;\n }\n vector<int>st;\n for(int i=0;i<s.size();i++){\n if(!st.empty() && (s[i]==top[1] && st.back()==top[0])){\n maxi+=topScore;\n st.pop_back();\n }else st.push_back(s[i]);\n }\n vector<int>Nst;\n for(int i=0;i<st.size();i++){\n if(!Nst.empty() && (st[i]==bot[1] && Nst.back()==bot[0])){\n maxi+=botScore;\n Nst.pop_back();\n }else Nst.push_back(st[i]);\n }\n return maxi;\n }\n \n};\n",
"memory": "43128"
} |
231 | <p>Given an integer <code>n</code>, return <em><code>true</code> if it is a power of two. Otherwise, return <code>false</code></em>.</p>
<p>An integer <code>n</code> is a power of two, if there exists an integer <code>x</code> such that <code>n == 2<sup>x</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> true
<strong>Explanation: </strong>2<sup>0</sup> = 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 16
<strong>Output:</strong> true
<strong>Explanation: </strong>2<sup>4</sup> = 16
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= n <= 2<sup>31</sup> - 1</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it without loops/recursion? | 0 | {
"code": "class Solution {\npublic:\n bool isPowerOfTwo(int n) {\n if(n<=0){\n return false;\n }\n int m = n-1;\n if((n&m) == 0) return true;\n else return false;\n }\n};",
"memory": "7100"
} |
231 | <p>Given an integer <code>n</code>, return <em><code>true</code> if it is a power of two. Otherwise, return <code>false</code></em>.</p>
<p>An integer <code>n</code> is a power of two, if there exists an integer <code>x</code> such that <code>n == 2<sup>x</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> true
<strong>Explanation: </strong>2<sup>0</sup> = 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 16
<strong>Output:</strong> true
<strong>Explanation: </strong>2<sup>4</sup> = 16
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= n <= 2<sup>31</sup> - 1</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it without loops/recursion? | 0 | {
"code": "class Solution {\npublic:\n bool isPowerOfTwo(int n) {\n return n > 0 && (n & (n - 1)) == 0;\n }\n};",
"memory": "7200"
} |
231 | <p>Given an integer <code>n</code>, return <em><code>true</code> if it is a power of two. Otherwise, return <code>false</code></em>.</p>
<p>An integer <code>n</code> is a power of two, if there exists an integer <code>x</code> such that <code>n == 2<sup>x</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> true
<strong>Explanation: </strong>2<sup>0</sup> = 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 16
<strong>Output:</strong> true
<strong>Explanation: </strong>2<sup>4</sup> = 16
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= n <= 2<sup>31</sup> - 1</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it without loops/recursion? | 0 | {
"code": "class Solution {\npublic:\n bool isPowerOfTwo(int n) {\n int ans=1;\n for(int i=0;i<=30;i++){\n if(ans==n){\n return true;\n }\n if((ans)>=INT_MAX/2){\n break;\n }\n else{\n ans=ans*2;\n }\n }\n return false;\n }\n};",
"memory": "7200"
} |
231 | <p>Given an integer <code>n</code>, return <em><code>true</code> if it is a power of two. Otherwise, return <code>false</code></em>.</p>
<p>An integer <code>n</code> is a power of two, if there exists an integer <code>x</code> such that <code>n == 2<sup>x</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> true
<strong>Explanation: </strong>2<sup>0</sup> = 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 16
<strong>Output:</strong> true
<strong>Explanation: </strong>2<sup>4</sup> = 16
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= n <= 2<sup>31</sup> - 1</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it without loops/recursion? | 0 | {
"code": "// Dhairya Kumar Singh\n\nclass Solution \n{\npublic:\n bool isPowerOfTwo(int n) \n {\n if(n==0) return false;\n while(n%2==0) n=n/=2;\n return n==1?true:false;\n }\n};",
"memory": "7300"
} |
231 | <p>Given an integer <code>n</code>, return <em><code>true</code> if it is a power of two. Otherwise, return <code>false</code></em>.</p>
<p>An integer <code>n</code> is a power of two, if there exists an integer <code>x</code> such that <code>n == 2<sup>x</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> true
<strong>Explanation: </strong>2<sup>0</sup> = 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 16
<strong>Output:</strong> true
<strong>Explanation: </strong>2<sup>4</sup> = 16
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= n <= 2<sup>31</sup> - 1</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it without loops/recursion? | 0 | {
"code": "class Solution {\npublic:\n bool isPowerOfTwo(int n) {\n\n return (n > 0) && ((n & (n - 1)) == 0);\n \n }\n};",
"memory": "7300"
} |
231 | <p>Given an integer <code>n</code>, return <em><code>true</code> if it is a power of two. Otherwise, return <code>false</code></em>.</p>
<p>An integer <code>n</code> is a power of two, if there exists an integer <code>x</code> such that <code>n == 2<sup>x</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> true
<strong>Explanation: </strong>2<sup>0</sup> = 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 16
<strong>Output:</strong> true
<strong>Explanation: </strong>2<sup>4</sup> = 16
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= n <= 2<sup>31</sup> - 1</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it without loops/recursion? | 0 | {
"code": "class Solution {\npublic:\n bool isPowerOfTwo(int n) {\n if(n==0) return false;\n\n while(n>0){\n if(n==1) return true;\n if(n%2!=0) break;\n n/=2;\n }\n return false;\n }\n};",
"memory": "7400"
} |
231 | <p>Given an integer <code>n</code>, return <em><code>true</code> if it is a power of two. Otherwise, return <code>false</code></em>.</p>
<p>An integer <code>n</code> is a power of two, if there exists an integer <code>x</code> such that <code>n == 2<sup>x</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> true
<strong>Explanation: </strong>2<sup>0</sup> = 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 16
<strong>Output:</strong> true
<strong>Explanation: </strong>2<sup>4</sup> = 16
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= n <= 2<sup>31</sup> - 1</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it without loops/recursion? | 0 | {
"code": "class Solution {\npublic:\n bool isPowerOfTwo(int n) {\n for(int i =0; i<31;i++){\n int ans = pow(2,i);\n if(ans == n)return true;\n }\n return false;\n }\n};",
"memory": "7400"
} |
231 | <p>Given an integer <code>n</code>, return <em><code>true</code> if it is a power of two. Otherwise, return <code>false</code></em>.</p>
<p>An integer <code>n</code> is a power of two, if there exists an integer <code>x</code> such that <code>n == 2<sup>x</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> true
<strong>Explanation: </strong>2<sup>0</sup> = 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 16
<strong>Output:</strong> true
<strong>Explanation: </strong>2<sup>4</sup> = 16
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= n <= 2<sup>31</sup> - 1</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it without loops/recursion? | 1 | {
"code": "class Solution {\npublic:\n bool isPowerOfTwo(int n) {\n if(n<=0) return 0;\n else if(n==1) return 1;\n else if(!(n&(n-1))) return 1;\n else return 0;\n }\n};",
"memory": "7500"
} |
231 | <p>Given an integer <code>n</code>, return <em><code>true</code> if it is a power of two. Otherwise, return <code>false</code></em>.</p>
<p>An integer <code>n</code> is a power of two, if there exists an integer <code>x</code> such that <code>n == 2<sup>x</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> true
<strong>Explanation: </strong>2<sup>0</sup> = 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 16
<strong>Output:</strong> true
<strong>Explanation: </strong>2<sup>4</sup> = 16
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= n <= 2<sup>31</sup> - 1</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you solve it without loops/recursion? | 1 | {
"code": "class Solution {\npublic:\n bool isPowerOfTwo(int n) {\n if(n<=0) return 0;\n if(!(n & n-1)) return 1;\n return 0;\n }\n};",
"memory": "7500"
} |
233 | <p>Given an integer <code>n</code>, count <em>the total number of digit </em><code>1</code><em> appearing in all non-negative integers less than or equal to</em> <code>n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 13
<strong>Output:</strong> 6
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 0
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int countDigitOne(int n) {\n int ones = 0;\n for (long long m = 1; m <= n; m *= 10)\n ones += (n/m + 8) / 10 * m + (n/m % 10 == 1) * (n%m + 1);\n return ones;\n }\n};",
"memory": "7100"
} |
233 | <p>Given an integer <code>n</code>, count <em>the total number of digit </em><code>1</code><em> appearing in all non-negative integers less than or equal to</em> <code>n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 13
<strong>Output:</strong> 6
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 0
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution { \npublic:\n int countDigitOne(int n) {\n int ret = 0;\n for(long long int i = 1; i <= n; i*= (long long int)10){\n int a = n / i;\n int b = n % i;\n int x = a % 10;\n if(x ==1){\n ret += (a / 10) * i + (b + 1);\n }\n else if(x == 0){\n ret += (a / 10) * i;\n } else {\n ret += (a / 10 + 1) *i;\n }\n }\n return ret;\n }\n};",
"memory": "7200"
} |
233 | <p>Given an integer <code>n</code>, count <em>the total number of digit </em><code>1</code><em> appearing in all non-negative integers less than or equal to</em> <code>n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 13
<strong>Output:</strong> 6
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 0
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution { \npublic:\n int countDigitOne(int n) {\n int ret = 0;\n for(long long int i = 1; i <= n; i*= (long long int)10){\n int a = n / i;\n int b = n % i;\n int x = a % 10;\n if(x ==1){\n ret += (a / 10) * i + (b + 1);\n }\n else if(x == 0){\n ret += (a / 10) * i;\n } else {\n ret += (a / 10 + 1) *i;\n }\n }\n return ret;\n }\n};",
"memory": "7300"
} |
233 | <p>Given an integer <code>n</code>, count <em>the total number of digit </em><code>1</code><em> appearing in all non-negative integers less than or equal to</em> <code>n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 13
<strong>Output:</strong> 6
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 0
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution { \npublic: \n int countDigitOne(int n) { \n long long count = 0; \n long long factor = 1; // This represents the current digit place (1, 10, 100, ...) \n \n while (factor <= n) { \n long long lowerNumbers = n - (n / factor) * factor; // Numbers lower than the current digit \n long long currentDigit = (n / factor) % 10; // Current digit \n long long higherNumbers = n / (factor * 10); // Numbers higher than the current digit \n \n // Count the occurrences of '1' based on the current digit \n if (currentDigit == 0) { \n count += higherNumbers * factor; \n } else if (currentDigit == 1) { \n count += higherNumbers * factor + lowerNumbers + 1; \n } else { \n count += (higherNumbers + 1) * factor; \n } \n \n factor *= 10; // Move to the next digit place \n } \n \n return count; \n } \n};",
"memory": "7300"
} |
233 | <p>Given an integer <code>n</code>, count <em>the total number of digit </em><code>1</code><em> appearing in all non-negative integers less than or equal to</em> <code>n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 13
<strong>Output:</strong> 6
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 0
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> dp;\n vector<int> count;\n int solve(int n){\n int dig=0;\n int t = n;\n while(t>0){\n dig++;\n t = t/10;\n }\n int ans=0;\n if (n==0) return 0;\n if (dig == 1) return 1;\n ans += dp[dig-1];\n int first = n/pow(10, dig-1);\n ans += (first-1)*dp[dig-1];\n ans += solve(n%(int)pow(10, dig-1));\n if (first == 1){\n ans += n%(int)pow(10, dig-1) + 1;\n }\n else {\n ans += count[dig-1];\n }\n return ans;\n }\n int countDigitOne(int n) {\n int ans=0;\n if (n==0) return 0;\n if (n>=1 && n<=9) return 1;\n dp = vector<int> (11, 0);\n count = vector<int> (11, 0);\n count[1] = 10;\n for(int i=2; i<=9; i++){\n count[i] = pow(10, i);\n }\n dp[1] = 1;\n for(int i=2; i<=9; i++){\n dp[i] += 10*dp[i-1];\n dp[i] += count[i-1];\n }\n dp[10] = dp[9]+1;\n if (n == 1000000000) return dp[10];\n int dig=0;\n int t = n;\n while(t>0){\n dig++;\n t = t/10;\n }\n ans = solve(n);\n\n return ans;\n }\n};",
"memory": "7400"
} |
233 | <p>Given an integer <code>n</code>, count <em>the total number of digit </em><code>1</code><em> appearing in all non-negative integers less than or equal to</em> <code>n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 13
<strong>Output:</strong> 6
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 0
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 10<sup>9</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int countDigitOne(int n) {\n long long count = 0;\n long long factor = 1;\n while (factor <= n) {\n // Calculate the parts\n long long lowerNumbers = n - (n / factor) * factor;\n long long currentDigit = (n / factor) % 10;\n long long higherNumbers = n / (factor * 10);\n \n // Counting digit `1` in the current place\n if (currentDigit == 0) {\n count += higherNumbers * factor;\n } else if (currentDigit == 1) {\n count += higherNumbers * factor + lowerNumbers + 1;\n } else {\n count += (higherNumbers + 1) * factor;\n }\n \n // Move to the next digit place\n factor *= 10;\n }\n return count;\n }\n};\n",
"memory": "7400"
} |
233 | <p>Given an integer <code>n</code>, count <em>the total number of digit </em><code>1</code><em> appearing in all non-negative integers less than or equal to</em> <code>n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 13
<strong>Output:</strong> 6
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 0
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 10<sup>9</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n string convert(int n) {\n string ret = \"\";\n\n while (n > 0) {\n ret = char(48 + n % 10) + ret; \n n /= 10;\n }\n\n if (ret.size() == 0) {\n ret = \"0\";\n }\n\n return ret;\n}\n\nint countDigitOne(int n) {\n long long ans = 0;\n string s = convert(n);\n long long t[11];\n\n t[0] = 1;\n for (int i = 1; i <= 10; i++) {\n t[i] = t[i - 1] * 10;\n }\n\n int cnt = 0;\n for (int i = 0; i < s.size(); i++) {\n int sz = s.size();\n int d = s[i] - 48;\n if (i == s.size() - 1)\n ans += long (cnt * (d + 1) * t[sz - i - 1]);\n else ans += long (cnt * d * t[sz - i - 1]);\n\n if (sz - i - 1 > 0) \n ans += t[sz - i - 2] * (d * (sz - i - 1));\n\n if (s[i] > '1') {\n ans += t[sz - i - 1];\n } else if (s[i] == '1') {\n cnt++;\n }\n }\n\n if (s[s.size() - 1] == '1') {\n ans++;\n }\n\n return ans;\n}\n};",
"memory": "7500"
} |
233 | <p>Given an integer <code>n</code>, count <em>the total number of digit </em><code>1</code><em> appearing in all non-negative integers less than or equal to</em> <code>n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 13
<strong>Output:</strong> 6
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 0
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 10<sup>9</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int countDigitOne(int n) {\n int ans = 0;\n long long p = 1;\n while(p <= n)\n {\n int a = n / p;\n int b = n % p;\n int d = a % 10;\n if(d == 0)\n ans += (a / 10) * p;\n else if(d == 1)\n ans += (a / 10) * p + b + 1;\n else\n ans += (a / 10 + 1) * p;\n p *= 10;\n }\n return ans;\n }\n};",
"memory": "7500"
} |
233 | <p>Given an integer <code>n</code>, count <em>the total number of digit </em><code>1</code><em> appearing in all non-negative integers less than or equal to</em> <code>n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 13
<strong>Output:</strong> 6
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 0
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int solve(string &num, int index, int less, int count, vector<vector<vector<int>>> &dp){\n if(index==num.size()) return count;\n\n if(dp[index][less][count]!=-1) return dp[index][less][count];\n\n int limit=9;\n if(less==0) limit=num[index]-'0';\n\n int res=0;\n for(int digit=0; digit<=limit; digit++){\n if(digit==1){\n res+=solve(num, index+1, less || (digit<limit), count+1, dp);\n }\n else{\n res+=solve(num, index+1, less || (digit<limit), count, dp);\n }\n } \n return dp[index][less][count]=res;\n }\n int countDigitOne(int n) {\n string num=to_string(n);\n vector<vector<vector<int>>> dp(num.size(), vector<vector<int>>(2, vector<int>(num.size(), -1)));\n return solve(num, 0, 0, 0, dp);\n }\n};",
"memory": "7600"
} |
233 | <p>Given an integer <code>n</code>, count <em>the total number of digit </em><code>1</code><em> appearing in all non-negative integers less than or equal to</em> <code>n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 13
<strong>Output:</strong> 6
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 0
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 10<sup>9</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int countDigitOne(int n) {\n int ans = 0;\n if(n <= 0) return 0;\n if(n <= 9) return 1;\n\n unordered_map<int,int> m;\n m[9] = 1;\n\n for(int i = 9 ; i <= (INT_MAX - 9)/10 ; i = i*10 + 9){\n m[i*10+9] = m[i]*10 + (i+1);\n }\n\n int new_n = n , div = 1;\n\n while(new_n/10){\n new_n /= 10;\n div *= 10;\n }\n\n ans += (n/div)*m[div-1];\n ans += (n/div > 1) ? div : 0;\n ans += (n/div == 1) ? n%div + 1 : 0;\n ans += countDigitOne(n%div);\n\n return ans;\n }\n};",
"memory": "7600"
} |
233 | <p>Given an integer <code>n</code>, count <em>the total number of digit </em><code>1</code><em> appearing in all non-negative integers less than or equal to</em> <code>n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 13
<strong>Output:</strong> 6
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 0
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n int factorial(int D) {\n if(D == 0)\n return 1;\n\n int prod1 = 1;\n for(int k=1; k <= D; k++)\n prod1 *= k;\n \n return prod1;\n }\n\n int solve(int D) {\n\n if(D == 1)\n return 1;\n\n int count = 0;\n for(int i=1; i <= D; i++) {\n count += i*(factorial(D)/(factorial(i)*factorial(D-i))*pow(9,D-i));\n }\n\n return count;\n\n }\n\n int countDigitOne(int n) {\n if(n == 0)\n return 0;\n\n if(n <= 9)\n return 1;\n\n int num = n;\n int digits = 0;\n\n vector<int> digs;\n\n while(num > 0) {\n digs.push_back(num%10);\n num /= 10;\n digits++;\n }\n\n reverse(digs.begin(),digs.end());\n vector<int> onesOnRight(digs.size(),0);\n\n int onesCount = 0;\n for(int i=0; i < digs.size(); i++) {\n onesOnRight[i] = onesCount;\n if(digs[i] == 1)\n onesCount++;\n }\n \n int ans = 1; // For single digits\n for(int i=2; i <= digits-1; i++) {\n ans += 9*solve(i-1); // For counting all ones occurign in all place excerp letmost\n ans += 1*pow(10,i-1); // Counting leftmost occurrences\n }\n\n cout << ans << endl;\n // for(auto ka : digs)\n // cout << ka << \" \";\n // // For exactly \n\n int digsOnRight = 1;\n ans += onesOnRight[digs.size()-1]*(digs[digs.size()-1]+1);\n if(digs[digs.size()-1] > 0) {\n ans++;\n }\n cout << ans << endl;\n for(int i=digs.size()-2; i >= 0; i--) {\n int d = digs[i];\n if(d == 0) {\n digsOnRight++;\n continue;\n }\n \n if(i != 0) {\n\n if(d == 1) {\n ans += onesOnRight[i]*(1)*pow(10,digsOnRight) + solve(digsOnRight);\n }\n else {\n ans += onesOnRight[i]*(d-1)*pow(10,digsOnRight) + solve(digsOnRight)*(d-1); // Count for all non ones on curr digits\n }\n if(d > 1)\n ans += (onesOnRight[i]+1)*(pow(10,digsOnRight)) + solve(digsOnRight);\n\n }\n else {\n ans += onesOnRight[i]*(max(0,d-2))*pow(10,digsOnRight) + solve(digsOnRight)*(max(0,d-2));\n\n if(d >= 2)\n ans += (onesOnRight[i]+1)*(pow(10,digsOnRight)) + solve(digsOnRight);\n\n }\n cout << \"Hello \" << ans << endl;\n digsOnRight++;\n }\n\n\n return ans;\n\n }\n};",
"memory": "7700"
} |
233 | <p>Given an integer <code>n</code>, count <em>the total number of digit </em><code>1</code><em> appearing in all non-negative integers less than or equal to</em> <code>n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 13
<strong>Output:</strong> 6
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 0
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n int countDigitOne(int n) {\n int ans = 0;\n if(n == 0) return 0;\n if(n <= 9) return 1;\n unordered_map<int, int> mp;\n mp[9] = 1;\n\n for(int i=9; i<(INT_MAX-9)/10; i=i*10+9) {\n mp[i*10+9] = mp[i]*10 + (i+1);\n }\n\n int temp = n;\n int divisor = 1;\n\n while(temp/10) {\n temp /= 10;\n divisor *= 10;\n }\n int first_digit = n / divisor;\n int rem = n % divisor;\n\n ans += mp[divisor - 1]* first_digit;\n ans += (first_digit > 1) ? divisor : 0;\n ans += (first_digit == 1) ? rem + 1 : 0;\n ans += countDigitOne(rem);\n \n return ans;\n }\n};",
"memory": "7800"
} |
233 | <p>Given an integer <code>n</code>, count <em>the total number of digit </em><code>1</code><em> appearing in all non-negative integers less than or equal to</em> <code>n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 13
<strong>Output:</strong> 6
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 0
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool flag;\n int countDigitOne(int n) {\n int ans = 0;\n if(n == 0) return 0;\n if(n <= 9) return 1;\n flag = true;\n unordered_map<int, int> mp;\n if(flag) {\n mp[9] = 1;\n\n for(int i=9; i<(INT_MAX-9)/10; i=i*10+9) {\n mp[i*10+9] = mp[i]*10 + (i+1);\n }\n flag = false;\n }\n \n int temp = n;\n int divisor = 1;\n\n while(temp/10) {\n temp /= 10;\n divisor *= 10;\n }\n int first_digit = n / divisor;\n int rem = n % divisor;\n\n ans += mp[divisor - 1]* first_digit;\n ans += (first_digit > 1) ? divisor : 0;\n ans += (first_digit == 1) ? rem + 1 : 0;\n ans += countDigitOne(rem);\n \n return ans;\n }\n};",
"memory": "7900"
} |
233 | <p>Given an integer <code>n</code>, count <em>the total number of digit </em><code>1</code><em> appearing in all non-negative integers less than or equal to</em> <code>n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 13
<strong>Output:</strong> 6
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 0
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "vector<int> powers{1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};\nclass Solution {\npublic:\n int recur(int index, bool flag, string &num, vector<vector<int>> &dp) {\n if(index == num.size()) return 0;\n if(dp[index][flag] != -1) return dp[index][flag];\n int answer = 0;\n char limit = num[index];\n if(!flag) limit = '9'+1;\n for(char dig = '0'; dig < limit; dig++) {\n if(dig == '1') answer += powers[num.size() - index - 1];\n answer += recur(index+1, false, num, dp);\n }\n if(flag) {\n if(num[index] == '1') {\n int partNumber = stoi(num) - stoi(num.substr(0, index+1) + string(num.size()-1-index, '0'))+1;\n // cout << index << \" \" << partNumber << endl;\n answer += partNumber;\n }\n answer += recur(index+1, true, num, dp);\n }\n return (dp[index][flag] = answer);\n }\n int countDigitOne(int n) {\n string num = to_string(n);\n vector<vector<int>> dp(num.size(), vector<int> (2, -1));\n\n cout << recur(1, false, num, dp) << endl;\n return recur(0, true, num, dp);\n }\n};",
"memory": "8000"
} |
233 | <p>Given an integer <code>n</code>, count <em>the total number of digit </em><code>1</code><em> appearing in all non-negative integers less than or equal to</em> <code>n</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 13
<strong>Output:</strong> 6
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 0
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "vector<int> powers{1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};\nclass Solution {\npublic:\n int recur(int index, bool flag, string &num, vector<vector<int>> &dp) {\n if(index == num.size()) return 0;\n if(dp[index][flag] != -1) return dp[index][flag];\n int answer = 0;\n char limit = num[index];\n if(!flag) limit = '9'+1;\n for(char dig = '0'; dig < limit; dig++) {\n if(dig == '1') answer += powers[num.size() - index - 1];\n answer += recur(index+1, false, num, dp);\n }\n if(flag) {\n if(num[index] == '1') {\n int partNumber = stoi(num) - stoi(num.substr(0, index+1) + string(num.size()-1-index, '0'))+1;\n // cout << index << \" \" << partNumber << endl;\n answer += partNumber;\n }\n answer += recur(index+1, true, num, dp);\n }\n return (dp[index][flag] = answer);\n }\n int countDigitOne(int n) {\n string num = to_string(n);\n vector<vector<int>> dp(num.size(), vector<int> (2, -1));\n return recur(0, true, num, dp);\n }\n};",
"memory": "8000"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n int init = [] {\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::ofstream out(\"user.out\");\n std::cout.rdbuf(out.rdbuf());\n for (std::string s; getline(std::cin, s);)\n out << (equal(s.begin() + 1, s.begin() + s.size() / 2, s.rbegin() + 1) ? \"true\\n\"\n : \"false\\n\");\n out.flush();\n exit(0);\n return 0;\n}();\nclass Solution {\npublic:\n ListNode* reverseLL(ListNode* head){\n if(head == NULL || head->next == NULL)return head;\n ListNode* NewHead = reverseLL(head->next);\n ListNode* front = head->next;\n front->next = head;\n head->next = NULL;\n\n return NewHead;\n\n }\n bool isPalindrome(ListNode* head) {\n ListNode* slow = head, *fast = head;\n while(fast->next && fast->next->next){\n slow = slow->next;\n fast = fast->next->next;\n }\n ListNode* NewHead = reverseLL(slow->next);\n ListNode* first = head,*second = NewHead;\n\n while(second){\n if(first->val != second->val ){\n reverseLL(NewHead);\n return false;\n }\n first = first->next;\n second = second->next;\n }\n reverseLL(NewHead);\n return true;\n\n }\n};",
"memory": "8895"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n int init = [] {\n ios_base::sync_with_stdio(false); cin.tie(nullptr);\n ofstream out(\"user.out\");\n for (string s; getline(cin, s);)\n out<<(equal(s.begin()+1, s.begin()+s.size()/2, s.rbegin() + 1) ? \"true\\n\" : \"false\\n\");\n out.flush();\n exit(0);\n \n return 0;\n}();\nclass Solution {\npublic:\n ListNode* temp;\n bool check(ListNode* head){\n if(head==NULL) return true;\n bool isPal=check(head->next)&&(temp->val==head->val);\n temp=temp->next;\n return isPal;\n }\n bool isPalindrome(ListNode* head) {\n temp=head;\n return check(head);\n }\n};",
"memory": "8895"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n bool isPalindrome(ListNode* head) {\n if(head ==NULL || head->next == NULL) {\n return true;\n }\n\n ListNode* slow = head;\n ListNode* fast = head;\n ListNode* prev = NULL;\n\n\n while(fast !=NULL && fast->next !=NULL) {\n fast = fast->next->next;\n\n ListNode* temp = slow->next;\n slow->next = prev;\n prev = slow;\n slow = temp;\n }\n\n // If the number of nodes is odd, move slow to the next node\n if (fast){\n slow = slow->next;\n }\n \n while(prev && slow) {\n if(prev->val != slow->val) {\n return false;\n }\n\n prev = prev->next;\n slow = slow->next;\n }\n\n return true;\n }\n};",
"memory": "10485"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n bool isPalindrome(ListNode* head) {\n int n = 0;\n ListNode *curr = head, *prev, *next;\n while(curr){\n curr = curr->next;\n n++;\n }\n if(n == 1) return true;\n prev = nullptr, curr = head, next = head->next;\n for(int i = 1; i <= n/2; i++){\n curr->next = prev;\n prev = curr;\n curr = next;\n next = next->next; \n }\n if(n%2){\n curr = curr->next;\n }\n while(curr){\n if(curr->val != prev->val){\n return false;\n }\n curr = curr->next;\n prev = prev->next;\n }\n return true;\n }\n};",
"memory": "12075"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n bool isPalindrome(ListNode* head) {\n size_t size{1};\n ListNode* ptr = head;\n \n while (ptr->next != nullptr) {\n size++;\n ptr = ptr->next;\n }\n\n if (size == 1) {\n return true;\n }\n\n ptr = head;\n ListNode* head1{head};\n ListNode* head2{head->next};\n head1->next = nullptr;\n for (int i = 1; i < size/2; i++) {\n ptr = head2;\n head2 = head2->next;\n ptr->next = head1;\n head1 = ptr;\n }\n\n if (size%2 != 0) {\n head2 = head2->next;\n }\n for (int i = 1; i <= size/2; i++) {\n if (head1->val != head2->val) {\n return false;\n }\n \n head1 = head1->next;\n head2 = head2->next;\n }\n\n return true;\n }\n};",
"memory": "12075"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n /*\n We avoid using stack because it is memor waist :)\n */\n bool isPalindrome(ListNode* head) {\n if(head == NULL || head->next == NULL)\n return true;\n \n // stack<int> vals;\n ListNode* slow = head, *fast = head, *newHead = NULL;\n // 1 2 3 4\n while(fast && fast->next) {\n // vals.push(slow->val);\n ListNode* tmp = slow;\n\n slow = slow->next;\n fast = fast->next->next;\n\n ListNode *prevNewHead = newHead;\n newHead = tmp;\n newHead->next = prevNewHead;\n }\n // not even number of nodes\n if(fast) {\n slow=slow->next;\n // newHead = newHead->next;\n }\n while(slow && newHead /*&& !vals.empty()*/) {\n if(slow->val != newHead->val /*vals.top()*/) {\n // cout<<slow->val<<\" != \"<<vals.top()<<endl;\n return false;\n }\n // vals.pop();\n slow = slow->next;\n newHead = newHead->next;\n\n }\n return true;\n }\n};",
"memory": "13665"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n bool isPalindrome(ListNode* head) {\n ListNode* tmp=head;\n int l=0;\n\n while(tmp!=NULL){\n tmp=tmp->next;\n l++;\n }\n cout<<\"L:\"<<l<<endl;\n\n if(l==1){\n return true;\n }\n\n int t=l/2;\n cout<<\"T:\"<<t<<endl;\n ListNode* prev=NULL;\n ListNode* curr=head;\n ListNode* tmp2=NULL;\n\n while(t){\n tmp2=curr->next;\n curr->next=prev;\n prev=curr;\n curr=tmp2;\n t--;\n\n }\n cout<<\"P:\"<<prev->val<<endl;\n cout<<\"C:\"<<curr->val<<endl;\n\n if(l%2!=0){\n curr=curr->next;\n }\n\n while(curr!=NULL and prev!=NULL){\n if(curr->val==prev->val){\n curr=curr->next;\n prev=prev->next;\n }else{\n return false;\n }\n }\n return true;\n }\n};",
"memory": "13665"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n bool isPalindrome(ListNode* head) {\n struct ListNode *current = head;\n struct ListNode *prev = NULL;\n struct ListNode *nxt = head;\n struct ListNode* slow = head;\n struct ListNode* fast = head;\n \n while(fast!=NULL && fast->next!= NULL){\n \n fast = fast->next->next;\n nxt = slow->next;\n slow->next = prev;\n prev = slow;\n slow = nxt;\n \n \n }\n if(fast!=NULL){\n slow = slow->next;\n }\n while(prev!=NULL && slow!=NULL){\n if(prev->val!=slow->val){\n return 0;\n }else{\n prev = prev->next;\n slow = slow->next;\n }\n }\n return 1;\n }\n};",
"memory": "15255"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n bool isPalindrome(ListNode* head) {\n if(!head->next) return true;\n int length = 0;\n ListNode* node = head;\n while(node!=nullptr){\n node = node->next;\n length++;\n }\n ListNode* prev = nullptr;\n ListNode* next = nullptr;\n for(int i = 0; i < length / 2; i++){\n next = head->next;\n head->next = prev;\n prev = head;\n head = next;\n }\n\n ListNode* left_head = prev;\n ListNode* right_head;\n if(length%2==0){\n right_head = head;\n }else{\n right_head = head->next;\n }\n\n while(left_head != nullptr && right_head != nullptr){\n if(left_head->val!=right_head->val){\n return false;\n }\n left_head = left_head->next;\n right_head = right_head->next;\n }\n\n return left_head == nullptr && right_head == nullptr;\n\n\n\n\n }\n};",
"memory": "15255"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n bool isPalindrome(ListNode* head) {\n ListNode *slow=head;\n ListNode *fast=head;\n if(head==NULL ||head->next==NULL)return true;//for single node list\n ListNode *prev=NULL,*next=NULL;\n while(fast && fast->next){\n fast=fast->next->next;\n next=slow->next;\n slow->next=prev;\n prev=slow;\n slow=next;\n }\n //if its a odd length list then fast will not be a null value\n if(fast!=NULL)slow=slow->next;\n while(prev!=NULL && slow!=NULL){\n if(slow->val==prev->val){\n slow=slow->next;\n prev=prev->next;\n }else return false;\n }\n return true;\n\n }\n};",
"memory": "16845"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 0 | {
"code": "static const int _ = []() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); return 0; }();\nclass Solution\n{\npublic:\n bool isPalindrome(ListNode *head)\n {\n if(nullptr == head || nullptr == head->next) \n {\n return true;\n }\n\n ListNode *slow = head;\n ListNode *fast = head;\n ListNode *prev = nullptr;\n\n while(nullptr != fast && nullptr != fast->next)\n {\n fast = fast->next->next;\n ListNode *temp = slow->next;\n slow->next = prev;\n prev = slow;\n slow = temp;\n\n }\n\n if(nullptr != fast)\n {\n slow = slow->next;\n }\n\n while(nullptr != slow && nullptr != prev)\n {\n if(slow->val != prev->val) \n {\n return false;\n }\n\n slow = slow->next;\n prev = prev->next;\n }\n\n fast = slow = prev = nullptr;\n return true;\n }\n};",
"memory": "18435"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n bool isPalindrome(ListNode* head) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n\n if(head==NULL || head->next==NULL)\n return true;\n \n ListNode* slow=head;\n ListNode* fast=head;\n while(fast!=NULL && fast->next!=NULL){\n slow=slow->next;\n fast=fast->next->next;\n }\n \n ListNode* mover1=NULL;\n ListNode* mover2=head;\n while(mover2!=slow){\n ListNode* temp=mover1;\n mover1=mover2;\n mover2=mover1->next;\n mover1->next=temp;\n }\n if(fast!=NULL){\n slow=slow->next;\n }\n while(mover1!=NULL && slow!=NULL){\n if(mover1->val!=slow->val)\n return false;\n mover1=mover1->next;\n slow=slow->next;\n }\n return true;\n }\n};",
"memory": "20025"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n bool isPalindrome(ListNode* head){\n ListNode* ptr = head;\n int size = 0;\n while(ptr->next!=NULL){\n ptr = ptr->next;\n size++;\n }\n size++;\n int arr[size];\n ptr = head;\n int i=0;\n while(ptr->next!=NULL){\n arr[i] = ptr->val;\n ptr = ptr->next;\n i++;\n }\n ListNode* prev = nullptr;\n ListNode* current = head;\n ListNode* next = nullptr;\n\n while (current != nullptr) {\n next = current->next; // Store next node\n current->next = prev; // Reverse the current node's pointer\n prev = current; // Move pointers one position ahead\n current = next;\n }\n head = prev; // Update head to new first node\n ptr = head;\n i=0;\n int flag=1;\n while(ptr->next!=NULL){\n if(arr[i]!=ptr->val){\n flag =0;\n }\n ptr = ptr->next;\n i++;\n }\n if(flag==0){\n return false;\n }\n else{\n return true;\n }\n }\n};",
"memory": "21615"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n bool isPalindrome(ListNode* head) {\n if(head-> next == NULL) return true;\n\n int arr1[100001];\n ListNode* curr = head;\n int count = 0;\n while(curr != NULL){\n arr1[count++] = curr-> val;\n curr = curr-> next;\n } \n \n\n // reverse\n curr = head;\n ListNode* prev = NULL;\n while(curr != NULL){\n ListNode* front = curr-> next;\n curr-> next = prev;\n prev = curr;\n curr = front;\n }\n\n // check\n count = 0;\n while(prev != NULL){\n if(arr1[count++] != prev-> val) return false;\n prev = prev-> next;\n } \n return true;\n }\n};",
"memory": "23205"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* fr(ListNode* h){\n ListNode *p=NULL,*q=h,*r;\n while(q){\n r=q->next;\n q->next=p;\n p=q;\n q=r;\n }\n return p;\n }\n bool isPalindrome(ListNode* head) {\n ListNode *s=head,*f=head;\n int y=0;\n ListNode* q=head;\n while(q){\n q=q->next;y++;\n }\n while(f->next!=NULL && f->next->next!=NULL){\n s=s->next;\n f=f->next->next;\n }\n f=s->next;\n q=f;\n s->next=NULL;\n s=head;\n s=fr(s);\n if(y&1){\n s=s->next; \n }\n while(s && f){\n if((s->val)!=(f->val)){\n return 0;\n }\n s=s->next;\n f=f->next;\n }\n s=head;\n s=fr(s);\n while(s->next){\n s=s->next;\n }\n s->next=q;\n return 1;\n }\n};",
"memory": "24795"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* midNode(ListNode* head){\n ListNode*slow = head;\n ListNode*fast = head;\n while(fast != NULL && fast->next != NULL){\n slow = slow->next;\n fast = fast->next->next;\n }\n return slow;\n }\n\n ListNode* revMidNode(ListNode* mid){\n ListNode*prev = NULL;\n ListNode*temp = mid;\n ListNode*front = mid->next;\n while(front != NULL){\n temp->next = prev;\n prev = temp;\n temp = front;\n front = front->next;\n }\n temp->next = prev;\n prev = temp;\n temp = front;\n delete temp;\n delete front;\n return prev;\n \n }\n\n bool isPalindrome(ListNode* head) {\n ListNode* temp = head;\n ListNode* mid = midNode(head); \n ListNode*revHead = revMidNode(mid);\n int cnt = 0; \n while(temp != mid && revHead != NULL){\n if(temp->val != revHead->val){\n cnt++;\n break;\n }\n temp = temp->next;\n revHead = revHead->next;\n }\n revMidNode(temp);\n if(cnt == 1) return false;\n return true;\n } \n\n};",
"memory": "26385"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\nListNode*rev (ListNode*r){\n\nListNode * c=0;\nwhile(r!=0){\n ListNode*temp=r->next;\n r->next=c;\n c=r;\n r=temp;\n}\nListNode*g=c;\nwhile(c!=0){\n cout<<c->val<<\",\";\n c=c->next;\n}\n\nreturn g;\n\n\n}\n\nbool compare(ListNode* r1,ListNode*r2){\n bool f=1;\nwhile(r1!=0&&r2!=0){\n \n if(r1!=0&&r2!=0){ if(r1->val!=r2->val){\n f=0;\n }\n \n }\n r1=r1->next;\n if(r2!=0){\n r2=r2->next;\n }\n}\nreturn f;\n\n\n}\n bool isPalindrome(ListNode* head) {\n if(head==0||head->next==0){\n return 1;\n }\n \n ListNode* slow=head;\n ListNode*fast=head->next;\n ListNode* prev;\n int c=0;\n while(fast!=NULL&&fast->next!=NULL){\n prev=slow;\n c++;\n slow=slow->next;\n fast=fast->next->next;\n }\n\n\n if(fast==0){\n prev->next=0;\n slow=slow->next;\n slow=rev(slow);\n if(compare(slow,head)){\n \n return 1;\n }\n else{\n return 0;\n }\n }\n else{\n prev=slow;\n\n slow=slow->next;\n prev->next=0;\n \n head=rev(head);\n\n\n if(compare(slow,head)){\n \n return 1;\n }\n else{\n return 0;\n }\n \n\n\n }\n\n\n\n\n\n }\n};",
"memory": "27975"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\nListNode*rev (ListNode*r){\n\nListNode * c=0;\nwhile(r!=0){\n ListNode*temp=r->next;\n r->next=c;\n c=r;\n r=temp;\n}\nListNode*g=c;\n\n\nreturn g;\n\n\n}\n\nbool compare(ListNode* r1,ListNode*r2){\n bool f=1;\nwhile(r1!=0&&r2!=0){\n \n if(r1!=0&&r2!=0){ if(r1->val!=r2->val){\n f=0;\n }\n \n }\n r1=r1->next;\n if(r2!=0){\n r2=r2->next;\n }\n}\nreturn f;\n\n\n}\n bool isPalindrome(ListNode* head) {\n if(head==0||head->next==0){\n return 1;\n }\n \n ListNode* slow=head;\n ListNode*fast=head->next;\n ListNode* prev;\n int c=0;\n while(fast!=NULL&&fast->next!=NULL){\n prev=slow;\n c++;\n slow=slow->next;\n fast=fast->next->next;\n }\n\n\n if(fast==0){\n prev->next=0;\n slow=slow->next;\n slow=rev(slow);\n if(compare(slow,head)){\n \n return 1;\n }\n else{\n return 0;\n }\n }\n else{\n prev=slow;\n\n slow=slow->next;\n prev->next=0;\n \n head=rev(head);\n\n\n if(compare(slow,head)){\n \n return 1;\n }\n else{\n return 0;\n }\n \n\n\n }\n\n\n\n\n\n }\n};",
"memory": "27975"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n\n // Function to reverse a linked list and return the new head\n ListNode* reverse(ListNode* head) {\n // Base case: if the list is empty or contains only one node, return the head\n if (head == NULL || head->next == NULL)\n return head;\n\n // Recursively reverse the rest of the list\n ListNode* newHead = reverse(head->next);\n\n // Reverse the pointers between the current node and the next node\n ListNode* front = head->next;\n front->next = head;\n head->next = NULL; // Set the current node's next to NULL (it becomes the new tail)\n \n return newHead; // Return the new head of the reversed list\n }\n\n // Approach 2:\n // This approach takes O(n) time complexity and O(1) space complexity\n bool isPalindrome(ListNode* head) {\n // If the list has only one node, it's trivially a palindrome\n if (head->next == NULL)\n return true;\n\n int cnt = 0; // Counter to count the number of nodes\n ListNode* mover = head; // Temporary pointer to traverse the list\n\n // Traverse the list to count the number of nodes\n while (mover) {\n mover = mover->next;\n cnt++;\n }\n\n ListNode* slow = head; // Slow pointer (moves one step at a time)\n ListNode* fast = head; // Fast pointer (moves two steps at a time)\n ListNode* temp = NULL; // Pointer to store the node just before the middle of the list\n\n // Find the middle of the list using the slow and fast pointer approach\n while (fast && fast->next) {\n temp = slow;\n slow = slow->next;\n fast = fast->next->next;\n }\n\n // If the list has an even number of nodes, fast will stop at the middle node\n if (cnt % 2 == 0)\n fast = slow;\n else\n fast = slow->next; // If the list has an odd number of nodes, skip the middle node\n\n temp->next = NULL; // Split the list into two halves\n\n // Reverse the first half of the list\n slow = reverse(head);\n\n // Compare the two halves of the list\n while (slow && fast) {\n if (slow->val != fast->val) // If the values don't match, it's not a palindrome\n return false;\n slow = slow->next;\n fast = fast->next;\n }\n\n // Check if both halves are exhausted or if only one extra node remains\n if ((slow == NULL && fast == NULL))\n return true; // It's a palindrome\n\n return false; // Not a palindrome\n }\n\n // Approach 1:\n // This solution requires O(n) time complexity and O(n) space.\n /*\n bool isPalindrome(ListNode* head) {\n // Reverse the entire list\n ListNode* head2 = reverse(head);\n\n // Compare the original list with the reversed list\n while (head && head2) {\n if (head->val != head2->val) // If values don't match, it's not a palindrome\n return false;\n head = head->next;\n head2 = head2->next;\n }\n\n return true; // It's a palindrome\n }\n */\n};\n",
"memory": "35925"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 0 | {
"code": "class Solution {\n ListNode* reverse(ListNode* head, ListNode* s){\n if(head==s) return head;\n ListNode* curr=reverse(head->next,s);\n head->next->next=head;\n head->next=NULL;\n return curr;\n }\npublic:\n bool isPalindrome(ListNode* head) {\n if(head->next && !head->next->next){if(head->val==head->next->val) return 1; return 0;}\n int x=0;\n ListNode* s=head;\n ListNode* f=head->next;\n\n while(f && f->next){\n s=s->next;\n f=f->next->next;\n }\n\n if(!f) x=1;\n\n f=s->next;\n s=reverse(head,s);\n if(x) s=s->next;\n\n while(s || f){\n if(!s || !f || s->val!=f->val) return 0;\n s=s->next; f=f->next;\n }\n\n return 1;\n }\n};",
"memory": "37515"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n\n ListNode* reverse(ListNode* head){\n if(head==NULL || head->next==NULL)\n return head;\n \n ListNode* newHead = reverse(head->next);\n ListNode* front = head->next;\n front->next = head;\n head->next = NULL;\n return newHead;\n \n }\n\n//Aporoach 2\n bool isPalindrome(ListNode* head) {\n if(head->next==NULL)\n return true;\n int cnt =0;\n ListNode* mover = head;\n while(mover){\n mover = mover->next;\n cnt++;\n }\n ListNode* slow = head;\n ListNode* fast = head;\n ListNode* temp =NULL;\n while(fast && fast->next){\n temp = slow;\n slow = slow->next;\n fast = fast->next->next;\n }if(cnt%2==0){\n fast = slow;\n temp->next = NULL;\n }\n else {\n fast = slow->next;\n temp->next= NULL;\n }\n slow = reverse(head);\n while(slow&& fast){\n if(slow->val!=fast->val)\n return false;\n slow= slow->next;\n fast = fast->next;\n }\n if((slow==NULL && fast==NULL)||(slow->next==NULL&&fast==NULL)||(fast->next==NULL && slow==NULL))\n return true;\n return false;\n \n \n }\n\n//Approach 1:\n//This solution requires O(n) time complexity and O(n) space.\n // bool isPalindrome(ListNode* head) {\n \n // ListNode* head2 = reverse(head);\n\n // while(head && head2){\n // if(head->val != head2->val)\n // return false;\n // head = head->next;\n // head2 = head2->next;\n // }\n\n // return true;\n \n // }\n};",
"memory": "39105"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\nListNode * reverse(ListNode * head){\n if (head == NULL || head->next == NULL) {\n return head;\n }\n \n ListNode *newhead = reverse(head->next);\n ListNode * front = head->next;\n front->next = head;\n head->next = NULL;\n return newhead;\n}\n bool isPalindrome(ListNode* head) {\n ListNode *slow = head;\n ListNode * fast= head;\n ListNode*temp = head;\n while(fast->next!=NULL&&fast->next->next!=NULL){\n slow = slow->next;\n fast = fast->next->next;\n }\n ListNode * newhead= reverse(slow->next);\n while(newhead!=NULL){\n if(temp->val !=newhead->val){\n reverse(head);\n return false;\n }\n temp = temp->next;\n newhead = newhead->next;\n\n \n }\n return true;\n \n }\n};",
"memory": "40695"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\n public:\n bool isPalindrome(ListNode* head) {\n ListNode* slow = head;\n ListNode* fast = head;\n\n while (fast != nullptr && fast->next != nullptr) {\n slow = slow->next;\n fast = fast->next->next;\n }\n\n if (fast != nullptr)\n slow = slow->next;\n slow = reverseList(slow);\n\n while (slow != nullptr) {\n if (slow->val != head->val)\n return false;\n slow = slow->next;\n head = head->next;\n }\n\n return true;\n }\n\n private:\n ListNode* reverseList(ListNode* head) {\n ListNode* prev = nullptr;\n while (head != nullptr) {\n ListNode* next = head->next;\n head->next = prev;\n prev = head;\n head = next;\n }\n return prev;\n }\n};",
"memory": "42285"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* reverseList(ListNode* head) {\n ListNode* prev = NULL;\n while (head != NULL) {\n ListNode* temp = head->next;\n head->next = prev;\n prev = head;\n head = temp;\n }\n return prev;\n }\n int len(ListNode* head) {\n int cnt = 0;\n while (head != NULL) {\n cnt++;\n head = head->next;\n }\n return cnt;\n }\n bool isPalindrome(ListNode* head) {\n if (head == NULL || head->next == NULL) {\n return head;\n }\n int l = len(head);\n ListNode* slow = head;\n ListNode* fast = head;\n\n while (fast != NULL && fast->next != NULL) {\n slow = slow->next;\n fast = fast->next->next;\n }\n ListNode* secondhalf = reverseList(slow);\n ListNode* firsthalf = head;\n\n while (secondhalf != NULL) {\n if (firsthalf->val != secondhalf->val) {\n return false;\n }\n firsthalf = firsthalf->next;\n secondhalf = secondhalf->next;\n }\n return true;\n }\n};",
"memory": "43875"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n bool isPalindrome(ListNode* head) {\n if (head == nullptr) return true;\n ListNode* slow=head;\n ListNode* fast=head;\n while(fast&&fast->next){\n slow=slow->next;\n fast=fast->next->next;\n }\n ListNode* prev=NULL;\n ListNode* curr=slow;\n while(curr!=NULL){\n ListNode* next_ptr=curr->next;\n curr->next=prev;\n prev=curr;\n curr=next_ptr;\n }\n ListNode* head2=prev;\n while(head2!=nullptr){\n if(head->val!=head2->val){\n return false;\n }\n head=head->next;\n head2=head2->next;\n }\n return true;\n }\n};\n",
"memory": "45465"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\nListNode* mid(ListNode* head)\n{\n ListNode* slow=head;\n ListNode* fast=head;\n while(fast!=NULL && fast->next!=NULL)\n {\n fast=fast->next->next;\n slow=slow->next;\n }\n return slow;\n}\nListNode* reverse(ListNode* head)\n{\n if(head==NULL || head->next==NULL) return head;\n ListNode* curr=head;\n ListNode* prev=NULL;\n ListNode* fwd=NULL;\n while(curr!=NULL)\n {\n fwd=curr->next;\n curr->next=prev;\n prev=curr;\n curr=fwd;\n }\n return prev;\n}\n bool isPalindrome(ListNode* head) {\n if(head->next==NULL || head==NULL) return true;\n ListNode* middle=mid(head);\n ListNode* reverseOfMid=reverse(middle);\n ListNode* head1=head;\n ListNode* head2=reverseOfMid;\n while(head2!=NULL)\n {\n if(head1->val != head2->val) return false;\n head1=head1->next;\n head2=head2->next;\n }\n return true;\n }\n\n};",
"memory": "45465"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 0 | {
"code": "class Solution {\npublic:\n ListNode* reverselist(ListNode* head){\n ListNode* prev,*curr,*Next;\n prev = NULL;\n curr = head;\n Next = head;\n while(curr){\n Next = curr->next;\n curr->next = prev;\n prev = curr;\n curr = Next;\n }\n return prev;\n }\n bool isPalindrome(ListNode* head) {\n ListNode* slow = head;\n ListNode* fast = head;\n while(fast->next != NULL && fast->next->next != NULL){\n slow = slow->next;\n fast = fast->next->next;\n }\n ListNode* NewHead = reverselist(slow->next);\n ListNode* A = head;\n ListNode* B = NewHead;\n while(B){\n if(A->val != B->val) return false;\n A = A->next;\n B = B->next;\n }\n return true;\n }\n};",
"memory": "47055"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* midEle(ListNode*head)\n {\n ListNode*s=head;\n ListNode*f=head;\n while(f->next!=NULL && f->next->next!=NULL)\n {\n s=s->next;\n f=f->next->next;\n }\n return s;\n }\n ListNode* reverse(ListNode* head)\n {\n ListNode*curr=head;\n ListNode*prev=NULL;\n ListNode* next;\n while(curr!=NULL)\n {\n next=curr->next;\n curr->next=prev;\n prev=curr;\n curr=next;\n }\n return prev;\n }\n bool isPalindrome(ListNode* head) {\n ListNode* mid=midEle(head);\n ListNode* nh=reverse(mid->next);\n ListNode*p=head;\n ListNode*q=nh;\n while(q!=NULL)\n {\n if(p->val!=q->val)\n {\n //mid->next=reverse(nh);\n return false;\n }\n p=p->next;\n q=q->next;\n }\n // mid->next=reverse(nh);\n return true;\n\n }\n};",
"memory": "47055"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nListNode *findMid(ListNode *head){\n if(head==NULL) return head;\n\n ListNode *slow=head;\n ListNode *fast=head;\n while(fast!=NULL && fast->next!=NULL){\n fast=fast->next->next;\n slow=slow->next;\n }\n return slow;\n}\nListNode *reverse(ListNode *head){\n if(head==NULL || head->next==NULL) return head;\n ListNode *temp=NULL;\n ListNode *cur=head;\n ListNode *pre=NULL;\n while(cur!=NULL){\n temp=cur->next;\n cur->next=pre;\n pre=cur;\n cur=temp;\n }\n return pre;\n}\nclass Solution {\npublic:\n bool isPalindrome(ListNode* head) {\n ListNode *mid=findMid(head);\n mid=reverse(mid);\n while(mid!=NULL){\n if(head->val!=mid->val){\n return false;\n }\n head=head->next;\n mid=mid->next;\n }\n return true;\n }\n};",
"memory": "48645"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n bool isPalindrome(ListNode* head) {\n if(head == NULL || head->next == NULL)\n return true;//empty list is pallindrome\n ListNode* slow = head;\n ListNode* fast = head;\n //middle node\n while(fast != NULL && fast->next != NULL){\n slow = slow->next;\n fast = fast->next->next;\n }\n \n //reverse the list\n ListNode* secondhalf = reverselist(slow);\n ListNode* firsthalf = head;\n\n //compare two list\n while(secondhalf != NULL){\n if(firsthalf-> val != secondhalf->val){\n return false;\n }\n firsthalf = firsthalf->next;\n secondhalf = secondhalf->next;\n }\n return true;\n\n }\n private:\n ListNode* reverselist(ListNode* head){\n ListNode* prev = NULL;\n ListNode* curr = head;\n while(curr != NULL){\n ListNode* next = curr->next;\n curr->next = prev;\n prev = curr;\n curr = next;\n }\n return prev;\n }\n};",
"memory": "48645"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n bool isPalindrome(ListNode* head) {\n if(head == nullptr || head->next == nullptr)\n return head;\n \n ListNode* slow = head;\n ListNode* fast = head;\n \n while(fast != nullptr && fast->next != nullptr){\n slow = slow->next;\n fast = fast->next->next;\n }\n \n ListNode* secondhalf = slow;\n\n ListNode* prev = nullptr;\n \n while(secondhalf != nullptr){\n ListNode* temp = secondhalf->next;\n secondhalf->next = prev;\n prev = secondhalf;\n secondhalf = temp;\n }\n \n ListNode* orig = head;\n while(prev != nullptr){\n if(prev->val != orig->val)\n return false;\n \n prev = prev->next;\n orig = orig->next;\n }\n \n return true;\n \n }\n};",
"memory": "50235"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* reverseUsingRecursion(ListNode* prev,ListNode* curr){\n if(curr==NULL){\n return prev;\n }\n ListNode* nextNode=curr->next;\n curr->next=prev;\n prev=curr;\n curr=nextNode; \n\n return reverseUsingRecursion(prev,curr);\n }\n\n ListNode* middleNode(ListNode* head){\n ListNode* slow=head;\n ListNode* fast=head;\n while(fast->next!=NULL){\n fast=fast->next;\n if(fast->next!=NULL){\n fast=fast->next;\n slow=slow->next;\n }\n }\n return slow;\n }\n\n bool compare(ListNode* head1, ListNode* head2){\n while(head2!=NULL){\n if(head1->val==head2->val){\n head1=head1->next;\n head2=head2->next;\n }\n else{\n return false;\n }\n }\n return true;\n }\n bool isPalindrome(ListNode* head) {\n ListNode* mid= middleNode(head);\n ListNode* head2=mid->next;\n mid->next=NULL;\n ListNode* prev=NULL;\n ListNode* curr=head2;\n head2=reverseUsingRecursion(prev,curr);\n bool ans=compare(head,head2);\n return ans;\n }\n\n};",
"memory": "50235"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 1 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* middleNode(ListNode* head) {\n ListNode* slow = head;\n ListNode* fast = head;\n\n while(fast->next != NULL) {\n fast = fast->next;\n if(fast->next != NULL) {\n fast = fast->next;\n slow = slow->next;\n }\n }\n return slow;\n }\n\n ListNode* reverseUsingRecursion(ListNode* prev, ListNode* curr) {\n //base case\n if(curr == NULL) {\n return prev;\n }\n //1 case hum solve krenge\n ListNode* nextNode = curr->next;\n curr->next = prev;\n prev = curr;\n curr = nextNode;\n\n //baaaki kon sambhalega - recursion\n return reverseUsingRecursion(prev, curr);\n }\n\n bool compareList(ListNode* head1, ListNode* head2) {\n while(head1 != NULL && head2 != NULL) {\n if(head1->val != head2->val) {\n return false;\n }\n else {\n head1 = head1->next;\n head2 = head2->next;\n }\n }\n return true;\n }\n\n bool isPalindrome(ListNode* head) {\n //break into 2 havles\n ListNode* midNode = middleNode(head);\n ListNode* head2 = midNode -> next;\n midNode -> next = NULL;\n\n //reverse second half\n ListNode* prev = NULL;\n ListNode* curr = head2;\n head2 = reverseUsingRecursion(prev, curr);\n \n //compare both linked list\n bool ans = compareList(head, head2);\n return ans;\n }\n};",
"memory": "51825"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 1 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n bool isPalindrome(ListNode* head) {\n if(head->next==NULL)\n return 1;\n int count=0;\n ListNode* temp2=head;\n while(temp2)\n {\n count++;\n temp2=temp2->next;\n };\n count=count/2;\n ListNode* temp=head;\n ListNode* prev=NULL;\n while(count--)\n {\n prev=temp;\n temp=temp->next;\n \n \n\n }\n prev->next=NULL;\n prev=NULL;\n ListNode* curr=temp;\n ListNode* furt=NULL;\n while(curr)\n {\n furt=curr->next;\n curr->next=prev;\n prev=curr;\n curr=furt;\n }\n\n ListNode* head1=head;\n ListNode* head2=prev;\n while(head1)\n {\n if(head1->val!=head2->val)\n return 0;\n head1=head1->next;\n head2=head2->next;\n }\n return 1;\n }\n};",
"memory": "51825"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 1 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n bool isPalindrome(ListNode* head) {\n ios::sync_with_stdio(NULL);\n cin.tie(NULL);\n if (head == NULL || head->next == NULL) return true;\n\n ListNode *fast = head, *slow = head;\n while(fast != NULL && fast->next != NULL){\n slow = slow->next;\n fast = fast->next->next;\n }\n\n if(fast != NULL){ // means odd no. of nodes\n slow = slow->next;\n }\n\n slow = reverseList(slow);\n fast = head;\n\n while(slow != NULL){\n if(slow->val != fast->val) return false;\n slow = slow->next;\n fast = fast->next;\n }\n return true;\n }\nprivate:\n ListNode* reverseList(ListNode* head){\n ListNode *prev = NULL;\n ListNode *curr = head;\n\n while(curr != NULL){\n ListNode *temp = curr->next;\n curr->next = prev;\n prev = curr;\n curr = temp;\n }\n return prev;\n }\n};",
"memory": "53415"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 1 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n#pragma GCC optimize(\"O3,unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\nstatic const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return true;\n}();\nclass Solution {\npublic:\n bool isPalindrome(ListNode* head) {\n if (!head || !head->next) return true;\n ListNode* slow = head;\n ListNode* fast = head;\n while(fast!= NULL&& fast->next!=NULL){\n slow = slow->next;\n fast = fast->next->next;\n }\n ListNode* current = slow;\n ListNode* prev = NULL;\n \n while(current!=NULL){\n ListNode*next = current->next;\n current->next = prev;\n prev = current;\n current = next;\n }\n ListNode* first = head;\n ListNode* second = prev;\n while(second!=NULL){\n if(first->val!=second->val) return false;\n first = first->next;\n second = second->next;\n \n }\n return true;\n }\n};",
"memory": "55005"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 1 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n bool isPalindrome(ListNode* head) {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n ListNode* slow=head;\n ListNode* fast=head;\n while(fast!=NULL && fast->next!=NULL){\n slow=slow->next;\n fast=fast->next->next;\n }\n ListNode* curr=slow;\n ListNode* prev=NULL;\n while(curr!=NULL){\n ListNode* temp=curr->next;\n curr->next=prev;\n prev=curr;\n curr=temp;\n }\n ListNode* head1=head;\n ListNode* head2=prev;\n while(head2){\n if(head1->val!=head2->val){\n return false;\n }\n head1=head1->next;\n head2=head2->next;\n }\n return true;\n }\n};",
"memory": "56595"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 1 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n bool isPalindrome(ListNode* head) {\n if (!head || !head->next) return true;\n\n // Step 1: Find the middle of the list\n ListNode* slow = head;\n ListNode* fast = head;\n\n while (fast && fast->next) {\n slow = slow->next;\n fast = fast->next->next;\n }\n\n // Step 2: Reverse the second half of the list\n ListNode* secondHalf = reverseList(slow);\n\n // Step 3: Compare the two halves\n ListNode* firstHalf = head;\n ListNode* secondHalfCopy = secondHalf; // Keep a copy for restoring the list later if needed\n\n while (secondHalf) {\n if (firstHalf->val != secondHalf->val) {\n return false;\n }\n firstHalf = firstHalf->next;\n secondHalf = secondHalf->next;\n }\n\n // Optional: Step 4: Restore the list (if needed)\n reverseList(secondHalfCopy);\n\n return true;\n }\n\n\nprivate:\n // Helper function to reverse the linked list\n ListNode* reverseList(ListNode* head) {\n ListNode* prev = NULL;\n ListNode* curr = head;\n while (curr) {\n ListNode* nextTemp = curr->next;\n curr->next = prev;\n prev = curr;\n curr = nextTemp;\n }\n return prev;\n }\n\n \n \n};",
"memory": "58185"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 1 | {
"code": "class Solution {\npublic:\n bool isPalindrome(ListNode* head) {\n if (head == NULL || head->next == NULL) return true;\n \n // Step 1: Find the middle of the linked list using two pointers\n ListNode *slow = head, *fast = head;\n \n while (fast->next != NULL && fast->next->next != NULL) {\n slow = slow->next;\n fast = fast->next->next;\n }\n \n // Step 2: Reverse the second half of the list\n ListNode* secondHalf = reverseList(slow->next);\n ListNode* firstHalf = head;\n \n // Step 3: Compare the first and the second half\n ListNode* secondHalfCopy = secondHalf; // Save the head of reversed part to restore later\n while (secondHalf != NULL) {\n if (firstHalf->val != secondHalf->val) {\n return false; // Not a palindrome\n }\n firstHalf = firstHalf->next;\n secondHalf = secondHalf->next;\n }\n \n // Step 4 (optional): Restore the second half of the list (optional step)\n slow->next = reverseList(secondHalfCopy);\n \n return true;\n }\n \n // Helper function to reverse a linked list\n ListNode* reverseList(ListNode* head) {\n ListNode* prev = NULL;\n while (head != NULL) {\n ListNode* nextNode = head->next;\n head->next = prev;\n prev = head;\n head = nextNode;\n }\n return prev;\n }\n};\n",
"memory": "59775"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 1 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n bool isPalindrome(ListNode* head) {\n if(head==NULL || head->next==NULL) return true;\n\n ListNode *mid=middle(head);\n ListNode* revhead=reverse(mid);\n ListNode *temp=head;\n ListNode *temp2=revhead;\n\n while(temp2!=NULL){\n if(temp->val!=temp2->val){\n return false;\n }\n temp=temp->next;\n temp2=temp2->next;\n }\n reverse(revhead); //this is to restore the reversed ll\n\n return true;\n }\n ListNode* reverse(ListNode* head){\n ListNode* prev=NULL;\n ListNode* curr=head;\n ListNode* front;\n while(curr!=NULL){\n front=curr->next;\n curr->next=prev;\n prev=curr;\n curr=front;\n }\n return prev;\n }\n ListNode *middle(ListNode* head){\n ListNode *slow=head;\n ListNode *fast=head;\n while(fast!=NULL && fast->next!=NULL){\n slow=slow->next;\n fast=fast->next->next;\n }\n return slow;\n }\n};",
"memory": "61365"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 1 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* reverse(ListNode* head) {\n ListNode* prev = nullptr;\n ListNode* curr = head;\n while (curr != nullptr) {\n ListNode* nextNode = curr->next;\n curr->next = prev;\n prev = curr;\n curr = nextNode;\n }\n return prev;\n }\n\n bool isPalindrome(ListNode* head) {\n if (head == nullptr || head->next == nullptr) return true;\n\n // Step 1: Find the middle of the list\n ListNode* slow = head;\n ListNode* fast = head;\n while (fast != nullptr && fast->next != nullptr) {\n slow = slow->next;\n fast = fast->next->next;\n }\n\n // Step 2: Reverse the second half of the list\n ListNode* secondHalf = reverse(slow);\n ListNode* firstHalf = head;\n\n // Step 3: Compare the two halves\n ListNode* secondHalfCopy = secondHalf; // To restore the original list later\n while (secondHalf != nullptr) {\n if (firstHalf->val != secondHalf->val) {\n return false;\n }\n firstHalf = firstHalf->next;\n secondHalf = secondHalf->next;\n }\n\n // Restore the original list (optional)\n reverse(secondHalfCopy);\n\n return true;\n }\n};\n",
"memory": "61365"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 1 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n bool isPalindrome(ListNode* head) {\n if(head==NULL || head->next==NULL) return true;\n\n ListNode *mid=middle(head);\n ListNode* revhead=reverse(mid);\n ListNode *temp=head;\n ListNode *temp2=revhead;\n\n while(temp2!=NULL){\n if(temp->val!=temp2->val){\n return false;\n }\n temp=temp->next;\n temp2=temp2->next;\n }\n reverse(revhead);\n\n return true;\n }\n ListNode* reverse(ListNode* head){\n ListNode* prev=NULL;\n ListNode* curr=head;\n ListNode* front;\n while(curr!=NULL){\n front=curr->next;\n curr->next=prev;\n prev=curr;\n curr=front;\n }\n return prev;\n }\n ListNode *middle(ListNode* head){\n ListNode *slow=head;\n ListNode *fast=head;\n while(fast!=NULL && fast->next!=NULL){\n slow=slow->next;\n fast=fast->next->next;\n }\n return slow;\n }\n};",
"memory": "62955"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 1 | {
"code": "class Solution {\npublic:\n ListNode* reverse(ListNode* head) {\n ListNode* curr = head;\n ListNode* prev = nullptr;\n ListNode* next = nullptr;\n\n while (curr != nullptr) {\n next = curr->next;\n curr->next = prev;\n prev = curr;\n curr = next;\n }\n\n return prev;\n }\n\n ListNode* getMid(ListNode* head) {\n ListNode* slow = head;\n ListNode* fast = head;\n\n while (fast != nullptr && fast->next != nullptr) {\n fast = fast->next->next;\n slow = slow->next;\n }\n\n return slow;\n }\n\n bool isPalindrome(ListNode* head) {\n if (head == nullptr || head->next == nullptr) {\n return true;\n }\n\n ListNode* mid = getMid(head);\n ListNode* secondHalf = reverse(mid);\n ListNode* firstHalf = head;\n ListNode* secondHalfCopy = secondHalf;\n\n while (secondHalf != nullptr) {\n if (firstHalf->val != secondHalf->val) {\n return false;\n }\n firstHalf = firstHalf->next;\n secondHalf = secondHalf->next;\n }\n\n reverse(secondHalfCopy);\n\n return true;\n }\n};\n",
"memory": "62955"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 1 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n void reverse(ListNode* &head, ListNode* &curr, ListNode* &prev){\n if(!curr){\n head=prev;\n return ;\n }\n ListNode* nxt=curr->next;\n reverse(head, nxt, curr);\n curr->next=prev;\n if(nxt){ \n nxt->next=curr;\n // delete nxt;\n }\n return ;\n }\n bool isPalindrome(ListNode* head) {\n if(!head) return false;\n ListNode* s=head, *f=head, *t1=NULL, *t2=NULL;\n while(s && f && f->next) s=s->next, f=f->next->next;\n t1=s, f=NULL;\n // reverse(mid, s, f);\n t2=NULL;\n while(t1) f=t1->next, t1->next=t2, t2=t1, t1=f;\n\n f=head, s=t2;\n while( f && s && f!=s){\n if(f->val!=s->val)return false;\n f=f->next, s=s->next;\n }\n\n t1=NULL, f=NULL;\n // reverse(mid, s,f);\n while(t2) f=t2->next, t2->next=t1, t1=t2, t2=f;\n\n delete f, s, t1, t2;\n return true;\n }\n\n};",
"memory": "64545"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 1 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\n ListNode* reverse(ListNode* head){\n ListNode* curr=head;\n ListNode* prev=nullptr;\n while(curr){\n ListNode* next=curr->next;\n curr->next=prev;\n prev=curr;\n curr=next;\n }\n return prev;\n }\npublic:\n bool isPalindrome(ListNode* head) {\n if(!head->next){return true;}\n ListNode* slow=head;\n ListNode* fast=head;\n while(fast->next && fast->next->next){\n slow=slow->next;\n fast=fast->next->next;\n }\n ListNode* newHead= reverse(slow->next);\n ListNode* first=head;\n ListNode* second=newHead;\n while(second){\n if(first->val!=second->val){\n return false;\n }\n first=first->next;\n second=second->next;\n }\n reverse(newHead);\n return true;\n }\n};",
"memory": "66135"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 1 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n\n ListNode* reverse(ListNode* head)\n {\n ListNode* temp=NULL;\n while(head!=NULL)\n {\n ListNode* next; \n next=head->next;\n head->next=temp;\n temp=head;\n head=next;\n }\n return temp;}\n \n bool isPalindrome(ListNode* head) {\n \n ListNode* slow=head;\n ListNode* fast=head;\n while(fast!=NULL && fast->next!=NULL)\n {\n fast=fast->next->next;\n slow=slow->next;\n }\n if(fast==NULL){\n //revrse from slow\n slow=reverse(slow);\n ListNode* dummy=head;\n while(slow!=NULL)\n {\n if(dummy->val!=slow->val)\n {\n return false;\n }\n slow=slow->next;\n dummy=dummy->next;\n }\n return true;\n }\n else{\n //reverse from slow+1\n slow->next=reverse(slow->next);\n slow=slow->next;\n ListNode* dummy=head;\n while(slow!=NULL)\n {\n if(dummy->val!=slow->val)\n {\n return false;\n }\n slow=slow->next;\n dummy=dummy->next;\n }\n return true;\n }\n \n \n }\n};",
"memory": "66135"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 1 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n // Function to reverse a linked list\n ListNode* reverseList(ListNode* head) {\n ListNode* prev = nullptr;\n ListNode* curr = head;\n ListNode* next = nullptr;\n\n while (curr != nullptr) {\n next = curr->next;\n curr->next = prev;\n prev = curr;\n curr = next;\n }\n\n return prev;\n }\n\n // Function to find the middle node of the linked list\n ListNode* findMiddle(ListNode* head) {\n ListNode* slow = head;\n ListNode* fast = head;\n\n while (fast != nullptr && fast->next != nullptr) {\n slow = slow->next;\n fast = fast->next->next;\n }\n\n return slow;\n }\n\n // Function to check if a linked list is a palindrome\n bool isPalindrome(ListNode* head) {\n ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);\n\n if (head == nullptr || head->next == nullptr) {\n return true;\n }\n\n // Step 1: Find the middle of the linked list\n ListNode* middle = findMiddle(head);\n\n // Step 2: Reverse the second half of the linked list\n ListNode* secondHalf = reverseList(middle);\n\n // Step 3: Compare the first and second halves\n ListNode* firstHalf = head;\n ListNode* secondHalfCopy = secondHalf; // To reverse back later\n\n while (secondHalf != nullptr) {\n if (firstHalf->val != secondHalf->val) {\n return false;\n }\n firstHalf = firstHalf->next;\n secondHalf = secondHalf->next;\n }\n\n // Optional: Reverse the second half back to its original form\n reverseList(secondHalfCopy);\n\n return true;\n }\n};\n",
"memory": "67725"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 1 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* reverseList(ListNode* &head) {\n ListNode*curr=head;\n ListNode*prev=NULL;\n ListNode*temp;\n while(curr!=NULL){\n temp=curr->next;\n curr->next=prev;\n prev=curr;\n curr=temp;\n\n }\n\n return prev;\n }\n\n bool isPalindrome(ListNode* head) {\n ListNode*curr=head;\n ListNode*check=head;\n queue<int>s;\n while(curr->next != NULL){\n s.push(curr->val);\n curr=curr->next;\n \n }\n s.push(curr->val);\n\n\n ListNode*temp=reverseList(check);\n // cout<<\"temp\"<<temp->val<<temp->next->val<<endl;\n // cout<<head->next->val<<endl;\n while(!s.empty()){\n if(s.front()==temp->val){\n cout<<\"temp\"<<temp->val<<s.front()<<endl;\n s.pop();\n temp=temp->next;\n }\n else if(s.front()!=temp->val){\n return false;\n }\n }\n if(s.empty()==1){\n return true;\n }\n return false;\n \n\n \n\n } \n};",
"memory": "69315"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 1 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n string linkedlist_in_string(ListNode* abcd){\n string str=\"\";\n while(abcd!=nullptr){\n str+=abcd->val;\n abcd=abcd->next;\n }\n return str;\n }\n \n ListNode *reversedlinked(ListNode *head){\n ListNode *prev=nullptr,*next;\n ListNode*curr=head;\n\n while(curr!=nullptr){\n next=curr->next;\n curr->next=prev;\n prev=curr;\n curr=next;\n }\n return prev; \n }\n\n bool isPalindrome(ListNode* head) {\n string str1= linkedlist_in_string (head);\n ListNode*prev = reversedlinked(head);\n string str2= linkedlist_in_string(prev);\n if (str1==str2) return true;\n else return false;\n\n }\n};",
"memory": "70905"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 1 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n string linkedlist_in_string(ListNode* abcd){\n string str=\"\";\n while(abcd!=nullptr){\n str+=abcd->val;\n abcd=abcd->next;\n }\n return str;\n }\n \n ListNode *reversedlinked(ListNode *head){\n ListNode *prev=nullptr,*next;\n ListNode*curr=head;\n\n while(curr!=nullptr){\n next=curr->next;\n curr->next=prev;\n prev=curr;\n curr=next;\n }\n return prev; \n }\n\n bool isPalindrome(ListNode* head) {\n string str1= linkedlist_in_string (head);\n ListNode*prev = reversedlinked(head);\n string str2= linkedlist_in_string(prev);\n if (str1==str2) return true;\n else return false;\n\n }\n};",
"memory": "72495"
} |
234 | <p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg" style="width: 422px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2,2,1]
<strong>Output:</strong> true
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg" style="width: 182px; height: 62px;" />
<pre>
<strong>Input:</strong> head = [1,2]
<strong>Output:</strong> false
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 9</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Could you do it in <code>O(n)</code> time and <code>O(1)</code> space? | 1 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n string linkedlist_in_string(ListNode* abcd){\n string str=\"\";\n while(abcd!=nullptr){\n str+=abcd->val;\n abcd=abcd->next;\n }\n return str;\n }\n \n ListNode *reversedlinked(ListNode *head){\n ListNode *prev=nullptr,*next;\n ListNode*curr=head;\n\n while(curr!=nullptr){\n next=curr->next;\n curr->next=prev;\n prev=curr;\n curr=next;\n }\n return prev; \n }\n\n bool isPalindrome(ListNode* head) {\n string str1= linkedlist_in_string (head);\n ListNode*prev = reversedlinked(head);\n string str2= linkedlist_in_string(prev);\n if (str1==str2) return true;\n else return false;\n\n }\n};",
"memory": "72495"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.