Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python finding programming question in a String
When working with strings in Python, we sometimes need to check if we can form a specific subsequence with equal spacing between characters. This problem asks us to find if we can extract the string "programmingquestion" from a given string where each character is picked at regular intervals.
The key insight is that we need to find positions of 'p' and 'r' characters, then check if we can form the target string using consistent spacing starting from these positions.
Problem Understanding
Given a lowercase string, we need to check if we can pick characters at regular intervals to form "programmingquestion". The conditions are ?
- Characters must be picked as a subsequence (maintaining original order)
- The spacing between consecutive picked characters must be constant
- The picked characters must spell "programmingquestion"
Algorithm Steps
To solve this problem, we follow these steps ?
- Find all positions where 'p' appears in the string
- Find all positions where 'r' appears in the string
- For each 'p' position and each 'r' position after it, calculate the spacing
- Check if we can form "programmingquestion" using this spacing
- Return True if any combination works, False otherwise
Example Implementation
class Solution:
def solve(self, s):
# Find all positions of 'p' and 'r'
p = [i for i, c in enumerate(s) if c == "p"]
r = [i for i, c in enumerate(s) if c == "r"]
# Try all combinations of p and r positions
for j in p:
for k in r:
if k > j: # r must come after p
# Extract substring with consistent spacing
spacing = k - j
subsequence = s[j:len(s):spacing]
# Check if target string is found in subsequence
if "programmingquestion" in subsequence:
return True
return False
# Test the solution
ob = Solution()
s = "pzrzozgzrzazmzmziznzgzqzuzezsztzizozn"
print(f"Input: {s}")
print(f"Output: {ob.solve(s)}")
Input: pzrzozgzrzazmzmziznzgzqzuzezsztzizozn Output: True
How It Works
Let's trace through the example ?
- The string contains 'p' at position 0 and 'r' at position 2
- The spacing between them is 2 - 0 = 2
- Extracting every 2nd character starting from position 0: "programmingquestion"
- Since this matches our target string exactly, the function returns True
Alternative Approach
Here's a more explicit version that shows the extracted subsequence ?
def find_programming_question(s):
target = "programmingquestion"
p_positions = [i for i, c in enumerate(s) if c == "p"]
r_positions = [i for i, c in enumerate(s) if c == "r"]
for p_pos in p_positions:
for r_pos in r_positions:
if r_pos > p_pos:
spacing = r_pos - p_pos
subsequence = s[p_pos::spacing]
print(f"Spacing: {spacing}, Subsequence: {subsequence}")
if target in subsequence:
return True
return False
# Test with example
s = "pzrzozgzrzazmzmziznzgzqzuzezsztzizozn"
result = find_programming_question(s)
print(f"Result: {result}")
Spacing: 2, Subsequence: programmingquestion Result: True
Time Complexity
The time complexity is O(n × p × r) where n is the string length, p is the number of 'p' characters, and r is the number of 'r' characters. In the worst case, this becomes O(n³).
Conclusion
This algorithm efficiently finds if we can extract "programmingquestion" with consistent character spacing. The key is to identify potential starting points and spacings, then verify if the target string can be formed using slice notation with step parameters.
