Skip to main content

Command Palette

Search for a command to run...

Count the Number of Special Characters II

Published
2 min read
Count the Number of Special Characters II
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

You are given a string word. A letter c is called special if it appears both in lowercase and uppercase in word, and every lowercase occurrence of c appears before the first uppercase occurrence of c.

Return the number of special letters in word.

LeetCode Problem - 3121

class Solution {
    public int numberOfSpecialChars(String word) {
        // Map to store the position of the first occurrence of uppercase characters (converted to lowercase)
        Map<Character, Integer> firstUpperCasePos = new HashMap<>();
        // Map to store the position of the last occurrence of lowercase characters
        Map<Character, Integer> lastLowerCasePos = new HashMap<>();

        // Iterate through the string to populate the maps
        for(int i = 0; i < word.length(); i++) {
            char ch = word.charAt(i); // Get the current character
            if(Character.isLowerCase(ch)) { 
                // If the character is lowercase, update its last occurrence position
                lastLowerCasePos.put(ch, i);
            } else if(Character.isUpperCase(ch)) {
                // If the character is uppercase, convert it to lowercase
                char lowerCh = Character.toLowerCase(ch);
                // Store the position of its first occurrence, if not already stored
                if(!firstUpperCasePos.containsKey(lowerCh)) {
                    firstUpperCasePos.put(lowerCh, i);
                }
            }
        }

        // Initialize a counter for "special" characters
        int specialCharsCount = 0;
        // Iterate through all lowercase characters recorded in the lastLowerCasePos map
        for(char ch: lastLowerCasePos.keySet()) {
            // Check if the corresponding uppercase (lowercase equivalent) exists in firstUpperCasePos
            if(firstUpperCasePos.containsKey(ch)) {
                // Check if the last occurrence of the lowercase character is before the first occurrence of its uppercase
                if(lastLowerCasePos.get(ch) < firstUpperCasePos.get(ch)) {
                    specialCharsCount++; // Increment the count if the condition is met
                }
            }
        }

        // Return the count of "special" characters
        return specialCharsCount;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.