-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCountWordsObtainedAfterAddingALetter.java
112 lines (99 loc) · 3.55 KB
/
CountWordsObtainedAfterAddingALetter.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*https://leetcode.com/problems/count-words-obtained-after-adding-a-letter/*/
//hash solution
class Solution {
public int wordCount(String[] startWords, String[] targetWords) {
HashSet<String> set = new HashSet<>();
//add all start words in sorted order to check whether targetWords can be formed
for(String s: startWords){
char[] chars = s.toCharArray();
Arrays.sort(chars);
set.add(new String(chars));
}
int count=0;
for(String t: targetWords){
char[] chars = t.toCharArray();
Arrays.sort(chars);
String newWord = new String(chars);
int wordLen = newWord.length();
for(int i=0;i<wordLen;i++){
//removing one char and checking if they exist in my hashSet
String str = newWord.substring(0,i) + newWord.substring(i+1);
if(set.contains(str))
{
count++;
break;
}
}
}
return count;
}
}
// TLE
// class Solution {
// public int wordCount(String[] startWords, String[] targetWords) {
// int s = startWords.length, t = targetWords.length, i, j, diff, count = 0;
// int[][] hash = new int[t][26];
// int[] sHash = new int[26];
// Set<String> set = new HashSet<String>();
// for (i = 0; i < t; ++i)
// for (j = 0; j < targetWords[i].length(); ++j)
// ++hash[i][targetWords[i].charAt(j)-'a'];
// for (i = 0; i < t; ++i)
// {
// for (String word : startWords)
// {
// diff = 0;
// if (targetWords[i].length() == word.length()+1)
// {
// sHash = new int[26];
// for (j = 0; j < word.length(); ++j)
// ++sHash[word.charAt(j)-'a'];
// for (j = 0; j < 26; ++j)
// {
// if (sHash[j] != hash[i][j] && sHash[j] == 0)
// {
// ++diff;
// //if (sHash[j] > 0 && hash[i][j] > 0) ++diff;
// }
// if (diff > 1) break;
// }
// if (diff == 1)
// {
// //System.out.println(word+" -> "+targetWords[i]);
// //if (!set.contains(targetWords[i]))
// //{
// ++count; break;
// //set.add(targetWords[i]);
// //}
// }
// }
// }
// }
// return count;
// }
// }
//bit solution
class Solution {
public int wordCount(String[] startWords, String[] targetWords) {
int ans = 0, m = 0;
Set<Integer> hs = new HashSet<>();
for(String w: startWords){
m = 0;
for(char c: w.toCharArray()) {
m |= (1<< (c-'a'));
}
hs.add(m);
}
for(String w: targetWords){
m = 0;
for(char c: w.toCharArray()) m |= (1<< (c-'a'));
for(char c: w.toCharArray()){
if(hs.contains(m - (1<< (c-'a')))){
ans++;
break;
}
}
}
return ans;
}
}