admin管理员组

文章数量:1317906

I want to know how can I print the number of characters of a string that does not change its position after reversal of that string in JavaScript.

Is there any way?

data stream - alphxxdida . After reverse - adidaxxhpla. so here x and a doesn't changed it position. How can I do this ?

Input - alphxxdida

Output-4

I want to know how can I print the number of characters of a string that does not change its position after reversal of that string in JavaScript.

Is there any way?

data stream - alphxxdida . After reverse - adidaxxhpla. so here x and a doesn't changed it position. How can I do this ?

Input - alphxxdida

Output-4

Share Improve this question edited Jul 20, 2020 at 16:01 Unmitigated 89.5k12 gold badges96 silver badges103 bronze badges asked Jul 20, 2020 at 15:47 OM-ॐOM-ॐ 1211 gold badge2 silver badges11 bronze badges 3
  • 2 what have you attempted? show us where you got stuck – PA. Commented Jul 20, 2020 at 15:49
  • Your reverse string seems to have found an extra character – Matt Burland Commented Jul 20, 2020 at 15:50
  • So if I understand what you are asking correctly, you want a count of the letters that are the same in the reverse string as in the original string. So iterate through the strings and pare original[i] with reversed[i] and increment a counter for each time they are equal. Where specifically are you stuck with that? – Matt Burland Commented Jul 20, 2020 at 15:53
Add a ment  | 

3 Answers 3

Reset to default 2

A character doesn't change upon reverse if the character in the "mirror image" position is the same character. The "mirror image" position of a character n positions from the start of the string is the character n positions from the end of the string.

let mirroredChars = str => {
  let result = [];
  let halfLen = Math.ceil(str.length / 2);
  let lastIndex = str.length - 1;
  for (let i = 0; i < halfLen; i++) {
    if (str[i] === str[lastIndex - i]) result.push(str[i]);
  }
  return result;
};
console.log(mirroredChars('alphxxdida'));

The count is actually slightly unintuitive. We can't simply take 2 * mirroredChars(...).length, since that would imply the number of mirrored characters is always even (and odd counts can occur, in any odd-length input string, since the middle character can always be considered mirrored).

The count will be:

let countMirroredChars = str => {
  let numMirrored = mirroredChars(str).length;
  return (str.length % 2) // "Is the input string of odd length?"
    ? (numMirrored * 2 - 1)
    : (numMirrored * 2);
};

We can use a bitwise trick to shorten this code. Either of the following work (and the second should deliver better performance, but looks a bit mystical):

let countMirroredChars = str => mirroredChars(str).length * 2 - str.length % 2;
let countMirroredChars = str => mirroredChars(str).length * 2 - str.length & 1;

You can filter over the characters of the string and pare with the character at the corresponding index in the reversed string. The length of the filtered array will be the number of characters that remained the same.

var str = "alphxxdida";
var reversed = [...str].reverse().join('');
const same = [...str].filter((char,i)=>char===reversed[i]);
console.log(same.length);

Of course, you don't actually need the reversed string to perform the filter, as you can calculate the index of the mirrored character.

var str = "alphxxdida";
var same = [...str].filter((char,i)=>char===str[str.length - i - 1]);
console.log(same.length);

Here is with reduce. Traverse till half length of string and pare the chars from both ends of string and count.

Update: As @Gershom pointed out, Fixed to work for odd length of string.

const getCount = (str) =>
  [...str.slice(0, str.length / 2)].reduce(
    (acc, char, i) => acc + (char === str[str.length - 1 - i] ? 2 : 0),
    str.length % 2
  );

var str = "alphxxdida";
var str2 = "alphxdida";

console.log(str, getCount(str));
console.log(str2, getCount(str2));

本文标签: javascriptHow to find characters of a string that did not changed position after resverseStack Overflow