File tree 1 file changed +32
-0
lines changed
1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string[] } votes
3
+ * @return {string }
4
+ */
5
+ var rankTeams = function ( votes ) {
6
+ if ( votes . length === 1 ) {
7
+ return votes [ 0 ] ;
8
+ }
9
+
10
+ let result = new Array ( 26 ) ;
11
+ for ( let i = 0 ; i < result . length ; i ++ ) {
12
+ result [ i ] = new Array ( 26 ) . fill ( 0 ) ;
13
+ }
14
+ for ( let i = 0 ; i < votes . length ; i ++ ) {
15
+ for ( let j = 0 ; j < votes [ i ] . length ; j ++ ) {
16
+ const code = votes [ i ] . charCodeAt ( j ) - 65 ;
17
+ result [ code ] [ j ] ++ ;
18
+ }
19
+ }
20
+
21
+ result = result . map ( ( list , i ) => ( { k : String . fromCharCode ( i + 65 ) , v : list } ) ) ;
22
+ result . sort ( ( a , b ) => {
23
+ for ( let i = 0 ; i < 26 ; i ++ ) {
24
+ if ( a . v [ i ] != b . v [ i ] ) {
25
+ return b . v [ i ] - a . v [ i ] ;
26
+ }
27
+ }
28
+ return a . k < b . k ? - 1 : 1 ;
29
+ } ) ;
30
+
31
+ return result . map ( ( { k} ) => k ) . slice ( 0 , votes [ 0 ] . length ) . join ( '' ) ;
32
+ } ;
You can’t perform that action at this time.
0 commit comments