Created
April 10, 2022 10:46
-
-
Save ingted/c42d2739ae6915efb9abdf261d6e9459 to your computer and use it in GitHub Desktop.
getMaxOccurrences
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/python3 | |
| import math | |
| import os | |
| import random | |
| import re | |
| import sys | |
| # | |
| # Complete the 'getMaxOccurrences' function below. | |
| # | |
| # The function is expected to return an INTEGER. | |
| # The function accepts following parameters: | |
| # 1. STRING components | |
| # 2. INTEGER minLength | |
| # 3. INTEGER maxLength | |
| # 4. INTEGER maxUnique | |
| # | |
| def countDis(str): | |
| s = set(str) | |
| return len(s) | |
| stat = {} | |
| def incr(k): | |
| if k in stat: | |
| stat[k] += 1 | |
| else: | |
| stat[k] = 1 | |
| def getMaxOccurrences(components, minLength, maxLength, maxUnique): | |
| n = len(components) | |
| for l in range(minLength,maxLength + 1): | |
| for s in range(0,n - l + 1): | |
| sub = components[s:s+l] | |
| if countDis(sub) <= maxUnique: | |
| incr(sub) | |
| if len(stat) == 0: | |
| return 0 | |
| else: | |
| return max(stat.values()) | |
| if __name__ == '__main__': | |
| fptr = open(os.environ['OUTPUT_PATH'], 'w') | |
| components = input() | |
| minLength = int(input().strip()) | |
| maxLength = int(input().strip()) | |
| maxUnique = int(input().strip()) | |
| result = getMaxOccurrences(components, minLength, maxLength, maxUnique) | |
| fptr.write(str(result) + '\n') | |
| fptr.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment