-
Notifications
You must be signed in to change notification settings - Fork 5
/
palindrome.js
160 lines (138 loc) · 4.22 KB
/
palindrome.js
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const word = 'kjowoemiduaaxasnqghxbxkiccikxbxhgqnsaxaaudimeowojk'
function palindrome(s) {
let arr1 = []
let arr2 = []
let test1 = []
let test2 = []
let bol1 = true
let bol2 = true
let count1 = 0
let count2 = 0
let indexTrac1 = 0
let indexTrac2 = 0
console.log(s.length)
if (s.length%2===0){
for (let i=0; i<s.length/2; i++){
arr1[i] = s[i];
test1[i] = s[i];
}
for (let i=s.length-1; i>s.length/2-1; i--){
arr2[s.length - i - 1] = s[i];
test2[s.length - i - 1] = s[i];
}
for (let i = 0; i < s.length / 2; i++){
if(arr1[i]!==arr2[i]){
indexTrac1 = i;
indexTrac2 = s.length - 1 - i;
test1.splice(i,1)
for (let j = 0; j <test1.length; j++){
if (test1[j]!==arr2[j]) {
count1++
}
if (count1>0){
bol1 = false;
break;
}
}
test2.splice(i, 1)
for (let j = 0; j < test2.length; j++) {
if (test2[j] !== arr1[j]) {
count2++
}
if (count2 > 0) {
bol2 = false;
break;
}
}
if (bol1===false) {
if (bol2===false){
return -1
} return indexTrac2
} return indexTrac1
}
}
return -1
} else {
// TO BE DONE
for (let i = 0; i < s.length / 2; i++) {
arr1[i] = s[i];
test1[i] = s[i];
}
for (let i = s.length - 1; i > s.length / 2 - 1; i--) {
arr2[s.length - i - 1] = s[i];
test2[s.length - i - 1] = s[i];
}
console.log(test1)
console.log(test2)
for (let i = 0; i < s.length / 2; i++) {
if (arr1[i] !== arr2[i]) {
indexTrac1 = i;
indexTrac2 = s.length - 1 - i;
test1.splice(i, 1)
for (let j = 0; j < test1.length; j++) {
if (test1[j] !== arr2[j]) {
count1++
}
if (count1 > 0) {
bol1 = false;
break;
}
}
test2.splice(i, 1)
for (let j = 0; j < test2.length; j++) {
if (test2[j] !== arr1[j]) {
count2++
}
if (count2 > 0) {
bol2 = false;
break;
}
}
console.log(bol1 + ' and '+bol2)
if (bol1 === false) {
if (bol2 === false) {
return -1
} return indexTrac2
} return indexTrac1
}
}
return -1
}
}
// if (arr1.join() == arr2.join()) {
// return -1
// }
// } else {
// for (let i = 0; i < Math.floor(s.length / 2); i++) {
// arr1[i] = s[i];
// }
// for (let i = Math.floor(s.length / 2) + 1; i < s.length; i++) {
// arr2[i - (Math.floor(s.length / 2) + 1)] = s[i];
// }
// console.log(arr1)
// console.log(arr2)
// if (arr1.join() == arr2.join()) {
// return -1
// }
// }
// }
console.log(palindrome(word))
// if (s[0]===s[s.length-1] && s[s.length/2]) {
// return -1
// } else {
// let arr1 = [];
// let arr2 = [];
// for (let i=0; i<s.length; i++){
// arr1[i] = s[i];
// arr2[i] = s[i];
// }
// let a1 = arr1.shift();
// let a2 = arr2.pop();
// if (arr1[0]===arr1[arr1.length-1]) {
// return a1
// } else if (arr2[0] === arr2[arr2.length - 1]) {
// return a2
// } else {
// return 'who knows'
// }
// }