-
Notifications
You must be signed in to change notification settings - Fork 1
/
ValidPalindrome-2.java
49 lines (42 loc) · 1.09 KB
/
ValidPalindrome-2.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
import java.util.*;
public class Main {
// ~~~~~~~~~~~~~~User Section~~~~~~~~~~~~~~
public static boolean validPalindrome(String s) {
boolean check = false ;
int i = 0 ;
int j = s.length()-1;
while(i < j)
{
char a = s.charAt(i);
char b = s.charAt(j);
if(a == b)
{
i++ ;
j-- ;
}else{
{
if(s.charAt(i+1) == b && check == false)
{
i+=2;
j-- ;
check = true ;
}else if(s.charAt(j-1) == a && check == false){
j-=2;
i++;
check = true ;
}else{
return false ;
}
}
}
}
return true ;
}
// ~~~~~~~~~~~~Input Management~~~~~~~~~~~~~~~~
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
String str = scn.nextLine();
boolean res = validPalindrome(str);
System.out.println(res);
}
}