From 8cd0f1baebbfa0da676217d852deef1aa737866c Mon Sep 17 00:00:00 2001 From: YonashPetit Date: Tue, 1 Oct 2024 15:16:34 -0400 Subject: [PATCH 1/3] adding 451. Sort Characters By Frequency.md page --- problems/451. Sort Characters By Frequency.md | 148 ++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 problems/451. Sort Characters By Frequency.md diff --git a/problems/451. Sort Characters By Frequency.md b/problems/451. Sort Characters By Frequency.md new file mode 100644 index 000000000..e6ecdc9f1 --- /dev/null +++ b/problems/451. Sort Characters By Frequency.md @@ -0,0 +1,148 @@ +## Problem +https://leetcode.com/problems/sort-characters-by-frequency/description/ + +## Problem Description +``` +Given a string s, sort it in decreasing order based on the frequency of the characters. The frequency of a character is the number of times it appears in the string. + +Return the sorted string. If there are multiple answers, return any of them. + +Example: + +Input: s = "tree" +Output: "eert" +Explanation: 'e' appears twice while 'r' and 't' both appear once. +So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer. + +``` + +## Solution +Map `Hash Map` from left to right, during traverse, group nodes in k, then reverse each group. +How to reverse a linked list given start, end node? + +1. Initializing Variables + +* `biggest`: This variable is used to keep track of the highest frequency of any character in the string s. + +* `hold`: This is a dictionary (hash map) used to store the frequency of each character in the string. The keys represent the characters, and the values represent the number of times they appear. + +``` +biggest = 0 +hold = {} +``` + +2. Building the Frequency Dictionary + +* For each character (`char`) in the string `s`: + +* If the character is not already in the dictionary `hold`, it is added to the dictionary with a count of `1`. + +* If the character already exists in the dictionary, its count is incremented by `1`. + +* `biggest` keeps track of the highest frequency of any character in `s` by updating it using `max(biggest, hold[char])` for each character. + +``` +for char in s: + if char not in hold: + hold[char] = 1 + else: + hold[char] += 1 + biggest = max(biggest, hold[char]) +``` +Example: +For a string like s = "abbccc", this step would produce: + +hold = {'a': 1, 'b': 2, 'c': 3} +biggest = 3 (since the character c appears the most with a frequency of 3). + + +3. Creating an Array to Hold Characters by Frequency + +* The `array` is created with `biggest + 1` empty strings. The purpose of this array is to group characters by their frequency. + +* For a string like s = "abbccc", this step would produce: + +''' +array = [""] * (biggest + 1) +''' + +4. Populating the Frequency Array + +* This loop iterates over each character (key) and its frequency (value) in the hold dictionary. + +* For each character, it places that character (repeated value times) at the index corresponding to its frequency in the array. The string of characters is concatenated at the corresponding index in array. + +''' +for key, value in hold.items(): + array[value] += key * value +''' + +Example: +Continuing with `s = "abbccc"`: + +* `hold = {'a': 1, 'b': 2, 'c': 3}` +* After this step, array will look like this: + - `array = ['', 'a', 'bb', 'ccc']` + - `array[1]` has `"a"` because `a` appears once. + - `array[2]` has `"bb"` because `b` appears twice. + - `array[3]` has `"ccc"` because `c` appears three times. + +5. Building the Answer String in Decreasing Order of Frequency + +* The answer variable is initialized as an empty string. It will store the final result. + +* A loop iterates through the array, and in each iteration, it pops the last element of the array (i.e., removes it from the end) and appends it to answer. + +* Since array.pop() removes elements from the end, it effectively appends characters to answer in decreasing order of their frequency. + +''' +answer = "" +for i in range(len(array)): + answer += array.pop() +return answer +''' + +Example: +With array = ['', 'a', 'bb', 'ccc'], the popping process will occur as follows: + +* Pop "ccc" → answer = "ccc" +* Pop "bb" → answer = "cccbb" +* Pop "a" → answer = "cccbbba" + +#### Complexity Analysis +- *Time Complexity:* `O(n) - n is number of characters in s` +- *Space Complexity:* `O(n)` + +## Summary of Key Operations: +1. Build a frequency dictionary. +2. Create an array where the index represents the frequency of characters. +3. Populate the array with the characters at the index corresponding to their frequency. +4. Build the result by popping elements from the array in reverse order. + +## Code (`Python3`) +*Python3 Code* +```python +class Solution: + def frequencySort(self, s: str) -> str: + biggest = 0 + hold = {} + + + for char in s: + if char not in hold: + hold[char] = 1 + else: + hold[char] += 1 + biggest = max(biggest, hold[char]) + + + array = [""]*(biggest+1) + for key, value in hold.items(): + array[value] += key*value + + answer = "" + for i in range(len(array)): + answer += array.pop() + + return answer +''' \ No newline at end of file From 367eb0f399765e31c65979f23116afcbcacf9705 Mon Sep 17 00:00:00 2001 From: YonashPetit Date: Tue, 1 Oct 2024 15:25:59 -0400 Subject: [PATCH 2/3] fixing formating on 451. Sort Character by Frequency --- problems/451. Sort Characters By Frequency.md | 49 +++++++++++-------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/problems/451. Sort Characters By Frequency.md b/problems/451. Sort Characters By Frequency.md index e6ecdc9f1..cc5f7de72 100644 --- a/problems/451. Sort Characters By Frequency.md +++ b/problems/451. Sort Characters By Frequency.md @@ -26,11 +26,12 @@ How to reverse a linked list given start, end node? * `hold`: This is a dictionary (hash map) used to store the frequency of each character in the string. The keys represent the characters, and the values represent the number of times they appear. -``` +```python biggest = 0 hold = {} ``` + 2. Building the Frequency Dictionary * For each character (`char`) in the string `s`: @@ -41,19 +42,21 @@ hold = {} * `biggest` keeps track of the highest frequency of any character in `s` by updating it using `max(biggest, hold[char])` for each character. -``` +```python for char in s: if char not in hold: hold[char] = 1 else: hold[char] += 1 - biggest = max(biggest, hold[char]) -``` -Example: -For a string like s = "abbccc", this step would produce: + biggest = max(biggest, hold[char])``` -hold = {'a': 1, 'b': 2, 'c': 3} -biggest = 3 (since the character c appears the most with a frequency of 3). ++ Example: + +* For a string like s = "abbccc", this step would produce: + +* `hold = {'a': 1, 'b': 2, 'c': 3}` + +* `biggest = 3 (since the character c appears the most with a frequency of 3).` 3. Creating an Array to Hold Characters by Frequency @@ -62,9 +65,11 @@ biggest = 3 (since the character c appears the most with a frequency of 3). * For a string like s = "abbccc", this step would produce: -''' + +```python array = [""] * (biggest + 1) -''' +``` + 4. Populating the Frequency Array @@ -72,13 +77,15 @@ array = [""] * (biggest + 1) * For each character, it places that character (repeated value times) at the index corresponding to its frequency in the array. The string of characters is concatenated at the corresponding index in array. -''' + +```python for key, value in hold.items(): - array[value] += key * value -''' + array[value] += key * value``` -Example: -Continuing with `s = "abbccc"`: + ++ Example: + +* Continuing with `s = "abbccc"`: * `hold = {'a': 1, 'b': 2, 'c': 3}` * After this step, array will look like this: @@ -87,6 +94,7 @@ Continuing with `s = "abbccc"`: - `array[2]` has `"bb"` because `b` appears twice. - `array[3]` has `"ccc"` because `c` appears three times. + 5. Building the Answer String in Decreasing Order of Frequency * The answer variable is initialized as an empty string. It will store the final result. @@ -95,15 +103,16 @@ Continuing with `s = "abbccc"`: * Since array.pop() removes elements from the end, it effectively appends characters to answer in decreasing order of their frequency. -''' +```python answer = "" for i in range(len(array)): answer += array.pop() -return answer -''' +return answer``` -Example: -With array = ['', 'a', 'bb', 'ccc'], the popping process will occur as follows: + ++ Example: + ++ With array = ['', 'a', 'bb', 'ccc'], the popping process will occur as follows: * Pop "ccc" → answer = "ccc" * Pop "bb" → answer = "cccbb" From 2854f9e9f56b89c15f577c0a0d4fa3a7aba82f32 Mon Sep 17 00:00:00 2001 From: YonashPetit Date: Tue, 1 Oct 2024 15:27:20 -0400 Subject: [PATCH 3/3] fixing formating on 451. Sort Character by Frequency --- problems/451. Sort Characters By Frequency.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/problems/451. Sort Characters By Frequency.md b/problems/451. Sort Characters By Frequency.md index cc5f7de72..de6e5bb9f 100644 --- a/problems/451. Sort Characters By Frequency.md +++ b/problems/451. Sort Characters By Frequency.md @@ -48,7 +48,8 @@ for char in s: hold[char] = 1 else: hold[char] += 1 - biggest = max(biggest, hold[char])``` + biggest = max(biggest, hold[char]) +``` + Example: @@ -80,7 +81,8 @@ array = [""] * (biggest + 1) ```python for key, value in hold.items(): - array[value] += key * value``` + array[value] += key * value +``` + Example: @@ -107,7 +109,8 @@ for key, value in hold.items(): answer = "" for i in range(len(array)): answer += array.pop() -return answer``` +return answer +``` + Example: