-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThe Longest Palindrome(brute-force solution).js
More file actions
78 lines (71 loc) · 2.08 KB
/
The Longest Palindrome(brute-force solution).js
File metadata and controls
78 lines (71 loc) · 2.08 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* Created by licheng on 16/4/5.
*/
function findPalindrome(str){
// extend String method to reverse the character like Array
String.prototype.reverse = function(){
return this.split('').reverse().join('')
}
// extend String method to splice(delete) the character like Array
String.prototype.splice = function(index,num,insert){
var that = this.split('')
that.splice(index,num,insert)
return that.join('')
}
//if a string is a palindrome
function judge(str){
if(str === str.reverse()){
return true
}
else{
return false
}
}
// wether to continue the function
var stop = false
// the function to enumerate the charater to be deleted
// c is the number of characters to be deleted
function del(str,c){
if(c ===0 && !stop){
if(judge(str)){
stop = true
console.log('Got it:'+str.length +' '+str)
return str.length
}
}
else if(c === 1 && !stop){
var string = str
var strlen = str.length
for(var i =0;i < strlen;i++){
var after = string.splice(i,1)
// console.log(after+' '+ i) //ex
if(judge(after)){
stop = true
console.log('Got it:'+after.length +' '+after)
return after.length
}
}
}
else if(c > 1 && !stop){
var str = str
var len = str.length
for(var j = 0;j<len&&!stop;j++){
// console.log(j)
var count = c
mystr = str.splice(j,1)
// console.log('Count: '+count+'str: '+mystr+' J: '+j)
del(mystr,--count)
}
}
else{
throw new error('error argument')
}
}
//enumerate the number of characters to be deleted
var i = 0
while (i < str.length && !stop ){
del(str,i)
i++
}
}
findPalindrome('abbaccsf')