We are given two strings P and Q, each consisting of N lowercase English letters. For each position in the strings, we have to choose one letter from either P or Q, in order to construct a new string S, such that the number of distinct letters in S is minimal. Our task is to find the number of distinct letters in the resulting string S.
For example, if P = "ca" and Q = "ab", S can be equal to: "ca", "cb", "aa" or "ab". String "aa" has only one distinct letter ('a'), so the answer is 1 (which is minimal among those strings).
Write a function:
def solution(P, Q)
that, given two strings P and Q, both of length N, returns the minimum number of distinct letters of a string S, that can be constructed from P and Q as described above.
Examples:
1. Given P = "abc", Q = "bcd", your function should return 2. All possible strings S that can be constructed are: "abc", "abd", "acc", "acd", "bbc", "bbd", "bcc", "bcd". The minimum number of distinct letters is 2, which be obtained by constructing the following strings: "acc", "bbc", "bbd", "bcc".
2. Given P = "axxz", Q = "yzwy", your function should return 2. String S must consist of at least two distinct letters in this case. We can construct S = "yxxy", where the number of distinct letters is equal to 2, and this is the only optimal solution.
3. Given P = "bacad", Q = "abada", your function should return 1. We can choose the letter 'a' in each position, so S can be equal to "aaaaa".
4. Given P = "amz", Q = "amz", your function should return 3. The input strings are identical, so the only possible S that can be constructed is "amz", and its number of distinct letters is 3.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..200,000];
- strings P and Q are both of length N;
- strings P and Q are made only of lowercase letters (a−z);
- strings P and Q contain a total of at most 20 distinct letters.
Traceback (most recent call last): File "exec.py", line 113, in <module> main() File "exec.py", line 89, in main result = solution( P, Q ) File "/tmp/exec_user_xxjz2p7f/solution.py", line 12, in solution return ans NameError: name 'ans' is not defined
Traceback (most recent call last): File "exec.py", line 113, in <module> main() File "exec.py", line 89, in main result = solution( P, Q ) File "/tmp/exec_user_xxjz2p7f/solution.py", line 12, in solution return ans NameError: name 'ans' is not defined
Traceback (most recent call last): File "exec.py", line 113, in <module> main() File "exec.py", line 89, in main result = solution( P, Q ) File "/tmp/exec_user_xxjz2p7f/solution.py", line 12, in solution return ans NameError: name 'ans' is not defined
Traceback (most recent call last): File "exec.py", line 113, in <module> main() File "exec.py", line 89, in main result = solution( P, Q ) File "/tmp/exec_user_xxjz2p7f/solution.py", line 12, in solution return ans NameError: name 'ans' is not defined
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(P, Q):
# setup
a = [0] * 26 # Initialize an array to keep track of character counts
b1 = [0] * 20 # Initialize an array to store intermediate calculations
b2 = [[0] * 20 for _ in range(20)] # Initialize a 2D array to store additional calculations
n = len(P) # Determine the size of the input strings
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(P, Q):
# setup
a = [0] * 26 # Initialize an array to keep track of character counts
b1 = [0] * 20 # Initialize an array to store intermediate calculations
b2 = [[0] * 20 for _ in range(20)] # Initialize a 2D array to store additional calculations
n = len(P) # Determine the size of the input strings
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(P, Q):
# setup
a = [0] * 26 # Initialize an array to keep track of character counts
b1 = [0] * 20 # Initialize an array to store intermediate calculations
b2 = [[0] * 20 for _ in range(20)] # Initialize a 2D array to store additional calculations
n = len(P) # Determine the size of the input strings
# compute
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(P, Q):
# setup
a = [0] * 26 # Initialize an array to keep track of character counts
b1 = [0] * 20 # Initialize an array to store intermediate calculations
b2 = [[0] * 20 for _ in range(20)] # Initialize a 2D array to store additional calculations
n = len(P) # Determine the size of the input strings
# compute
# Update the character counts in array 'a' based on the characters in strings 'P' and 'Q'
for i in range(n):
a[ord(P[i]) - ord('a')] = a[ord(Q[i]) - ord('a')] = 1
# Calculate cumulative sums in array 'a'
for i in range(1, 26):
a[i] += a[i-1]
b1 = [0] * 20 # Reset array 'b1' to zeros
b2 = [[0] * 20 for _ in range(20)] # Reset array 'b2' to zeros
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(P, Q):
# setup
a = [0] * 26 # Initialize an array to keep track of character counts
b1 = [0] * 20 # Initialize an array to store intermediate calculations
b2 = [[0] * 20 for _ in range(20)] # Initialize a 2D array to store additional calculations
n = len(P) # Determine the size of the input strings
# compute
# Update the character counts in array 'a' based on the characters in strings 'P' and 'Q'
for i in range(n):
a[ord(P[i]) - ord('a')] = a[ord(Q[i]) - ord('a')] = 1
# Calculate cumulative sums in array 'a'
for i in range(1, 26):
a[i] += a[i-1]
b1 = [0] * 20 # Reset array 'b1' to zeros
b2 = [[0] * 20 for _ in range(20)] # Reset array 'b2' to zeros
# Perform calculations for 'b1' and 'b2'
for i in range(n):
if P[i] == Q[i]:
b1[a[ord(P[i]) - ord('a')]] += 1
else:
b1[a[ord(P[i]) - ord('a')]] += 1
b1[a[ord(Q[i]) - ord('a')]] += 1
b2[a[ord(P[i]) - ord('a')]][a[ord(Q[i]) - ord('a')]] -= 1
b2[a[ord(Q[i]) - ord('a')]][a[ord(P[i]) - ord('a')]] -= 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(P, Q):
# setup
a = [0] * 26 # Initialize an array to keep track of character counts
b1 = [0] * 20 # Initialize an array to store intermediate calculations
b2 = [[0] * 20 for _ in range(20)] # Initialize a 2D array to store additional calculations
n = len(P) # Determine the size of the input strings
# compute
# Update the character counts in array 'a' based on the characters in strings 'P' and 'Q'
for i in range(n):
a[ord(P[i]) - ord('a')] = a[ord(Q[i]) - ord('a')] = 1
# Calculate cumulative sums in array 'a'
for i in range(1, 26):
a[i] += a[i-1]
b1 = [0] * 20 # Reset array 'b1' to zeros
b2 = [[0] * 20 for _ in range(20)] # Reset array 'b2' to zeros
# Perform calculations for 'b1' and 'b2'
for i in range(n):
if P[i] == Q[i]:
b1[a[ord(P[i]) - ord('a')]] += 1
else:
b1[a[ord(P[i]) - ord('a')]] += 1
b1[a[ord(Q[i]) - ord('a')]] += 1
b2[a[ord(P[i]) - ord('a')]][a[ord(Q[i]) - ord('a')]] -= 1
b2[a[ord(Q[i]) - ord('a')]][a[ord(P[i]) - ord('a')]] -= 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(P, Q):
# setup
a = [0] * 26 # Initialize an array to keep track of character counts
b1 = [0] * 20 # Initialize an array to store intermediate calculations
b2 = [[0] * 20 for _ in range(20)] # Initialize a 2D array to store additional calculations
n = len(P) # Determine the size of the input strings
# compute
# Update the character counts in array 'a' based on the characters in strings 'P' and 'Q'
for i in range(n):
a[ord(P[i]) - ord('a')] = a[ord(Q[i]) - ord('a')] = 1
# Calculate cumulative sums in array 'a'
for i in range(1, 26):
a[i] += a[i-1]
b1 = [0] * 20 # Reset array 'b1' to zeros
b2 = [[0] * 20 for _ in range(20)] # Reset array 'b2' to zeros
# Perform calculations for 'b1' and 'b2'
for i in range(n):
if P[i] == Q[i]:
b1[a[ord(P[i]) - ord('a')]] += 1
else:
b1[a[ord(P[i]) - ord('a')]] += 1
b1[a[ord(Q[i]) - ord('a')]] += 1
b2[a[ord(P[i]) - ord('a')]][a[ord(Q[i]) - ord('a')]] -= 1
b2[a[ord(Q[i]) - ord('a')]][a[ord(P[i]) - ord('a')]] -= 1
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(P, Q):
# setup
a = [0] * 26 # Initialize an array to keep track of character counts
b1 = [0] * 20 # Initialize an array to store intermediate calculations
b2 = [[0] * 20 for _ in range(20)] # Initialize a 2D array to store additional calculations
n = len(P) # Determine the size of the input strings
# compute
# Update the character counts in array 'a' based on the characters in strings 'P' and 'Q'
for i in range(n):
a[ord(P[i]) - ord('a')] = a[ord(Q[i]) - ord('a')] = 1
# Calculate cumulative sums in array 'a'
for i in range(1, 26):
a[i] += a[i-1]
b1 = [0] * 20 # Reset array 'b1' to zeros
b2 = [[0] * 20 for _ in range(20)] # Reset array 'b2' to zeros
# Perform calculations for 'b1' and 'b2'
for i in range(n):
if P[i] == Q[i]:
b1[a[ord(P[i]) - ord('a')]] += 1
else:
b1[a[ord(P[i]) - ord('a')]] += 1
b1[a[ord(Q[i]) - ord('a')]] += 1
b2[a[ord(P[i]) - ord('a')]][a[ord(Q[i]) - ord('a')]] -= 1
b2[a[ord(Q[i]) - ord('a')]][a[ord(P[i]) - ord('a')]] -= 1
m = a[25] + 1 # Determine the number of distinct characters
ans = m # Set 'ans' as the maximum possible value, worst case
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(P, Q):
# setup
a = [0] * 26 # Initialize an array to keep track of character counts
b1 = [0] * 20 # Initialize an array to store intermediate calculations
b2 = [[0] * 20 for _ in range(20)] # Initialize a 2D array to store additional calculations
n = len(P) # Determine the size of the input strings
# compute
# Update the character counts in array 'a' based on the characters in strings 'P' and 'Q'
for i in range(n):
a[ord(P[i]) - ord('a')] = a[ord(Q[i]) - ord('a')] = 1
# Calculate cumulative sums in array 'a'
for i in range(1, 26):
a[i] += a[i-1]
b1 = [0] * 20 # Reset array 'b1' to zeros
b2 = [[0] * 20 for _ in range(20)] # Reset array 'b2' to zeros
# Perform calculations for 'b1' and 'b2'
for i in range(n):
if P[i] == Q[i]:
b1[a[ord(P[i]) - ord('a')]] += 1
else:
b1[a[ord(P[i]) - ord('a')]] += 1
b1[a[ord(Q[i]) - ord('a')]] += 1
b2[a[ord(P[i]) - ord('a')]][a[ord(Q[i]) - ord('a')]] -= 1
b2[a[ord(Q[i]) - ord('a')]][a[ord(P[i]) - ord('a')]] -= 1
m = a[25] + 1 # Determine the number of distinct characters
ans = m # Set 'ans' as the maximum possible value, worst case
o = [0] * (1 << m) # Create a list to store intermediate calculations
s = [0] * (1 << m) # Create a list to store intermediate calculations
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(P, Q):
# setup
a = [0] * 26 # Initialize an array to keep track of character counts
b1 = [0] * 20 # Initialize an array to store intermediate calculations
b2 = [[0] * 20 for _ in range(20)] # Initialize a 2D array to store additional calculations
n = len(P) # Determine the size of the input strings
# compute
# Update the character counts in array 'a' based on the characters in strings 'P' and 'Q'
for i in range(n):
a[ord(P[i]) - ord('a')] = a[ord(Q[i]) - ord('a')] = 1
# Calculate cumulative sums in array 'a'
for i in range(1, 26):
a[i] += a[i-1]
b1 = [0] * 20 # Reset array 'b1' to zeros
b2 = [[0] * 20 for _ in range(20)] # Reset array 'b2' to zeros
# Perform calculations for 'b1' and 'b2'
for i in range(n):
if P[i] == Q[i]:
b1[a[ord(P[i]) - ord('a')]] += 1
else:
b1[a[ord(P[i]) - ord('a')]] += 1
b1[a[ord(Q[i]) - ord('a')]] += 1
b2[a[ord(P[i]) - ord('a')]][a[ord(Q[i]) - ord('a')]] -= 1
b2[a[ord(Q[i]) - ord('a')]][a[ord(P[i]) - ord('a')]] -= 1
m = a[25] + 1 # Determine the number of distinct characters
ans = m # Set 'ans' as the maximum possible value, worst case
o = [0] * (1 << m) # Create a list to store intermediate calculations
s = [0] * (1 << m) # Create a list to store intermediate calculations
return ans
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(P, Q):
# setup
a = [0] * 26 # Initialize an array to keep track of character counts
b1 = [0] * 20 # Initialize an array to store intermediate calculations
b2 = [[0] * 20 for _ in range(20)] # Initialize a 2D array to store additional calculations
n = len(P) # Determine the size of the input strings
# compute
# Update the character counts in array 'a' based on the characters in strings 'P' and 'Q'
for i in range(n):
a[ord(P[i]) - ord('a')] = a[ord(Q[i]) - ord('a')] = 1
# Calculate cumulative sums in array 'a'
for i in range(1, 26):
a[i] += a[i-1]
b1 = [0] * 20 # Reset array 'b1' to zeros
b2 = [[0] * 20 for _ in range(20)] # Reset array 'b2' to zeros
# Perform calculations for 'b1' and 'b2'
for i in range(n):
if P[i] == Q[i]:
b1[a[ord(P[i]) - ord('a')]] += 1
else:
b1[a[ord(P[i]) - ord('a')]] += 1
b1[a[ord(Q[i]) - ord('a')]] += 1
b2[a[ord(P[i]) - ord('a')]][a[ord(Q[i]) - ord('a')]] -= 1
b2[a[ord(Q[i]) - ord('a')]][a[ord(P[i]) - ord('a')]] -= 1
m = a[25] + 1 # Determine the number of distinct characters
ans = m # Set 'ans' as the maximum possible value, worst case
o = [0] * (1 << m) # Create a list to store intermediate calculations
s = [0] * (1 << m) # Create a list to store intermediate calculations
# Perform calculations using bitwise operations
for i in range(1, 1 << m):
j = 0
k = i & -i
# Find the rightmost bit that is set to 1
while not (k >> j) & 1:
j += 1
# Update intermediate calculations for 'o' and 's'
o[i] = o[i >> 1] + (i & 1)
s[i] = s[i ^ k] + b1[j]
# Perform additional calculations for 's' based on 'b2'
for k in range(j + 1, m):
if (i >> k) & 1:
s[i] += b2[j][k]
# Check if 's' is equal to 'n' and update 'ans' accordingly
if s[i] == n:
ans = min(ans, o[i])
return ans
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(P, Q):
# setup
a = [0] * 26 # Initialize an array to keep track of character counts
b1 = [0] * 20 # Initialize an array to store intermediate calculations
b2 = [[0] * 20 for _ in range(20)] # Initialize a 2D array to store additional calculations
n = len(P) # Determine the size of the input strings
# compute
# Update the character counts in array 'a' based on the characters in strings 'P' and 'Q'
for i in range(n):
a[ord(P[i]) - ord('a')] = a[ord(Q[i]) - ord('a')] = 1
# Calculate cumulative sums in array 'a'
for i in range(1, 26):
a[i] += a[i-1]
b1 = [0] * 20 # Reset array 'b1' to zeros
b2 = [[0] * 20 for _ in range(20)] # Reset array 'b2' to zeros
# Perform calculations for 'b1' and 'b2'
for i in range(n):
if P[i] == Q[i]:
b1[a[ord(P[i]) - ord('a')]] += 1
else:
b1[a[ord(P[i]) - ord('a')]] += 1
b1[a[ord(Q[i]) - ord('a')]] += 1
b2[a[ord(P[i]) - ord('a')]][a[ord(Q[i]) - ord('a')]] -= 1
b2[a[ord(Q[i]) - ord('a')]][a[ord(P[i]) - ord('a')]] -= 1
m = a[25] + 1 # Determine the number of distinct characters
ans = m # Set 'ans' as the maximum possible value, worst case
o = [0] * (1 << m) # Create a list to store intermediate calculations
s = [0] * (1 << m) # Create a list to store intermediate calculations
# Perform calculations using bitwise operations
for i in range(1, 1 << m):
j = 0
k = i & -i
# Find the rightmost bit that is set to 1
while not (k >> j) & 1:
j += 1
# Update intermediate calculations for 'o' and 's'
o[i] = o[i >> 1] + (i & 1)
s[i] = s[i ^ k] + b1[j]
# Perform additional calculations for 's' based on 'b2'
for k in range(j + 1, m):
if (i >> k) & 1:
s[i] += b2[j][k]
# Check if 's' is equal to 'n' and update 'ans' accordingly
if s[i] == n:
ans = min(ans, o[i])
return ans
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(P, Q):
# setup
a = [0] * 26 # Initialize an array to keep track of character counts
b1 = [0] * 20 # Initialize an array to store intermediate calculations
b2 = [[0] * 20 for _ in range(20)] # Initialize a 2D array to store additional calculations
n = len(P) # Determine the size of the input strings
# compute
# Update the character counts in array 'a' based on the characters in strings 'P' and 'Q'
for i in range(n):
a[ord(P[i]) - ord('a')] = a[ord(Q[i]) - ord('a')] = 1
# Calculate cumulative sums in array 'a'
for i in range(1, 26):
a[i] += a[i-1]
b1 = [0] * 20 # Reset array 'b1' to zeros
b2 = [[0] * 20 for _ in range(20)] # Reset array 'b2' to zeros
# Perform calculations for 'b1' and 'b2'
for i in range(n):
if P[i] == Q[i]:
b1[a[ord(P[i]) - ord('a')]] += 1
else:
b1[a[ord(P[i]) - ord('a')]] += 1
b1[a[ord(Q[i]) - ord('a')]] += 1
b2[a[ord(P[i]) - ord('a')]][a[ord(Q[i]) - ord('a')]] -= 1
b2[a[ord(Q[i]) - ord('a')]][a[ord(P[i]) - ord('a')]] -= 1
m = a[25] + 1 # Determine the number of distinct characters
ans = m # Set 'ans' as the maximum possible value, worst case
o = [0] * (1 << m) # Create a list to store intermediate calculations
s = [0] * (1 << m) # Create a list to store intermediate calculations
# Perform calculations using bitwise operations
for i in range(1, 1 << m):
j = 0
k = i & -i
# Find the rightmost bit that is set to 1
while not (k >> j) & 1:
j += 1
# Update intermediate calculations for 'o' and 's'
o[i] = o[i >> 1] + (i & 1)
s[i] = s[i ^ k] + b1[j]
# Perform additional calculations for 's' based on 'b2'
for k in range(j + 1, m):
if (i >> k) & 1:
s[i] += b2[j][k]
# Check if 's' is equal to 'n' and update 'ans' accordingly
if s[i] == n:
ans = min(ans, o[i])
return ans
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(P, Q):
# setup
a = [0] * 26 # Initialize an array to keep track of character counts
b1 = [0] * 20 # Initialize an array to store intermediate calculations
b2 = [[0] * 20 for _ in range(20)] # Initialize a 2D array to store additional calculations
n = len(P) # Determine the size of the input strings
# compute
# Update the character counts in array 'a' based on the characters in strings 'P' and 'Q'
for i in range(n):
a[ord(P[i]) - ord('a')] = a[ord(Q[i]) - ord('a')] = 1
# Calculate cumulative sums in array 'a'
for i in range(1, 26):
a[i] += a[i-1]
b1 = [0] * 20 # Reset array 'b1' to zeros
b2 = [[0] * 20 for _ in range(20)] # Reset array 'b2' to zeros
# Perform calculations for 'b1' and 'b2'
for i in range(n):
if P[i] == Q[i]:
b1[a[ord(P[i]) - ord('a')]] += 1
else:
b1[a[ord(P[i]) - ord('a')]] += 1
b1[a[ord(Q[i]) - ord('a')]] += 1
b2[a[ord(P[i]) - ord('a')]][a[ord(Q[i]) - ord('a')]] -= 1
b2[a[ord(Q[i]) - ord('a')]][a[ord(P[i]) - ord('a')]] -= 1
m = a[25] + 1 # Determine the number of distinct characters
ans = m # Set 'ans' as the maximum possible value, worst case
o = [0] * (1 << m) # Create a list to store intermediate calculations
s = [0] * (1 << m) # Create a list to store intermediate calculations
# Perform calculations using bitwise operations
for i in range(1, 1 << m):
j = 0
k = i & -i
# Find the rightmost bit that is set to 1
while not (k >> j) & 1:
j += 1
# Update intermediate calculations for 'o' and 's'
o[i] = o[i >> 1] + (i & 1)
s[i] = s[i ^ k] + b1[j]
# Perform additional calculations for 's' based on 'b2'
for k in range(j + 1, m):
if (i >> k) & 1:
s[i] += b2[j][k]
# Check if 's' is equal to 'n' and update 'ans' accordingly
if s[i] == n:
ans = min(ans, o[i])
return ans
The solution obtained perfect score.
A - size of the alphabet
Small tests where some letters have big but similar frequencies, N <= 17, A <= 12.