Similar Problems
Similar Problems not available
Maximize The Confusion Of An Exam - Leetcode Solution
Companies:
LeetCode: Maximize The Confusion Of An Exam Leetcode Solution
Difficulty: Medium
Topics: string sliding-window prefix-sum binary-search
Problem Statement
Given a string answerKey
representing the correct answers to an exam, and a string studentsAnswer
representing a student's answers, return the maximum number of times the student's answers may differ from the correct answers, provided that:
- The student is allowed to make at most one change.
- If the student changes an answer, it must be changed to a valid answer.
- For each incorrect answer, there must be exactly one correct answer.
Example Input: answerKey = "ABA", studentsAnswer = "CCC" Output: 0 Explanation: The student has no answers correct.
Input: answerKey = "AAAAA", studentsAnswer = "AAACC" Output: 3 Explanation: The student makes 3 changes from 'A' to 'C'.
Approach
One approach for this problem is to use a sliding window of length n
, where n
is the length of the answerKey
, and then slide it over the studentsAnswer
string and count the number of differences between the substrings of studentsAnswer
and answerKey
. To handle the case where the student is allowed to make at most one change, we will maintain a change
variable, initialized to zero, and increment it by one when we encounter a mismatch between the substrings. If we encounter a second mismatch, we will break the loop and return the current value of change
. However, if the substrings match, we will reset the change
variable to zero and continue sliding the window.
To ensure that each incorrect answer has exactly one correct answer, we will keep track of the frequency of the correct answers in the answerKey
string using a frequency array, and the frequency of the incorrect answers in the substring of studentsAnswer
using another frequency array. We will increment the count of the change
variable by the difference in the frequencies of the correct and incorrect answers in the current substring.
At the end of the loop, we will return the value of the change
variable.
Code Implementation
def max_confusion(answerKey: str, studentsAnswer: str) -> int:
# initialize frequency arrays for answerKey and studentsAnswer
count_a = [0] * 26
count_b = [0] * 26
for c in answerKey:
count_a[ord(c) - ord('A')] += 1
# initialize sliding window
n = len(answerKey)
left = right = change = 0
# slide window over studentsAnswer and count differences
for right in range(len(studentsAnswer)):
# update frequency of current character in studentsAnswer
count_b[ord(studentsAnswer[right]) - ord('A')] += 1
# update change counter if current character is incorrect
if right - left + 1 > n:
count_b[ord(studentsAnswer[left]) - ord('A')] -= 1
left += 1
if right - left + 1 == n:
diff = sum(abs(count_a[i] - count_b[i]) for i in range(26)) // 2
if diff > 1:
return change
else:
change = max(change, diff)
return change
Time Complexity
The time complexity of the algorithm is O(n), where n is the length of the studentsAnswer
string, since we slide the window over studentsAnswer
one character at a time.
Space Complexity
The space complexity of the algorithm is O(1), since we use two frequency arrays of size 26 to keep track of the frequencies of the characters in answerKey
and the current substring of studentsAnswer
.
Maximize The Confusion Of An Exam Solution Code
1