-
Notifications
You must be signed in to change notification settings - Fork 618
/
palindrome_checker.java
200 lines (150 loc) · 4.85 KB
/
palindrome_checker.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
ALGORITHM
Step 1: Create a doubly linked list where each node contains only one
character of a string.
Step 2: Initialize two pointers left at starting of list and right at
the end of the list.
Step 3: Check the data at left node is equal to right node, if it is equal
then increment left and decrement right till middle of the list
Step 4: If at any stage it is not equal then return false.
Step 5: If returned boolean is true, then the given string is palindrome
Step 6: Else the inputted string is not palindrome
*/
package Java;
import java.util.*;
public class PalindromeChecker
{
static String inputWord;
static String inputWords;
static int length;
static boolean flag;
static int choice;
// Creating class to represent each node of a doubly linked list
static class Node
{
char value;
Node next;
Node previous;
};
// Reference pointer
static Node headNode = new Node();
// Funtion to insert each character of the string into each node of doubly Linked list
public static void insert(char v)
{
// Creating a new node of doubly linked list
Node newNode = new Node();
Node temp = new Node();
// if the doubly linked list is empty
if (headNode == null) {
newNode.next = null;
newNode.value = v;
headNode = newNode;
newNode.previous = null;
}
// each node contains only one character of a string.
else {
temp = headNode;
// Traversing till the last node of the doubly linked list
while (temp.next != null)
{
temp = temp.next;
}
// Inserting new node at the end of the doubly linked list
newNode.value = v;
temp.next = newNode;
newNode.previous = temp;
newNode.next = null;
}
}
// Function to check if list is palindrome or not
public static boolean reverse()
{
Node left = new Node();
Node right = new Node();
// Initialize pointer left to starting of list
left = headNode;
right = headNode;
if (headNode == null)
{
return true;
}
// Traversing till the end of the list is reached
while (right.next != null)
{
// Initialize pointer right to end of list
right = right.next;
}
// Check the data at left node is equal to right node,
// if it is equal then increment left and decrement right till middle of the list
// If at any stage it is not equal then return false.
while (left != right && right != left.previous)
{
if (left.value != right.value)
return false;
left = left.next;
right = right.previous;
}
return true;
}
public static void main(String[] args)
{
// Setting headNode (pointer to pointer) to null
headNode = null;
// Creating Scanner Object to read input from user
Scanner s = new Scanner(System.in);
System.out.println("Enter the string");
// Reading the string from the user
inputWords = s.next();
//Converting the string to lowercase if there mixed Casing is present
inputWord = inputWords.toLowerCase();
//Calculating the length of the string
length = inputWord.length();
// Converting the given string into array of characters
char[] inputWordArray = inputWord.toCharArray();
for (int i = 0; i < length; i++)
{
// Funtion to insert each character of the string into each node of doubly
// Linked list
insert(inputWordArray[i]);
}
// Function call to check whether the given string is palindrome
flag = reverse();
if (flag == false)
{
System.out.printf("%s is not palindrome", inputWords);
}
else
{
System.out.printf("%s is palindrome", inputWords);
}
// Printing new Line
System.out.println();
// Closing the scanner Object
s.close();
}
}
/*
* TEST CASES
*
* Enter the string
* Malayalam
* Malayalam is palindrome
*
*
* Enter the string
* Morning
* Morning is not palindrome
*
* Enter the string
* rotor
* rotor is palindrome
*
*/
/*
* COMPLEXITY
*
* Time complexity : O(n)
* Space complexity : O(1)
*
*
*/