Skip to content

Commit 9728a5b

Browse files
authored
Create Number of Pairs of Strings With Concatenation Equal to Target.java
1 parent 91581fb commit 9728a5b

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int numOfPairs(String[] nums, String target) {
3+
Map<String, Integer> map = new HashMap<>();
4+
int count = 0;
5+
for (String num : nums) {
6+
if (target.startsWith(num)) {
7+
String end = target.substring(num.length());
8+
count += map.getOrDefault(end, 0);
9+
}
10+
if (target.endsWith(num)) {
11+
String start = target.substring(0, target.length() - num.length());
12+
count += map.getOrDefault(start, 0);
13+
}
14+
map.put(num, map.getOrDefault(num, 0) + 1);
15+
}
16+
return count;
17+
}
18+
}

0 commit comments

Comments
 (0)