File tree Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public String [] getFolderNames (String [] names ) {
3
+ int n = names .length ;
4
+ Map <String , Integer > map = new HashMap <>();
5
+ String [] ans = new String [n ];
6
+ for (int i = 0 ; i < n ; i ++) {
7
+ if (map .containsKey (names [i ])) {
8
+ Integer val = map .get (names [i ]);
9
+ StringBuilder sb = new StringBuilder (names [i ]);
10
+ sb .append ('(' ).append (val ).append (')' );
11
+ while (map .containsKey (sb .toString ())) {
12
+ val ++;
13
+ sb = new StringBuilder (names [i ]);
14
+ sb .append ('(' ).append (val ).append (')' );
15
+ }
16
+ ans [i ] = sb .toString ();
17
+ map .put (sb .toString (), 1 );
18
+ map .put (names [i ], val + 1 );
19
+ }
20
+ else {
21
+ ans [i ] = names [i ];
22
+ map .put (names [i ], 1 );
23
+ }
24
+ }
25
+ return ans ;
26
+ }
27
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public List <String > printVertically (String s ) {
3
+ String [] words = s .split ("\\ s+" );
4
+ int longestWordLength = 0 ;
5
+ for (String word : words ) {
6
+ longestWordLength = Math .max (longestWordLength , word .length ());
7
+ }
8
+ List <String > ans = new ArrayList <>();
9
+ for (int i = 0 ; i < longestWordLength ; i ++) {
10
+ StringBuilder sb = new StringBuilder ();
11
+ for (String word : words ) {
12
+ sb .append (i < word .length () ? word .charAt (i ) : " " );
13
+ }
14
+ while (sb .charAt (sb .length () - 1 ) == ' ' ) {
15
+ sb .deleteCharAt (sb .length () - 1 );
16
+ }
17
+ if (sb .length () > 0 ) {
18
+ ans .add (sb .toString ());
19
+ }
20
+ }
21
+ return ans ;
22
+ }
23
+ }
You can’t perform that action at this time.
0 commit comments