Skip to content

Commit 367eb0f

Browse files
committed
fixing formating on 451. Sort Character by Frequency
1 parent 8cd0f1b commit 367eb0f

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

problems/451. Sort Characters By Frequency.md

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ How to reverse a linked list given start, end node?
2626

2727
* `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.
2828

29-
```
29+
```python
3030
biggest = 0
3131
hold = {}
3232
```
3333

34+
3435
2. Building the Frequency Dictionary
3536

3637
* For each character (`char`) in the string `s`:
@@ -41,19 +42,21 @@ hold = {}
4142

4243
* `biggest` keeps track of the highest frequency of any character in `s` by updating it using `max(biggest, hold[char])` for each character.
4344

44-
```
45+
```python
4546
for char in s:
4647
if char not in hold:
4748
hold[char] = 1
4849
else:
4950
hold[char] += 1
50-
biggest = max(biggest, hold[char])
51-
```
52-
Example:
53-
For a string like s = "abbccc", this step would produce:
51+
biggest = max(biggest, hold[char])```
5452

55-
hold = {'a': 1, 'b': 2, 'c': 3}
56-
biggest = 3 (since the character c appears the most with a frequency of 3).
53+
+ Example:
54+
55+
* For a string like s = "abbccc", this step would produce:
56+
57+
* `hold = {'a': 1, 'b': 2, 'c': 3}`
58+
59+
* `biggest = 3 (since the character c appears the most with a frequency of 3).`
5760

5861

5962
3. Creating an Array to Hold Characters by Frequency
@@ -62,23 +65,27 @@ biggest = 3 (since the character c appears the most with a frequency of 3).
6265

6366
* For a string like s = "abbccc", this step would produce:
6467

65-
'''
68+
69+
```python
6670
array = [""] * (biggest + 1)
67-
'''
71+
```
72+
6873

6974
4. Populating the Frequency Array
7075

7176
* This loop iterates over each character (key) and its frequency (value) in the hold dictionary.
7277

7378
* 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.
7479

75-
'''
80+
81+
```python
7682
for key, value in hold.items():
77-
array[value] += key * value
78-
'''
83+
array[value] += key * value```
7984

80-
Example:
81-
Continuing with `s = "abbccc"`:
85+
86+
+ Example:
87+
88+
* Continuing with `s = "abbccc"`:
8289

8390
* `hold = {'a': 1, 'b': 2, 'c': 3}`
8491
* After this step, array will look like this:
@@ -87,6 +94,7 @@ Continuing with `s = "abbccc"`:
8794
- `array[2]` has `"bb"` because `b` appears twice.
8895
- `array[3]` has `"ccc"` because `c` appears three times.
8996

97+
9098
5. Building the Answer String in Decreasing Order of Frequency
9199

92100
* The answer variable is initialized as an empty string. It will store the final result.
@@ -95,15 +103,16 @@ Continuing with `s = "abbccc"`:
95103

96104
* Since array.pop() removes elements from the end, it effectively appends characters to answer in decreasing order of their frequency.
97105

98-
'''
106+
```python
99107
answer = ""
100108
for i in range(len(array)):
101109
answer += array.pop()
102-
return answer
103-
'''
110+
return answer```
104111

105-
Example:
106-
With array = ['', 'a', 'bb', 'ccc'], the popping process will occur as follows:
112+
113+
+ Example:
114+
115+
+ With array = ['', 'a', 'bb', 'ccc'], the popping process will occur as follows:
107116

108117
* Pop "ccc" → answer = "ccc"
109118
* Pop "bb" → answer = "cccbb"

0 commit comments

Comments
 (0)