700816. Count the Specials

Easy

Count the Specials

Slug: count-the-specials Difficulty: Easy Id: 700816 Topic Tags: Arrays, Data Structures Company Tags: None

Summary

Count the Specials is an easy problem that involves counting the number of special characters in a given array. The problem requires the candidate to write an algorithm that iterates through the array and counts the frequency of special characters.

Detailed Explanation

The given problem description does not provide any specific constraints or rules about what constitutes a "special" character. For the purpose of this explanation, we will assume that a special character is any character that is not alphanumeric (i.e., not a letter or a number).

To solve this problem, we can use a simple iteration through the array and count the frequency of each character. We can keep track of the count in a dictionary (in Python) or a HashMap (in Java/C++), where the keys are the characters and the values are their frequencies.

Here's a step-by-step breakdown of the solution:

  1. Initialize an empty dictionary (HashMap) to store the frequency of each character.
  2. Iterate through the array, character by character.
  3. For each character, check if it is alphanumeric or not.
  4. If the character is not alphanumeric, increment its count in the dictionary (HashMap).
  5. After iterating through the entire array, return the sum of all counts.

Here's a diagram to illustrate this step-by-step process:

          +---------------+
          |  Initialize  |
          +---------------+
                  |
                  v
          +---------------+
          |  Iterate Array  |
          +---------------+
                  |
                  v
          +---------------+
          |  Check Alphanumeric|
          +---------------+
                  |
                  v
          +---------------+
          |  Increment Count  |
          +---------------+
                  |
                  v
          +---------------+
          |  Return Sum of Counts  |
          +---------------+

Time complexity: O(n), where n is the length of the array. Space complexity: O(c), where c is the number of unique characters in the array.

Optimized Solutions

Java

View on GeeksforGeeks
import java.util.HashMap;
import java.util.Map;

public class CountSpecials {
    public static int countSpecialChars(char[] arr) {
        Map<Character, Integer> charCount = new HashMap<>();
        for (char c : arr) {
            if (!Character.isLetterOrDigit(c)) {
                charCount.put(c, charCount.getOrDefault(c, 0) + 1);
            }
        }
        int totalSpecialChars = 0;
        for (Map.Entry<Character, Integer> entry : charCount.entrySet()) {
            totalSpecialChars += entry.getValue();
        }
        return totalSpecialChars;
    }
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.