Counting the number of letters that occupy their positions in the alphabets for array of strings using JavaScript

We are required to write a JavaScript function that takes in an array of strings of English lowercase alphabets.

Our function should map the input array to an array whose corresponding elements are the count of the number of characters that had the same 1-based index in the string as their 1-based index in the alphabets.

For instance, this count for the string 'akcle' will be 3 because the characters 'a', 'c' and 'e' have 1-based index of 1, 3 and 5 respectively both in the string and the English alphabets.

Understanding the Problem

We need to check each character in a string and see if its position matches its alphabetical position. For example:

  • 'a' at position 1 matches alphabet position 1 ?
  • 'b' at position 2 matches alphabet position 2 ?
  • 'z' at position 1 does not match alphabet position 26 ?

Solution Approach

We'll iterate through each string in the array, compare each character's position with its alphabetical position, and count the matches.

Example

const arr = ["abode", "ABc", "xyzD"];

const findIndexPairCount = (arr = []) => {
    const alphabet = 'abcdefghijklmnopqrstuvwxyz';
    const res = [];
    
    for (let i = 0; i < arr.length; i++) {
        let count = 0;
        for (let j = 0; j < arr[i].length; j++) {
            if (arr[i][j].toLowerCase() === alphabet[j]) {
                count++;
            }
        }
        res.push(count);
    }
    
    return res;
};

console.log(findIndexPairCount(arr));
[ 4, 3, 1 ]

Step-by-Step Breakdown

Let's trace through the example:

const testArray = ["abode", "ABc", "xyzD"];

// Breaking down each string:
console.log("String 'abode':");
console.log("a(pos 0) matches a(alphabet pos 0) ?");
console.log("b(pos 1) matches b(alphabet pos 1) ?"); 
console.log("o(pos 2) vs c(alphabet pos 2) ?");
console.log("d(pos 3) matches d(alphabet pos 3) ?");
console.log("e(pos 4) matches e(alphabet pos 4) ?");
console.log("Total matches: 4");

console.log("\nString 'ABc':");
console.log("A(pos 0) matches a(alphabet pos 0) ?");
console.log("B(pos 1) matches b(alphabet pos 1) ?");
console.log("c(pos 2) matches c(alphabet pos 2) ?");
console.log("Total matches: 3");

console.log("\nString 'xyzD':");
console.log("x(pos 0) vs a(alphabet pos 0) ?");
console.log("y(pos 1) vs b(alphabet pos 1) ?");
console.log("z(pos 2) vs c(alphabet pos 2) ?");
console.log("D(pos 3) matches d(alphabet pos 3) ?");
console.log("Total matches: 1");
String 'abode':
a(pos 0) matches a(alphabet pos 0) ?
b(pos 1) matches b(alphabet pos 1) ?
o(pos 2) vs c(alphabet pos 2) ?
d(pos 3) matches d(alphabet pos 3) ?
e(pos 4) matches e(alphabet pos 4) ?
Total matches: 4

String 'ABc':
A(pos 0) matches a(alphabet pos 0) ?
B(pos 1) matches b(alphabet pos 1) ?
c(pos 2) matches c(alphabet pos 2) ?
Total matches: 3

String 'xyzD':
x(pos 0) vs a(alphabet pos 0) ?
y(pos 1) vs b(alphabet pos 1) ?
z(pos 2) vs c(alphabet pos 2) ?
D(pos 3) matches d(alphabet pos 3) ?
Total matches: 1

Alternative Implementation

Here's a more concise version using array methods:

const findIndexPairCountAlt = (arr = []) => {
    return arr.map(str => 
        str.split('').reduce((count, char, index) => {
            return char.toLowerCase() === String.fromCharCode(97 + index) 
                ? count + 1 
                : count;
        }, 0)
    );
};

const testArr = ["abode", "ABc", "xyzD"];
console.log(findIndexPairCountAlt(testArr));
[ 4, 3, 1 ]

Key Points

  • The function handles both uppercase and lowercase letters by converting to lowercase
  • Position comparison is 0-based (array index 0 corresponds to 'a')
  • String.fromCharCode(97 + index) generates alphabet characters dynamically
  • The algorithm has O(n×m) time complexity where n is array length and m is average string length

Conclusion

This solution efficiently counts characters that match their alphabetical positions in each string. The function works with mixed-case input and returns an array of counts corresponding to each input string.

Updated on: 2026-03-15T23:19:00+05:30

367 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements