OM-ॐ
OM-ॐ

Reputation: 121

How to find characters of a string that did not changed position after resverse

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

Upvotes: 2

Views: 19040

Answers (3)

Siva Kondapi Venkata
Siva Kondapi Venkata

Reputation: 11001

Here is with reduce. Traverse till half length of string and compare 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));

Upvotes: 0

Gershom Maes
Gershom Maes

Reputation: 8150

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;

Upvotes: 2

Unmitigated
Unmitigated

Reputation: 89224

You can filter over the characters of the string and compare 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);

Upvotes: 1

Related Questions