-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindromes.js
More file actions
156 lines (128 loc) · 3.42 KB
/
palindromes.js
File metadata and controls
156 lines (128 loc) · 3.42 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
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
/*! palindromes v0.0.1 - MIT license */
(function (global) {
'use strict';
//Polyfill
if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/g, '');
};
}
function moduleDefinition() {
function Palindrome(text, index) {
this.text = text;
this.length = text.length;
this.index = index;
}
function find(baseStr, maxlen) {
if (typeof baseStr !== 'string' || baseStr === '' || maxlen < 2) {
return null;
}
if (typeof maxlen !== 'number') {
maxlen = baseStr.length;
}
if (maxlen < 2) {
return null;
}
var
arr,
arrlen,
bufferArray = [],
cachedIndex = 0,
cachedLen = 0,
startIndex = 0,
endIndex = 0,
i = 1,
bufferIndex,
index,
resultArray,
result,
resultLen = 0;
arr = (' ' + baseStr + ' ').split('').join().trim().split('');
arrlen = arr.length;
maxlen = maxlen || baseStr.length;
for (; i < arrlen; i++) {
if (i > cachedIndex) {
bufferArray[i] = 0;
startIndex = i - 1;
endIndex = i + 1;
} else {
bufferIndex = cachedLen * 2 - i;
if (bufferArray[bufferIndex] < (cachedIndex - i)) {
bufferArray[i] = bufferArray[bufferIndex];
startIndex = -1;
} else {
bufferArray[i] = cachedIndex - i;
endIndex = cachedIndex + 1;
startIndex = i * 2 - endIndex;
}
}
while (startIndex >= 0 && endIndex < arr.length && arr[startIndex] === arr[endIndex]) {
bufferArray[i]++;
startIndex--;
endIndex++;
}
if ((i + bufferArray[i]) > cachedIndex) {
cachedLen = i;
cachedIndex = i + bufferArray[i];
}
}
cachedLen = 0;
i = 1;
arrlen = arr.length;
for (; i < arrlen; i++) {
if (resultLen < bufferArray[i] &&
bufferArray[i] <= maxlen &&
bufferArray[i] > 1) {
resultLen = bufferArray[i];
cachedLen = i;
}
}
if (cachedLen === 0) {
return null;
}
index = cachedLen - resultLen;
resultArray = arr.slice(index, cachedLen + resultLen + 1);
result = resultArray.join('').replace(/(\,)/g, '');
index = index / 2;
return new Palindrome(result, index);
}
/**
* @param {String} baseStr
* @param {Number} count
* @return {Array}
* @api public
*/
function palindromes(baseStr, count) {
var result = [],
i = 0,
palindrome,
maxlen = baseStr.length;
for (; i < count; i++) {
palindrome = find(baseStr, maxlen);
if (palindrome && palindrome.text) {
result.push(palindrome);
maxlen = palindrome.text.length - 1;
} else {
break;
}
}
return result;
}
palindromes.find = find;
palindromes.Palindrome = Palindrome;
/**
* Expose palindromes
*/
return palindromes;
}
if (typeof exports === 'object') {
// node export
module.exports = moduleDefinition();
} else if (typeof define === 'function' && define.amd) {
// amd anonymous module registration
define([], moduleDefinition);
} else {
// browser global
global.palindromes = moduleDefinition();
}
}(this));