Skip to content

Instantly share code, notes, and snippets.

@ingted
Created April 10, 2022 10:46
Show Gist options
  • Save ingted/c42d2739ae6915efb9abdf261d6e9459 to your computer and use it in GitHub Desktop.
Save ingted/c42d2739ae6915efb9abdf261d6e9459 to your computer and use it in GitHub Desktop.
getMaxOccurrences
#!/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