-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanpalindrome.cpp
More file actions
37 lines (30 loc) · 827 Bytes
/
Copy pathcanpalindrome.cpp
File metadata and controls
37 lines (30 loc) · 827 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
using iter = string::iterator;
int maxd(0);
bool canpal(iter i, iter j, int d){
string si{i,next(j)};
//cout << "got s " << si << ' ' << d << '\n';
if (d > maxd) return false;
iter is(i), js(j);
while(i < j){
if (*i != *j) return canpal(i+1,j,d+1) || canpal(i,j-1,d+1);
++i;--j;
}
string s{is,next(js)};
cout << "true " << s << '\n';
return true;
}
// To execute C++, please define "int main()"
int main() {
string s("waterrfetawx");
maxd = 2;
bool b = canpal(s.begin(),prev(s.end()),0);
cout << "ret " << b << endl;
return 0;
}
/*
Given a string which we can delete at most k, return whether you can make a palindrome.
For example, given 'waterrfetawx' and a k of 2, you could delete f and x to get 'waterretaw'.
*/