Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bf681d1

Browse files
merge: alphaNumericPlaindrome optamization (#857)
1 parent 6f1edd1 commit bf681d1

File tree

1 file changed

+10
-34
lines changed

1 file changed

+10
-34
lines changed

‎String/AlphaNumericPalindrome.js‎

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
/**
2-
* alphaNumericPlaindrome should return true if the string has alphanumeric characters that are palindrome irrespective of special characters and the letter case
3-
* @param {string} str the string to check
4-
* @returns `Boolean`
5-
*/
6-
71
/*****************************************************************************
8-
* What is a palindrome? https://en.wikipedia.org/wiki/Palindrome
9-
*
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.
4+
* @param {string} str the string to check
5+
* @returns {Boolean}
6+
* @see [Factorial](https://en.wikipedia.org/wiki/Palindrome)
7+
* @example
108
* The function alphaNumericPlaindrome() receives a string with varying formats
119
* like "racecar", "RaceCar", and "race CAR"
1210
* The string can also have special characters
@@ -16,41 +14,19 @@
1614
* are palindrome i.e remove spaces, symbols, punctuations etc
1715
* and the case of the characters doesn't matter
1816
*
19-
* This is one of the questions/projects that we have to solve for the
20-
* JavaScript Algorithms and Data Structures course on https://www.freecodecamp.org
21-
*
22-
* Author -- Syed Fasiuddin
23-
* https://github.com/SyedFasiuddin
24-
*
2517
****************************************************************************/
2618

2719
const alphaNumericPlaindrome = (str) => {
2820
// removing all the special characters and turning everything to lowercase
2921
const newStr = str.replace(/[^a-zA-Z0-9]*/g, '').toLowerCase()
30-
// the newStr variable is a string and only has alphanumeric characters all in lowercase
31-
32-
// making an array of individual characters as it's elements
33-
const arr = newStr.split('')
34-
35-
// setting a variable to see if change occurs to it
36-
let palin = 0
37-
38-
// making a copy of arr with spread operator
39-
const arrRev = [...arr]
40-
// you can use arrRev.reverse() to reverse the array
41-
// or else you can use the below method
4222

43-
// iterate through the arr and check the condition of palindrome
44-
for (let i = 0; i < arr.length; i++) {
45-
if (arr[i] !== arrRev[arr.length - 1 - i]) {
46-
// if the string is not palindrome then we change palin variable to 1
47-
palin = 1
23+
for (let i = 0; i < newStr.length; i++) {
24+
if (newStr[i] !== newStr[newStr.length - 1 - i]) {
25+
return false
4826
}
4927
}
5028

51-
// if the string is palindrome then palin variable is never changed
52-
if (palin === 0) return true
53-
else return false
29+
return true
5430
}
5531

5632
export { alphaNumericPlaindrome }

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /