Skip to main content

Command Palette

Search for a command to run...

Count the Number of Consistent Strings

Published
1 min read
Count the Number of Consistent Strings
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 allowed consisting of distinct characters and an array of strings words. A string is consistent if all characters in the string appear in the string allowed.

Return the number of consistent strings in the array words.

LeetCode Problem - 1684

class Solution {
    public int countConsistentStrings(String allowed, String[] words) {
        // Initialize a counter to track the number of consistent strings
        int count = 0;

        // Iterate over each word in the 'words' array
        for (String temp : words) {
            // Initialize a variable to count how many characters in the current word are allowed
            int val = 0;

            // Iterate over each character in the current word 'temp'
            for (int j = 0; j < temp.length(); j++) {
                // Get the current character as a string
                String check = String.valueOf(temp.charAt(j));

                // Check if the character is in the 'allowed' string
                if (allowed.contains(check)) {
                    // Increment val if the character is allowed
                    val++;
                } else {
                    // If the character is not allowed, break out of the loop
                    break;
                }
            }

            // If all characters in the word are allowed (i.e., val equals word length)
            if (val == temp.length()) {
                // Increment the count of consistent strings
                count++;
            }
        }

        // Return the final count of consistent strings
        return count;
    }
}

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.