-
Notifications
You must be signed in to change notification settings - Fork 148
/
Expression_PalindromeReplace.cpp
47 lines (41 loc) · 1.14 KB
/
Expression_PalindromeReplace.cpp
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
38
39
40
41
42
43
44
45
46
47
/Two pointers approach
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
cin>>s;
int i=0,j=s.size()-1;
while(i<=j)
{
//Case 1: If s[i] ans s[j] both are '?'
if(s[i]=='?' && s[j]=='?')
{
s[i]='a';
s[j]='a';
}
//Case 2: If s[i] or s[j] one of them is '?'
else if(s[i]=='?' || s[j]=='?')
{
if(s[i]=='?') s[i]=s[j];
else s[j]=s[i];
}
//Case 3: If s[i] and s[j] both are same
else if(s[i]==s[j])
{
i++;
j--;
continue;
}
//Case 4: If s[i] and s[j] both are different alphabets
else
{
cout<<-1;
return 0;
}
i++;
j--;
}
cout<<s;
return 0;
}