Untitled

mail@pastecode.io avatar
unknown
python
a month ago
574 B
1
Indexable
Never
def most_frequent_char_replace(n, str_input, x):
    if n == 0:
        print("NULL")
        return

    char_count = {}
    max_freq_char = ""
    max_freq = 0

    for char in str_input:
        if char in char_count:
            char_count[char] += 1
        else:
            char_count[char] = 1

        if char_count[char] > max_freq or (char_count[char] == max_freq and char < max_freq_char):
            max_freq_char = char
            max_freq = char_count[char]

    modified_str = str_input.replace(max_freq_char, x)
    print(modified_str)
Leave a Comment