-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalid_pali_2.java
More file actions
executable file
·60 lines (55 loc) · 2.12 KB
/
valid_pali_2.java
File metadata and controls
executable file
·60 lines (55 loc) · 2.12 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Neel_Kapadia
*/
public class valid_pali_2 {
public boolean validPalindrome(String s) {
int arr[] = checkPali(s);
//System.out.println(arr[0]);
if (arr[0] == -1) {
return true;
} else {
String temp = s.substring(arr[0] + 1, arr[1] + 1);
//System.out.println("temp 1: " + temp);
int x[] = checkPali(temp);
// System.out.println("x: " + x[0]);
if(x[0]==-1)
return true;
temp = s.substring(arr[0], arr[1]);
// System.out.println("temp 2: " + temp);
x = checkPali(temp);
// System.out.println("x: " + x[0]);
if(x[0]==-1)
return true;
}
return false;
}
public int[] checkPali(String s) {
int arr[] = new int[2];
int i = 0, j = s.length() - 1;
while (s.charAt(i) == s.charAt(j) && i < j) {
i++;
j--;
}
if (i >= j) {
arr[0] = arr[1] = -1;
} else {
arr[0] = i;
arr[1] = j;
}
return arr;
}
public static void main(String[] args) {
String a = "zergbfjvptexsunllmgxyphgkoamzokbulcfijqjrybxampfmpmkklpasnmuiocxckculoziywffmxkrzyqlqhbxditqfhuydfqnxqehmumfbeqmmiadpvnbdbcxrbqqjuztvlgnxyiiekwuunsebfnhylcgraxkvotqmhobxuwuvdigitffoemvjamcxaujspctlamhupbbzzbdbparftnbsjnmgldrkltqpodcjankueyhrzrlylundccdnulylrzrhyeuknajcdopqtlkrdlgmnjsbntfrapbdbzzbbpuhmaltcpsjuaxcmajvmeofftigidvuwuxbohmqtovkxargclyhnfbesnuuwkeiiyxnglvtzujqqbrxcbdbnvpdaimmqebfmumheqxnqfdyuhfqtidxbhqlqyzrkxmffwyizoluckcxcoiumnsaplkkmpmfpmaxbyrjqjifclubkozmaokghpyxgmllnusxetpvjfbgrezl";
//String a = "abcda";
valid_pali_2 v = new valid_pali_2();
//System.out.println(a.substring(1,2));
boolean x = v.validPalindrome(a);
System.out.println(x);
}
}