-3

I need a piece of code to find the minimum occurrences of a letter in a given string.

I cannot use the min() function. I already have a function to find the max occurrences of a letter. What modifications would I need to make to the code?

def getMaxOccuringChar(str):

    ASCII_SIZE = 256
    count = [0] * ASCII_SIZE
    max = -1
    c = ''
    for i in str:
        count[ord(i)] += 1;

    for i in str:
        if max < count[ord(i)]:
            max = count[ord(i)]
            c = i

    return c
print(getMaxOccuringChar(""))
quamrana
  • 37,849
  • 12
  • 53
  • 71
  • 3
    Even if you can’t use the min() or max() functions you shouldn’t use min or max as variable names. – quamrana Apr 19 '20 at 20:29
  • Does this answer your question? [How can I count the occurrences of a list item?](https://stackoverflow.com/questions/2600191/how-can-i-count-the-occurrences-of-a-list-item) – wovano Apr 19 '20 at 20:37
  • What you ask doesn't make sense as it is written. The occurrence of a letter in a string is a value, not a range for which you can find min and max. What you want to find is the _minimum number of occurrences_ or the _minimum frequency_ among all letters' frequencies. You could say _minimal frequency_ (in place of minimum), meaning there might be more than one letter in the string that has that frequency. – user2314737 Apr 19 '20 at 20:43

1 Answers1

1

You can replace max = -1 with min = len(str) and in your second for loop you replace if max < count[ord(i)] with if min > count[ord(i)].

Erich
  • 1,838
  • 16
  • 20