-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc30.c
More file actions
50 lines (44 loc) · 977 Bytes
/
c30.c
File metadata and controls
50 lines (44 loc) · 977 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
38
39
40
41
42
43
44
45
46
47
48
49
50
// Palindrome after removing at most one characterValid
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
bool isPalindrome(char s[], int left, int right)
{
while (left < right)
{
if (s[left] != s[right])
return false;
left++;
right--;
}
return true;
}
bool validPalindrome(char s[])
{
int left = 0;
int right = strlen(s) - 1;
while (left < right) {
if (s[left] == s[right])
{
left++;
right--;
}
else
{
// Try skipping one character from either side
return isPalindrome(s, left + 1, right) || isPalindrome(s, left, right - 1);
}
}
return true;
}
int main()
{
char s[100];
printf("Enter string: ");
scanf("%s", s);
if (validPalindrome(s))
printf("Valid palindrome after at most one removal\n");
else
printf("Not a valid palindrome\n");
return 0;
}