Answer to Question #256251 in Python for rasi
6. Find out the number of palindrome strings in a given string - Link
def count_palindromes[string]:
count = 0
string = string.lower[]
for i in range[len[string] - 1]:
for j in range[i + 2, len[string] + 1]:
substr = string[i:j]
if substr[:len[substr] // 2] == substr[-1:-[len[substr] // 2] - 1:-1]:
count += 1
return count
Learn more about our help with Assignments: Python
Palindrome - 3
This Program name is Palindrome - 2. Write a Python program to Palindrome - 3
The below link contains Palindrome - 3 question, explanation and test cases
//drive.google.com/file/d/1ZELb2KSvV36MM4kiF-dTFTC43CeUiFH9/view?usp=sharing
We need exact output when the code was run
s = input['Enter your words: ']
s = s.lower[]
s2 = s.split[]
s3 = ''.join[s2]
if s3 == s3[::-1]:
print["True"]
else:
print["False"]
Enter your words: No melon no lemon
True
Enter your words: Race Cars
False
Learn more about our help with Assignments: Python
Given a string, the task is to count all palindrome sub string in a given string. Length of palindrome sub string is greater than or equal to 2.
Examples:
Input : str = "abaab" Output: 3 Explanation : All palindrome substring are : "aba" , "aa" , "baab" Input : str = "abbaeae" Output: 4 Explanation : All palindrome substring are : "bb" , "abba" ,"aea","eae"
We have discussed a similar problem below.
Find all distinct palindromic sub-strings of a given string
The above problem can be recursively defined.
Initial Values : i = 0, j = n-1; Given string 'str' CountPS[i, j] // If length of string is 2 then we // check both character are same or not If [j == i+1] return str[i] == str[j] // this condition shows that in recursion if i crosses // j then it will be a invalid substring or // if i==j that means only one character is remaining // and we require substring of length 2 //in both the conditions we need to return 0 Else if[i == j || i > j] return 0; Else If str[i..j] is PALINDROME // increment count by 1 and check for // rest palindromic substring [i, j-1], [i+1, j] // remove common palindrome substring [i+1, j-1] return countPS[i+1, j] + countPS[i, j-1] + 1 - countPS[i+1, j-1]; Else // if NOT PALINDROME // We check for rest palindromic substrings [i, j-1] // and [i+1, j] // remove common palindrome substring [i+1 , j-1] return countPS[i+1, j] + countPS[i, j-1] - countPS[i+1 , j-1];
If we draw recursion tree of above recursive solution, we can observe overlapping Subproblems. Since the problem has overlapping sub-problems, we can solve it efficiently using Dynamic Programming.
Below is a Dynamic Programming based solution.
C++
#include
using
namespace
std;
int
CountPS[
char
str[],
int
n]
{
int
ans=0;
bool
P[n][n];
memset
[P,
false
,
sizeof
[P]];
for
[
int
i = 0; i < n; i++]{
P[i][i] =
true
;
}
for
[
int
gap = 2; gap
Javascript
function
CountPS[str,n]
{
let dp=
new
Array[n];
let P=
new
Array[n];
for
[let i=0;i j]
return
1;
if
[dp[i][j] != -1]
return
dp[i][j];
if
[s[i] != s[j]]
return
dp[i][j] = 0;
return
dp[i][j] = isPal[s, i + 1, j - 1];
}
int
countSubstrings[string s]
{
memset
[dp, -1,
sizeof
[dp]];
int
n = s.length[];
int
count = 0;
for
[
int
i = 0; i < n; i++]
{
for
[
int
j = i + 1; j < n; j++]
{
if
[isPal[s, i, j]]
count++;
}
}
return
count;
}
int
main[]
{
string s =
"abbaeae"
;
cout j]
return
1
;
if
[dp[i][j] != -
1
]
return
dp[i][j];
if
[s.charAt[i] != s.charAt[j]]
return
dp[i][j] =
0
;
return
dp[i][j] = isPal[s, i +
1
, j -
1
];
}
public
static
int
countSubstrings[String s]
{
for
[
int
[] row: dp]
{
Arrays.fill[row, -
1
];
}
int
n = s.length[];
int
count =
0
;
for
[
int
i =
0
; i < n; i++]
{
for
[
int
j = i +
1
; j < n; j++]
{
if
[isPal[s, i, j] !=
0
]
count++;
}
}
return
count;
}
public
static
void
main[String[] args] {
String s =
"abbaeae"
;
System.out.println[countSubstrings[s]];
}
}
Python3
dp
=
[[
-
1
for
i
in
range
[
1001
]]
for
j
in
range
[
1001
]]
def
isPal[s, i, j]:
if
[i > j]:
return
1
if
[dp[i][j] !
=
-
1
]:
return
dp[i][j]
if
[s[i] !
=
s[j]]:
dp[i][j]
=
0
return
dp[i][j]
dp[i][j]
=
isPal[s, i
+
1
, j
-
1
]
return
dp[i][j]
def
countSubstrings[s]:
n
=
len
[s]
count
=
0
for
i
in
range
[n]:
for
j
in
range
[i
+
1
, n]:
if
[isPal[s, i, j]]:
count
+
=
1
return
count
s
=
"abbaeae"
print
[countSubstrings[s]]
C#
using
System;
class
GFG{
static
int
[,] dp =
new
int
[1001, 1001];
static
int
isPal[
string
s,
int
i,
int
j]
{
if
[i > j]
return
1;
if
[dp[i, j] != -1]
return
dp[i, j];
if
[s[i] != s[j]]
return
dp[i, j] = 0;
return
dp[i, j] = isPal[s, i + 1, j - 1];
}
static
int
countSubstrings[
string
s]
{
for
[
int
i = 0; i < 1001; i++]
{
for
[
int
j = 0; j < 1001; j++]
{
dp[i, j] = -1;
}
}
int
n = s.Length;
int
count = 0;
for
[
int
i = 0; i < n; i++]
{
for
[
int
j = i + 1; j < n; j++]
{
if
[isPal[s, i, j] != 0]
count++;
}
}
return
count;
}
static
void
Main[]
{
string
s =
"abbaeae"
;
Console.WriteLine[countSubstrings[s]];
}
}
Javascript
var
dp = Array[1001].fill[].map[[]=>Array[1001].fill[-1]];
function
isPal[ s , i , j]
{
if
[i > j]
return
1;
if
[dp[i][j] != -1]
return
dp[i][j];
if
[s.charAt[i] != s.charAt[j]]
return
dp[i][j] = 0;
return
dp[i][j] = isPal[s, i + 1, j - 1];
}
function
countSubstrings[ s] {
var
n = s.length;
var
count = 0;
for
[i = 0; i < n; i++] {
for
[j = i + 1; j < n; j++] {
if
[isPal[s, i, j] != 0]
count++;
}
}
return
count;
}
var
s =
"abbaeae"
;
document.write[countSubstrings[s]];
Count All Palindrome Sub-Strings in a String | Set 2
This article is contributed by Nishant_Singh[Pintu]. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.