Python finding programming question in a String

PythonServer Side ProgrammingProgramming

Suppose we have a lowercase string s, we have to check whether it's possible to pick some subsequence of characters in s such that − 1. The difference of any two successive indexes of the characters is same 2. The characters form the string "programmingquestion"

So, if the input is like "pzrzozgzrzazmzmziznzgzqzuzezsztzizozn", then the output will be True

To solve this, we will follow these steps −

  • p := An array of indices where p is present
  • r := An array of indices where r is present
  • for each j in p, do
    • for each k in r, do
      • if k > j, then
        • if "programmingquestion" in substring of s from index j to size of s, by skipping k-j character, then
          • return True
  • return False

Let us see the following implementation to get better understanding −

Example

 Live Demo

class Solution:
   def solve(self, s):
      p = [i for i, c in enumerate(s) if c == "p"]
      r = [i for i, c in enumerate(s) if c == "r"]
      for j in p:
         for k in r:
            if k > j:
               if "programmingquestion" in s[j:len(s):k-j]:
                  return True
      return False
ob = Solution()
s = "pzrzozgzrzazmzmziznzgzqzuzezsztzizozn"
print(ob.solve(s))

Input

"pzrzozgzrzazmzmziznzgzqzuzezsztzizozn"

Output

True
raja
Updated on 22-Sep-2020 11:37:46

Advertisements