-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindrome.java
More file actions
62 lines (52 loc) · 1.18 KB
/
Copy pathpalindrome.java
File metadata and controls
62 lines (52 loc) · 1.18 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
import java.util.Scanner;
public class Solution {
public static void main(String [] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("enter String");
String s=sc.next();
int count=0;
int start=0;
int end = s.length()-1;
for(int i=0;i<s.length();i++)
{
//For odd
int r=1;
count++;
System.out.println(s.charAt(i));
// for odd
while(true)
{
int startIndex=i-r;
int endIndex=i+r;
if(startIndex>=start && endIndex<=end)
{
r++;
if(s.charAt(startIndex)==s.charAt(endIndex))
{
System.out.println(s.substring(startIndex,endIndex+1));
count++;
}else break;
}
else break;
}
r=1;
//for even
while(true)
{
int startIndex=i-r+1;
int endIndex=i+r;
if(startIndex>=start && endIndex<=end)
{
r++;
if(s.charAt(startIndex)==s.charAt(endIndex))
{
System.out.println(s.substring(startIndex,endIndex+1));
count++;
}else break;
} else break;
}
}
System.out.println(" No of Palindromes "+count);
}
}