File tree Expand file tree Collapse file tree 4 files changed +9
-24
lines changed
solution/0400-0499/0451.Sort Characters By Frequency Expand file tree Collapse file tree 4 files changed +9
-24
lines changed Original file line number Diff line number Diff line change 65
65
``` python
66
66
class Solution :
67
67
def frequencySort (self , s : str ) -> str :
68
- if not s or len (s) < 3 :
69
- return s
70
68
counter = collections.Counter(s)
71
- buckets = [[] for _ in range ( len (s) + 1 )]
69
+ buckets = collections.defaultdict( list )
72
70
for c, freq in counter.items():
73
71
buckets[freq].append(c)
74
72
res = []
@@ -86,12 +84,9 @@ class Solution:
86
84
``` java
87
85
class Solution {
88
86
public String frequencySort (String s ) {
89
- if (s == null || s. length() < 3 ) {
90
- return s;
91
- }
92
87
Map<Character , Integer > counter = new HashMap<> ();
93
- for (int i = 0 ; i < s . length(); ++ i ) {
94
- counter. put(s . charAt(i) , counter. getOrDefault(s . charAt(i) , 0 ) + 1 );
88
+ for (char c : s . toCharArray() ) {
89
+ counter. put(c , counter. getOrDefault(c , 0 ) + 1 );
95
90
}
96
91
List<Character > [] buckets = new List [s. length() + 1 ];
97
92
for (Map . Entry<Character , Integer > entry : counter. entrySet()) {
Original file line number Diff line number Diff line change @@ -51,10 +51,8 @@ Note that 'A' and 'a' are treated as two different characters.
51
51
``` python
52
52
class Solution :
53
53
def frequencySort (self , s : str ) -> str :
54
- if not s or len (s) < 3 :
55
- return s
56
54
counter = collections.Counter(s)
57
- buckets = [[] for _ in range ( len (s) + 1 )]
55
+ buckets = collections.defaultdict( list )
58
56
for c, freq in counter.items():
59
57
buckets[freq].append(c)
60
58
res = []
@@ -70,12 +68,9 @@ class Solution:
70
68
``` java
71
69
class Solution {
72
70
public String frequencySort (String s ) {
73
- if (s == null || s. length() < 3 ) {
74
- return s;
75
- }
76
71
Map<Character , Integer > counter = new HashMap<> ();
77
- for (int i = 0 ; i < s . length(); ++ i ) {
78
- counter. put(s . charAt(i) , counter. getOrDefault(s . charAt(i) , 0 ) + 1 );
72
+ for (char c : s . toCharArray() ) {
73
+ counter. put(c , counter. getOrDefault(c , 0 ) + 1 );
79
74
}
80
75
List<Character > [] buckets = new List [s. length() + 1 ];
81
76
for (Map . Entry<Character , Integer > entry : counter. entrySet()) {
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public String frequencySort (String s ) {
3
- if (s == null || s .length () < 3 ) {
4
- return s ;
5
- }
6
3
Map <Character , Integer > counter = new HashMap <>();
7
- for (int i = 0 ; i < s . length (); ++ i ) {
8
- counter .put (s . charAt ( i ) , counter .getOrDefault (s . charAt ( i ) , 0 ) + 1 );
4
+ for (char c : s . toCharArray () ) {
5
+ counter .put (c , counter .getOrDefault (c , 0 ) + 1 );
9
6
}
10
7
List <Character >[] buckets = new List [s .length () + 1 ];
11
8
for (Map .Entry <Character , Integer > entry : counter .entrySet ()) {
Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
def frequencySort (self , s : str ) -> str :
3
- if not s or len (s ) < 3 :
4
- return s
5
3
counter = collections .Counter (s )
6
- buckets = [[] for _ in range ( len ( s ) + 1 )]
4
+ buckets = collections . defaultdict ( list )
7
5
for c , freq in counter .items ():
8
6
buckets [freq ].append (c )
9
7
res = []
You can’t perform that action at this time.
0 commit comments