-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIterativePalindrome.java
More file actions
44 lines (35 loc) · 1.36 KB
/
IterativePalindrome.java
File metadata and controls
44 lines (35 loc) · 1.36 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
// Christopher Yonek
// Compute Palindrome (Iteration)
public class IterativePalindrome {
/*Function that returns true if
str is a palindrome
*/
static boolean isPalindrome(String str) {
// Pointers pointing to the beginning
// and the end of the string
int i = 0, numChars = str.length() - 1;
// While there are characters toc compare
while (i < numChars) {
// If there is a mismatch
if (str.charAt(i) != str.charAt(numChars))
return false;
// Increment first pointer and
// decrement the other
i++;
numChars--;
}
// Given string is a palindrome
return true;
}
public static void main(String[] args) {
String[] palinList = {"I","kk","wow","anna","madam","poppop",
"racecar", "snellens","evitative","jjiijjiijj","aibohphobia",
"tattarrattat","lolololololol","ccddccddccddcc","yoyoyoyoyoyoyoy",
"xxxxxxxxxxxxxxxx","vevevevevevevevev","yyyyyyyyyyyyyyyyyy",
"zzzzzzzzzzzzzzzzzzz"};
long startTime = System.nanoTime();
System.out.print(isPalindrome(palinList[18]) + " ");
long elapsedTime = System.nanoTime() - startTime;
System.out.println(elapsedTime);
}
}