-
Notifications
You must be signed in to change notification settings - Fork 0
/
deque-dynamic-array.js
58 lines (44 loc) · 939 Bytes
/
deque-dynamic-array.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
class Deque {
constructor(){
this.deque = [];
}
isEmpty(){
return this.deque.length == 0;
}
size(){
return this.deque.length;
}
addFirst(item){
this.deque.unshift(item);
}
addLast(item){
this.deque.push(item);
}
removeFirst(){
if(this.isEmpty()){
throw new Error('Deque is empty');
}
return this.deque.shift();
}
removeLast(){
if(this.isEmpty()){
throw new Error('Deque is empty');
}
return this.deque.pop();
}
}
function isPalindrome(string){
d = new Deque();
for(s of string){
d.addLast(s);
}
while(d.size() > 1){
if(d.removeFirst() != d.removeLast()){
return false;
}
}
return true;
}
console.log(isPalindrome('kayak'));
console.log(isPalindrome('kayyak'));
console.log(isPalindrome('kaykak'));