Skip to content

Commit afba1db

Browse files
committed
Added 2 solutions
1 parent f6fbc3b commit afba1db

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

Medium/Making File Names Unique.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}

Medium/Print Words Vertically.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
}

0 commit comments

Comments
 (0)