Program to count the number of consistent strings in Python

PythonServer Side ProgrammingProgramming

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 −

 Live Demo

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:
         count += 1
   return count

s= "px"
words = ["ad","xp","pppx","xpp","apxpa"]
print(solve(s, words))


"px", ["ad","xp","pppx","xpp","apxpa"]


Published on 18-May-2021 11:41:19