-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountwords.java
36 lines (30 loc) · 1.19 KB
/
countwords.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
36
import java.util.Arrays;
import java.util.HashSet;
public class countwords {
public static int wordCount (String[] startWords, String[] targetWords) {
HashSet<String> set = new HashSet<>();
int count=0;
for(String startWord:startWords) {
char[] wordChars = startWord.toCharArray();
Arrays.sort(wordChars);
set.add(new String(wordChars));
}
for(String word:targetWords){
for(int i=0;i<word.length();i++){
String modifiedWord= word.substring(0,i)+word.substring(i+1,word.length());
char[] wordChars = modifiedWord.toCharArray();
Arrays.sort(wordChars);
if(set.contains(new String(wordChars))){
count++;
break;
}
}
}
return count;
}
public static void main(String[] args) {
String []startWords={"ant","act","tack"};
String []targetWords={"tack","act","acti"};
System.out.println(wordCount(startWords, targetWords));
}
}