-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathcode_1.cpp
36 lines (31 loc) · 917 Bytes
/
code_1.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
//
// code_1.cpp
// Algorithm
//
// Created by Mohd Shoaib Rayeen on 23/11/18.
// Copyright © 2018 Shoaib Rayeen. All rights reserved.
//
#include <iostream>
#include <string>
using namespace std;
int getLengthOfMinInsertions(string str, int l, int h) {
if (l > h) {
return INT_MAX;
}
if (l == h) {
return 0;
}
if (l == h - 1) {
return (str[l] == str[h])? 0 : 1;
}
return (str[l] == str[h])? getLengthOfMinInsertions(str, l + 1, h - 1): ( min (
getLengthOfMinInsertions(str, l, h - 1), getLengthOfMinInsertions(str, l + 1, h)
) + 1);
}
int main() {
string str;
cout << "\nEnter String\t:\t";
cin >> str;
cout << "\nMinimum Insertions to form Palindrome\t:\t" << getLengthOfMinInsertions(str, 0, int(str.length())-1) << "\n";
return 0;
}