

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to count the number of consistent strings in Python
Suppose we have a string s consisting of distinct characters and also have an array of strings called words. A string is consistent when all characters in the string appear in the string s. We have to find the number of consistent strings present in the array words.
So, if the input is like s= "px", words = ["ad","xp","pppx","xpp","apxpa"], then the output will be 3 because there are three strings with only 'p' and 'x', ["xp","pppx","xpp"].
To solve this, we will follow these steps −
count := 0
for i in range 0 to size of words - 1, do
for j in range 0 to size of words[i] - 1, do
if words[i, j] is not in s, then
come out from loop
otherwise,
count := count + 1
return count
Example (Python)
Let us see the following implementation to get better understanding −
def solve(s, words): count = 0 for i in range(len(words)): for j in range(len(words[i])): if words[i][j] not in s: break else: count += 1 return count s= "px" words = ["ad","xp","pppx","xpp","apxpa"] print(solve(s, words))
Input
"px", ["ad","xp","pppx","xpp","apxpa"]
Output
3
- Related Questions & Answers
- Python program to count the pairs of reverse strings
- Python Program to Count number of binary strings without consecutive 1’
- Program to count number of strings we can make using grammar rules in Python
- Program to count sorted vowel strings in Python
- Python program to Sort Strings by Punctuation count
- Program to count number of palindromic substrings in Python
- Program to count number of unhappy friends in Python
- Program to count number of homogenous substrings in Python
- Program to count number of nice subarrays in Python
- Count the number of common divisors of the given strings in C++
- Program to count number of surrounded islands in the matrix in python
- Python program to count number of substring present in string
- Program to count number of possible humble matrices in Python
- Program to count maximum number of strings we can generate from list of words and letter counts in python
- Golang Program to Count the Number of Digits in a Number
Advertisements