Skip to content

Commit 1ea2ba3

Browse files
authored
Create 1616.Split-Two-Strings-to-Make-Palindrome.cpp
1 parent 68c850a commit 1ea2ba3

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
bool checkPalindromeFormation(string a, string b)
4+
{
5+
return check(a,b) || check(b,a);
6+
}
7+
8+
bool check(string&a, string& b)
9+
{
10+
int i=0, j=b.size()-1;
11+
while (i<j && a[i]==b[j])
12+
{
13+
i++;
14+
j--;
15+
}
16+
if (i>=j) return true;
17+
return isPalindrome(a.substr(i,j-i+1))||isPalindrome(b.substr(i,j-i+1));
18+
}
19+
20+
bool isPalindrome(string s)
21+
{
22+
int i=0, j = s.size()-1;
23+
while (i<j && s[i]==s[j])
24+
{
25+
i++;
26+
j--;
27+
}
28+
return i>=j;
29+
}
30+
};

0 commit comments

Comments
 (0)