forked from kdn251/interviews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PeskyPalindromes.java
124 lines (73 loc) · 3.14 KB
/
PeskyPalindromes.java
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// A palindrome is a sequence of one or more characters that reads the same from the left as it does from
// the right. For example, Z, TOT and MADAM are palindromes, but ADAM is not.
// Your job, should you choose to accept it, is to write a program that reads a sequence of strings and
// for each string determines the number of UNIQUE palindromes that are substrings.
// Input
// The input file consists of a number of strings (one per line), of at most 80 characters each, starting in
// column 1.
// Output
// For each non-empty input line, the output consists of one line containing the message:
// The string 'input string' contains nnnn palindromes.
// where input string is replaced by the actual input string and nnnn is replaced by the number of
// UNIQUE palindromes that are substrings.
// Note:
// See below the explanation of the sample below
// • The 3 unique palindromes in ‘boy’ are ‘b’, ‘o’ and ‘y’.
// • The 4 unique palindromes in ‘adam’ are ‘a’, ‘d’, ‘m’, and ‘ada’.
// • The 5 unique palindromes in ‘madam’ are ‘m’, ‘a’, ‘d’, ‘ada’, and ‘madam’.
// • The 3 unique palindromes in ‘tot’ are ‘t’, ‘o’ and ‘tot’.
// Sample input
// boy
// adam
// madam
// tot
// Sample output
// The string 'boy' contains 3 palindromes.
// The string 'adam' contains 4 palindromes.
// The string 'madam' contains 5 palindromes.
// The string 'tot' contains 3 palindromes.
import java.util.*;
public class PeskyPalindromes {
public static void main(String args[]) {
int x;
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
String currentString = sc.next();
List<String> allSubstrings = generateSubstrings(currentString);
int uniquePalindromes = findUniquePalindromes(allSubstrings);
System.out.println("The string " + "'" + currentString + "'" + " contains " + uniquePalindromes + " palindromes.");
}
}
public static List<String> generateSubstrings(String s) {
List<String> allSubstrings = new ArrayList<String>();
for(int i = 0; i < s.length(); i++) {
for(int j = i + 1; j <= s.length(); j++) {
String currentSubstring = s.substring(i, j);
if(!allSubstrings.contains(currentSubstring)) {
allSubstrings.add(currentSubstring);
}
}
}
return allSubstrings;
}
public static int findUniquePalindromes(List<String> allSubstrings) {
int totalUniquePalindromes = 0;
for(String s : allSubstrings) {
int left = 0;
int right = s.length() - 1;
boolean isPalindrome = true;
while(left < right) {
if(s.charAt(left) != s.charAt(right)) {
isPalindrome = false;
break;
}
left++;
right--;
}
if(isPalindrome) {
totalUniquePalindromes++;
}
}
return totalUniquePalindromes;
}
}