Naming a Company LeetCode solution in Java

1
class Solution {
2
public long distinctNames(String[] ideas) {
3
// HashSet + String Manipulation; TC: O(26*26*n); SC: O(26*n)
4
HashSet<String>[] arr = new HashSet[26];
5
for (int i = 0; i < 26; i++) {
6
arr[i] = new HashSet<>();
7
}
8
for (String s : ideas) {
9
arr[s.charAt(0) - 'a'].add(s.substring(1));
10
}
11
long ans = 0, cnt;
12
for (int i = 0; i < 26; i++) {
13
for (int j = i + 1; j < 26; j++) {
14
cnt = 0;
15
for (String str : arr[j]) {
16
if (arr[i].contains(str)) cnt++;
17
}
18
ans += 2 * (arr[i].size() - cnt) * (arr[j].size() - cnt);
19
}
20
}
21
return ans;
22
}
23
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0