You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: problems/451. Sort Characters By Frequency.md
+29-20Lines changed: 29 additions & 20 deletions
Original file line number
Diff line number
Diff line change
@@ -26,11 +26,12 @@ How to reverse a linked list given start, end node?
26
26
27
27
*`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.
28
28
29
-
```
29
+
```python
30
30
biggest =0
31
31
hold = {}
32
32
```
33
33
34
+
34
35
2. Building the Frequency Dictionary
35
36
36
37
* For each character (`char`) in the string `s`:
@@ -41,19 +42,21 @@ hold = {}
41
42
42
43
*`biggest` keeps track of the highest frequency of any character in `s` by updating it using `max(biggest, hold[char])` for each character.
43
44
44
-
```
45
+
```python
45
46
for char in s:
46
47
if char notin hold:
47
48
hold[char] =1
48
49
else:
49
50
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])```
54
52
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).`
57
60
58
61
59
62
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).
62
65
63
66
* For a string like s ="abbccc", this step would produce:
64
67
65
-
'''
68
+
69
+
```python
66
70
array = [""] * (biggest +1)
67
-
'''
71
+
```
72
+
68
73
69
74
4. Populating the Frequency Array
70
75
71
76
* This loop iterates over each character (key) and its frequency (value) in the hold dictionary.
72
77
73
78
* 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.
74
79
75
-
'''
80
+
81
+
```python
76
82
for key, value in hold.items():
77
-
array[value] += key * value
78
-
'''
83
+
array[value] += key * value```
79
84
80
-
Example:
81
-
Continuing with `s = "abbccc"`:
85
+
86
+
+ Example:
87
+
88
+
* Continuing with`s = "abbccc"`:
82
89
83
90
*`hold = {'a': 1, 'b': 2, 'c': 3}`
84
91
* After this step, array will look like this:
@@ -87,6 +94,7 @@ Continuing with `s = "abbccc"`:
87
94
-`array[2]` has `"bb"` because `b` appears twice.
88
95
-`array[3]` has `"ccc"` because `c` appears three times.
89
96
97
+
90
98
5. Building the Answer String in Decreasing Order of Frequency
91
99
92
100
* The answer variable is initialized as an empty string. It will store the final result.
@@ -95,15 +103,16 @@ Continuing with `s = "abbccc"`:
95
103
96
104
* Since array.pop() removes elements from the end, it effectively appends characters to answer in decreasing order of their frequency.
97
105
98
-
'''
106
+
```python
99
107
answer =""
100
108
for i inrange(len(array)):
101
109
answer += array.pop()
102
-
return answer
103
-
'''
110
+
return answer```
104
111
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:
0 commit comments