Count the Number of Special Characters I

easy

By - Aman Pareek

Last Updated - 11/09/2024

Problem Statement

You are given a string word. A character is considered special if it appears in both lowercase and uppercase forms within the string.

Your task is to count the number of unique special characters in the given string.

Example 1

Input: word = "aaAbcBC"

Output: 3

Example 2

Input: word = "abc"

Output: 0

Example 3

Input: word = "abBCab"

Output: 1

Solution 1: Using Sets to Track Special Characters

function countSpecialCharactersSets(word) {
    const lowerSet = new Set();
    const upperSet = new Set();
    
    for (const char of word) {
        if (char === char.toLowerCase()) {
            lowerSet.add(char);
        } else {
            upperSet.add(char.toLowerCase());
        }
    }

    let specialCount = 0;
    for (const char of lowerSet) {
        if (upperSet.has(char)) {
            specialCount++;
        }
    }

    return specialCount;
} 

const word1 = "aaAbcBC";
countSpecialCharactersSets(word1);  //output: 3 

const word2 = "abc";
countSpecialCharactersSets(word2);  //output: 0 

const word3 = "abBCab";
countSpecialCharactersSets(word3);  //output: 1 

Solution 2: Using Frequency Count Arrays

function countSpecialCharactersFrequency(word) {
    const lowerFreq = new Array(26).fill(false);
    const upperFreq = new Array(26).fill(false);

    for (const char of word) {
        if (char === char.toLowerCase()) {
            lowerFreq[char.charCodeAt(0) - 'a'.charCodeAt(0)] = true;
        } else {
            upperFreq[char.charCodeAt(0) - 'A'.charCodeAt(0)] = true;
        }
    }

    let specialCount = 0;
    for (let i = 0; i < 26; i++) {
        if (lowerFreq[i] && upperFreq[i]) {
            specialCount++;
        }
    }

    return specialCount;
} 

const word1 = "aaAbcBC";
countSpecialCharactersFrequency(word1);  //output: 3 

const word2 = "abc";
countSpecialCharactersFrequency(word2);  //output: 0 

const word3 = "abBCab";
countSpecialCharactersFrequency(word3);  //output: 1 

Solution 3: Using Map to Track Character Occurrences

function countSpecialCharactersMap(word) {
    const charMap = new Map();

    for (const char of word) {
        const lowerChar = char.toLowerCase();
        const isUpper = char === char.toUpperCase();

        if (!charMap.has(lowerChar)) {
            charMap.set(lowerChar, { lower: false, upper: false });
        }

        if (isUpper) {
            charMap.get(lowerChar).upper = true;
        } else {
            charMap.get(lowerChar).lower = true;
        }
    }

    let specialCount = 0;
    for (const [_, { lower, upper }] of charMap) {
        if (lower && upper) {
            specialCount++;
        }
    }

    return specialCount;
} 

const word1 = "aaAbcBC";
countSpecialCharactersMap(word1);  //output: 3 

const word2 = "abc";
countSpecialCharactersMap(word2);  //output: 0 

const word3 = "abBCab";
countSpecialCharactersMap(word3);  //output: 1 

Solution 4: Using Regular Expressions

function countSpecialCharactersRegex(word) {
    const uniqueChars = new Set(word.toLowerCase());
    let specialCount = 0;

    uniqueChars.forEach(char => {
        const lowerMatch = new RegExp(char, 'g').test(word);
        const upperMatch = new RegExp(char.toUpperCase(), 'g').test(word);

        if (lowerMatch && upperMatch) {
            specialCount++;
        }
    });

    return specialCount;
} 

const word1 = "aaAbcBC";
countSpecialCharactersRegex(word1);  //output: 3 

const word2 = "abc";
countSpecialCharactersRegex(word2);  //output: 0 

const word3 = "abBCab";
countSpecialCharactersRegex(word3);  //output: 1 

Solution 5: Using Two Passes and indexOf

function countSpecialCharactersIndexOf(word) {
    const uniqueChars = new Set(word.toLowerCase());
    let specialCount = 0;

    for (const char of uniqueChars) {
        if (word.indexOf(char) !== -1 && word.indexOf(char.toUpperCase()) !== -1) {
            specialCount++;
        }
    }

    return specialCount;
} 

const word1 = "aaAbcBC";
countSpecialCharactersIndexOf(word1);  //output: 3 

const word2 = "abc";
countSpecialCharactersIndexOf(word2);  //output: 0 

const word3 = "abBCab";
countSpecialCharactersIndexOf(word3);  //output: 1 

Solution 6: Using Set Intersection

function countSpecialCharactersSetIntersection(word) {
    const lowerSet = new Set();
    const upperSet = new Set();

    for (const char of word) {
        if (char === char.toLowerCase()) {
            lowerSet.add(char);
        } else {
            upperSet.add(char.toLowerCase());
        }
    }

    const intersection = [...lowerSet].filter(char => upperSet.has(char));
    return intersection.length;
} 

const word1 = "aaAbcBC";
countSpecialCharactersSetIntersection(word1);  //output: 3 

const word2 = "abc";
countSpecialCharactersSetIntersection(word2);  //output: 0 

const word3 = "abBCab";
countSpecialCharactersSetIntersection(word3);  //output: 1 

Popular Solutions