How would you implement a program to determine the frequency of each letter in a given string, such as "asrfrfgg," producing an output like "a1s1r2f2g2," while considering the time and space complexity of your solution?
Asked in the Round 4: Technical - Bar Raiser stage.
Implement letter_frequency(s), accepting a string and returning one concatenated string. Each distinct character must appear once, ordered by its first appearance, followed immediately by its frequency. Treat characters exactly as provided. Return an empty string for empty input.
def letter_frequency(s):