Skip to content

Commit cbc669a

Browse files
merge: Upgraded Algorithm of alpha numeric palindrome (TheAlgorithms#1006)
* feat: improved memoize function used Map instead of object & used the JSON.stringfy method for generate a valid string as a key * docs: modified documentation * style: format with standard * docs: modified stringify doc * refactor: remove two repetition implementation * feat: added validation, test codes * chore: remove useless words * feat: added types for jest * chore: added link box * feat: added new validation test casses & methods * style: formated with standard * feat: added parse method & test cases * docs: added js docs * chore: added default import export * feat: imporved algorithm via replace method * test: added two test cases * feat: added jest type for suggestions * feat: added `reduceRight` & `trim` method * chore: added helper variable * feat: added new rotation option * Revert "chore: added helper variable" This reverts commit 489544d. * remove: yarn lock * chore: fix grammer * feat: used replace method & added test case * feat: remove revert * chore: added new line * feat: optimized algo n to n / 2 & replaced test cases * chore: update node version * chore: set node version to lts * chore: updated the node version & added engines prop * resolve: removed while loop * chore: added right shift ops comment * chore: update comment * chore: removed abs Co-authored-by: Rak Laptudirm <[email protected]>
1 parent e05b443 commit cbc669a

File tree

5 files changed

+48
-39
lines changed

5 files changed

+48
-39
lines changed

.github/workflows/Ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
- uses: actions/checkout@v2
1515
- uses: actions/setup-node@v2
1616
with:
17-
node-version: "14"
17+
node-version: "16.x"
1818
cache: npm
1919

2020
- name: 📦 Install dependencies

String/AlphaNumericPalindrome.js

+16-13
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
1-
/*****************************************************************************
2-
* @function alphaNumericPlaindrome
3-
* @description alphaNumericPlaindrome should return true if the string has alphanumeric characters that are palindrome irrespective of special characters and the letter case.
1+
/**
2+
* @function alphaNumericPalindrome
3+
* @description alphaNumericPalindrome should return true if the string has alphanumeric characters that are palindrome irrespective of special characters and the letter case.
44
* @param {string} str the string to check
5-
* @returns {Boolean}
6-
* @see [Factorial](https://en.wikipedia.org/wiki/Palindrome)
5+
* @returns {boolean}
6+
* @see [Palindrome](https://en.wikipedia.org/wiki/Palindrome)
77
* @example
8-
* The function alphaNumericPlaindrome() receives a string with varying formats
8+
* The function alphaNumericPalindrome() receives a string with varying formats
99
* like "racecar", "RaceCar", and "race CAR"
1010
* The string can also have special characters
1111
* like "2A3*3a2", "2A3 3a2", and "2_A3*3#A2"
1212
*
1313
* But the catch is, we have to check only if the alphanumeric characters
1414
* are palindrome i.e remove spaces, symbols, punctuations etc
1515
* and the case of the characters doesn't matter
16-
*
17-
****************************************************************************/
16+
*/
17+
const alphaNumericPalindrome = (str) => {
18+
if (typeof str !== 'string') {
19+
throw new TypeError('Argument should be string')
20+
}
1821

19-
const alphaNumericPlaindrome = (str) => {
2022
// removing all the special characters and turning everything to lowercase
21-
const newStr = str.replace(/[^a-zA-Z0-9]*/g, '').toLowerCase()
23+
const newStr = str.replace(/[^a-z0-9]+/ig, '').toLowerCase()
24+
const midIndex = newStr.length >> 1 // x >> y = floor(x / 2^y)
2225

23-
for (let i = 0; i < newStr.length; i++) {
24-
if (newStr[i] !== newStr[newStr.length - 1 - i]) {
26+
for (let i = 0; i < midIndex; i++) {
27+
if (newStr.at(i) !== newStr.at(~i)) { // ~n = -(n + 1)
2528
return false
2629
}
2730
}
2831

2932
return true
3033
}
3134

32-
export { alphaNumericPlaindrome }
35+
export default alphaNumericPalindrome
+17-17
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
import { alphaNumericPlaindrome } from '../AlphaNumericPalindrome'
1+
import alphaNumericPalindrome from '../AlphaNumericPalindrome'
22

3-
test('should return true if the given string has alphanumeric characters that are palindrom irrespective of case and symbols', () => {
4-
expect(alphaNumericPlaindrome('eye')).toBe(true)
5-
})
6-
7-
test('should return true if the given string has alphanumeric characters that are palindrom irrespective of case and symbols', () => {
8-
expect(alphaNumericPlaindrome('0_0 (: /-:) 0-0')).toBe(true)
9-
})
3+
describe('Testing the alpha numeric palindrome', () => {
4+
// should return true if the given string has alphanumeric characters that are palindrome irrespective of case and symbols
5+
it('Testing with valid alphabetic palindrome', () => {
6+
expect(alphaNumericPalindrome('eye')).toBe(true)
7+
expect(alphaNumericPalindrome('Madam')).toBe(true)
8+
expect(alphaNumericPalindrome('race CAR')).toBe(true)
9+
expect(alphaNumericPalindrome('A man, a plan, a canal. Panama')).toBe(true)
10+
})
1011

11-
test('should return true if the given string has alphanumeric characters that are palindrom irrespective of case and symbols', () => {
12-
expect(alphaNumericPlaindrome('five|_/|four')).toBe(false)
13-
})
14-
15-
test('should return true if the given string has alphanumeric characters that are palindrom irrespective of case and symbols', () => {
16-
expect(alphaNumericPlaindrome('A man, a plan, a canal. Panama')).toBe(true)
17-
})
12+
it('Testing with number and symbol', () => {
13+
expect(alphaNumericPalindrome('0_0 (: /-:) 0-0')).toBe(true)
14+
expect(alphaNumericPalindrome('03_|53411435|_30')).toBe(true)
15+
})
1816

19-
test('should return true if the given string has alphanumeric characters that are palindrom irrespective of case and symbols', () => {
20-
expect(alphaNumericPlaindrome('1 eye for of 1 eye.')).toBe(false)
17+
it('Testing with alphabets and symbols', () => {
18+
expect(alphaNumericPalindrome('five|_/|evif')).toBe(true)
19+
expect(alphaNumericPalindrome('five|_/|four')).toBe(false)
20+
})
2121
})

package-lock.json

+10-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"@babel/preset-env": "^7.11.5",
1818
"atob": "2.1.2",
1919
"jsdom": "^16.3.0",
20-
"node": "^14.13.1",
20+
"node": "^16.13.2",
2121
"node-fetch": "3.1.1"
2222
},
2323
"standard": {
@@ -34,5 +34,8 @@
3434
"husky": "^7.0.4",
3535
"jest": "^26.4.2",
3636
"standard": "^16.0.4"
37+
},
38+
"engines": {
39+
"node": ">=16.6.0"
3740
}
3841
}

0 commit comments

Comments
 (0)