Skip to content

Commit 5040eac

Browse files
committed
Runtime: 6 ms (Top 43.33%) | Memory: 7.90 MB (Top 47.14%)
1 parent 506c1ca commit 5040eac

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
1+
// Runtime: 6 ms (Top 43.33%) | Memory: 7.90 MB (Top 47.14%)
2+
13
class Solution {
24
public:
35
int longestDecomposition(string text) {
4-
if(text.size() == 0)
5-
return 0;
6-
int i = 0;
7-
deque<char> sFront;
8-
deque<char> sBack;
9-
while(i < text.size() / 2){
10-
sFront.push_back(text[i]);
11-
sBack.push_front(text[text.size() - 1 - i]);
12-
if(sFront == sBack)
13-
return 2 + longestDecomposition(text.substr(i + 1, text.size() - 2*(i+1)));
6+
int ans=0,i=0,j=text.size()-1;
7+
string s="",t="";
8+
9+
while(i<j){
10+
s+=text[i];
11+
t+=text[j];
12+
string rev=t;
13+
reverse(rev.begin(),rev.end());
14+
15+
if(s==rev){
16+
ans++;
17+
t="";
18+
s="";
19+
}
20+
1421
i++;
22+
j--;
1523
}
16-
return 1;
24+
ans*=2;
25+
26+
if(i==j || s.size()!=0)ans++;
27+
return ans;
1728
}
18-
};
29+
};

0 commit comments

Comments
 (0)