forked from RyanFehr/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
119 lines (99 loc) · 3.44 KB
/
Solution.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
//Problem: https://www.hackerrank.com/challenges/palindrome-index
//Java 8
/*
Initial Thoughts:
We can use pointers from the beggining and
end to iterate through the string
If they pointers' chars match then when advance
each one
If they don't match we check if the element to
the right of the left pointer matches the right
pointer and if not we check if the pointer to the
left of the right pointer matches the left pointer
if neither is true then return -1 else if 1 is true
we advance the corresponding pointer and store its
prev index
If we have non matching pointers after something was
already removed we just return -1
It is possible to delete the wrong one when
checking ahead, so we check the other branch if
our first branch fails
*/
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner input = new Scanner(System.in);
int n = input.nextInt(); input.nextLine();
tests:
for(int t = 0; t < n; t++)
{
String s = input.nextLine();
int outputIndex = -1;
boolean removal = false;
for(int i = 0, j = s.length()-1; i < j; i++, j--)
{
if(s.charAt(i) != s.charAt(j))
{
if(removal)
{
removal = false;
outputIndex = -1;
break;
}
if(s.charAt(i+1) == s.charAt(j))
{
removal = true;
outputIndex = i;
i++;
}
else if(s.charAt(i) == s.charAt(j-1))
{
removal = true;
outputIndex = j;
j--;
}
else
{
removal = false;
outputIndex = -1;
break;
}
}
}
if(outputIndex != -1)
{
System.out.println(outputIndex); continue tests;
}
//Case where we made the wrong removal decision and we could have made a palindrome
for(int i = 0, j = s.length()-1; i < j; i++, j--)
{
if(s.charAt(i) != s.charAt(j))
{
if(removal)
{
System.out.println(-1); continue tests;
}
if(s.charAt(i) == s.charAt(j-1))
{
removal = true;
outputIndex = j;
j--;
}
else if(s.charAt(i+1) == s.charAt(j))
{
removal = true;
outputIndex = i;
i++;
}
else
{
System.out.println(-1); continue tests;
}
}
}
System.out.println(outputIndex);
}
}
}