Skip to content

Commit aedac27

Browse files
authored
Create split-two-strings-to-make-palindrome.cpp
1 parent e18b9df commit aedac27

File tree

1 file changed

+29
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)