- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python finding programming question in a String
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
- if "programmingquestion" in substring of s from index j to size of s, by skipping k-j character, then
- if k > j, then
- for each k in r, do
- return False
Let us see the following implementation to get better understanding −
Example
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
Advertisements