-
Couldn't load subscription status.
- Fork 266
Aang's Training Sequence
Unit 12 Session 1 Standard (Click for link to problem statements)
- 💡 Difficulty: Medium
- ⏰ Time to complete: 25 mins
- 🛠️ Topics: String Manipulation, Dynamic Programming
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
- What is the goal of the problem?
- The goal is to find the maximum
k-repeatingvalue for a givenmovein asequence.
- The goal is to find the maximum
- How do we define k-repeating?
- A substring is
k-repeatingif it consists of the techniquemoverepeatedktimes consecutively.
- A substring is
HAPPY CASE
Input:
sequence = ""airairwater""
move = ""air""
Output:
2
Explanation:
""airair"" is a substring that repeats ""air"" twice in the sequence.
EDGE CASE
Input:
sequence = ""waterfire""
move = ""air""
Output:
0
Explanation:
""air"" does not appear in the sequence, so its k-repeating value is 0.
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For k-repeating problems, we want to consider the following approaches:
-
String Manipulation: We can check for repeating patterns by constructing a string
move * kand checking if it is a substring ofsequence. -
Dynamic Programming: While not necessary in this case, DP can be used to optimize searching through the sequence for larger values of
k.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: We will incrementally check how many times the string move repeats in the sequence by checking if move * k is a substring of sequence. We continue until we find the largest k such that move * k is a valid substring.
-
Initialize k to 0: Start by assuming
k = 0and incrementkas long asmove * (k + 1)is a substring ofsequence. -
Check for Substring:
- Use the
inoperator to check ifmove * (k + 1)exists in thesequence.
- Use the
-
Return k:
- Once
move * (k + 1)is no longer a substring, returnkas the maximum k-repeating value.
- Once
Implement the code to solve the algorithm.
def max_k_repeating(sequence, move):
k = 0
# Keep increasing k while move * k is a valid substring in sequence
while (move * (k + 1)) in sequence:
k += 1
return kReview the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Example 1:
- Input: sequence = ""airairwater"" move = ""air""
- Expected Output:
2
Example 2:
- Input: sequence = ""fireearthfire"" move = ""fire""
- Expected Output:
1
Example 3:
- Input: sequence = ""waterfire"" move = ""air""
- Expected Output:
0
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume n is the length of sequence and m is the length of move.
-
Time Complexity:
O(n * m)because we construct the stringmove * kand check if it is a substring ofsequencefor increasing values ofk. -
Space Complexity:
O(k * m)to store the repeated stringmove * kwhile checking for each repetition.