from collections import defaultdictdef is_associated_substring(str1, str2):len1, len2 = len(str1), len(str2)if len1 > len2:return -1# Frequency dictionary for str1freq1 = defaultdict(int)for c in str1:freq1[c] += 1# Frequency dictionary for the initial window in str2freq2 = defaultdict(int)for i in range(len1):freq2[str2[i]] += 1# Check initial windowif freq1 == freq2:return 0# Slide the window through str2for i in range(1, len2 - len1 + 1):# Remove the leftmost character of the previous windowleft_char = str2[i - 1]if freq2[left_char] == 1:del freq2[left_char]else:freq2[left_char] -= 1# Add the new character to the rightright_char = str2[i + len1 - 1]freq2[right_char] += 1# Check if current window matches str1's frequencyif freq1 == freq2:return ireturn -1# Read inputinput_str = input.stripstr1, str2 = input_str.split# Get the resultresult = is_associated_substring(str1, str2)print(result)摘要:from collections import defaultdictdef is_associated_substring(str1, str2):len1, len2 = len(str1), len(str2)if len1 > len2:return
来源:金典教育