forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCount Words Obtained After Adding a Letter.java
35 lines (32 loc) · 1.21 KB
/
Count Words Obtained After Adding a Letter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution {
public int wordCount(String[] startWords, String[] targetWords) {
int n = startWords.length;
int count = 0;
Set<String> set = new HashSet<>();
//1. store lexicographically sorted letters of startword in set
for(String start: startWords){
char[] sAr = start.toCharArray();
Arrays.sort(sAr);
set.add(new String(sAr));
}
int m = targetWords.length;
boolean ans = false;
for(int i = 0; i < m; i++){
//2. sort lexicographically letters of targetword and store in new string s
char[] tAr = targetWords[i].toCharArray();
Arrays.sort(tAr);
int k = tAr.length;
String s = String.valueOf(tAr);
ans = false;
for(int j = 0; j < k; j++){
//3. make a new string by omitting one letter from word and check if it is present in set than increase count value
String str = s.substring(0,j) + s.substring(j+1);
if(set.contains(str)){
count++;
break;
}
}
}
return count;
}
}