-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindPalindromesNotLCProblem.java
More file actions
60 lines (51 loc) · 1.63 KB
/
findPalindromesNotLCProblem.java
File metadata and controls
60 lines (51 loc) · 1.63 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
public class findPalindromesNotLCProblem {
public static void main(String[] args)
{
System.out.println(palindromeSolver2("eye"));
System.out.println(palindromeSolver2(""));
System.out.println(palindromeSolver2("hi"));
System.out.println(palindromeSolver2("i"));
System.out.println(palindromeSolver2("do geese see god"));
System.out.println("next set:");
System.out.println(palindromeSolver1("eye"));
System.out.println(palindromeSolver1(""));
System.out.println(palindromeSolver1("hi"));
System.out.println(palindromeSolver1("i"));
System.out.println(palindromeSolver1("do geese see god"));
}
public static boolean palindromeSolver1(String input)
{
//solving using efficient pointer-based method
input = input.replaceAll(" ", "");
int i = 0;
int j = input.length() - 1;
int loopLen = input.length()/2;
for(i = 0; i < loopLen; i++)
{
if(input.charAt(i) != input.charAt(j))
{
return false;
}
}
return true;
}
public static boolean palindromeSolver2(String input)
{
//solving using inefficient new string creation method
String temp = "";
input = input.replaceAll(" ", "");
for(int i = input.length()-1; i >= 0; i--)
{
temp += input.charAt(i);
}
//System.out.println(temp);
//System.out.println(input);
if(input.equals(temp))
{
return true;
}
else{
return false;
}
}
}