-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRansomNote.java
More file actions
153 lines (117 loc) · 4.93 KB
/
RansomNote.java
File metadata and controls
153 lines (117 loc) · 4.93 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package Algorithms.Hashing;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* Given two strings ransomNote and magazine, return true if ransomNote can be constructed by using the letters from magazine and false otherwise.
* Each character in ransomNote may only be used once in a magazine.
* i.e we cut the letter in the magazine book and paste it in our personal Note
* @author Srinvas Vadige, srinivas.vadige@gmail.com
* @since 09 Nov 2024
* @link 383. Ransom Note <a href="https://leetcode.com/problems/ransom-note/">LeetCode Link</a>
* @topics Hash table, String, Counting
*/
public class RansomNote {
public static void main(String[] args) {
String ransomNote = "aa", magazine = "aab";
System.out.println("canConstruct using HashMap => " + canConstructUsingHashMap(ransomNote, magazine));
System.out.println("canConstruct using int[] => " + canConstructUsingIntArray(ransomNote, magazine));
}
/**
* @TimeComplexity O(n+m)
* @SpaceComplexity O(n+m)
*/
public static boolean canConstructUsingHashMap(String ransomNote, String magazine) {
// Map<Character, Integer> rMap = ransomNote.chars().mapToObj(i->(char)i).collect(Collectors.groupingBy(Function.identity(), Collectors.summingInt(e->1)));
// Map<Character, Integer> mMap = Arrays.stream(magazine.split("")).map(s->s.charAt(0)).collect(Collectors.groupingBy(Function.identity(), Collectors.summingInt(e->1)));
Map<Character, Integer> rMap = new HashMap<>();
Map<Character, Integer> mMap = new HashMap<>();
for(int i=0; i<ransomNote.length(); i++) {
rMap.merge(ransomNote.charAt(i), 1, Integer::sum);
}
for(int i=0; i<magazine.length(); i++) {
mMap.merge(magazine.charAt(i), 1, Integer::sum);
}
for(char c: rMap.keySet()) {
if(!mMap.containsKey(c) || rMap.get(c) > mMap.get(c)) {
return false;
}
}
return true;
}
public static boolean canConstructUsingHashMap2(String ransomNote, String magazine) {
Map<String, Integer> rMap= Arrays.stream(ransomNote.split("")).collect(Collectors.groupingBy(Function.identity(), Collectors.summingInt(_->1)));
Map<String, Integer> mMap= Arrays.stream(magazine.split("")).collect(Collectors.groupingBy(Function.identity(), Collectors.summingInt(_->1)));
for(Map.Entry<String, Integer> re: rMap.entrySet() ) {
if(mMap.getOrDefault(re.getKey(), 0) < re.getValue())
return false;
}
return true;
}
/**
* @TimeComplexity O(n+m)
* @SpaceComplexity O(1)
*/
public static boolean canConstructUsingIntArray(String ransomNote, String magazine) {
int[] rFreq = new int[26];
int[] mFreq = new int[26];
for(int i=0; i<ransomNote.length(); i++) {
rFreq[ransomNote.charAt(i)-'a']++;
}
for(int i=0; i<magazine.length(); i++) {
mFreq[magazine.charAt(i)-'a']++;
}
for(int i=0; i<26; i++) {
if(rFreq[i] > mFreq[i]) {
return false;
}
}
return true;
}
/**
* @TimeComplexity O(n+m)
* @SpaceComplexity O(n+m), because we are using str.toCharArray()
*/
public static boolean canConstructUsingIntArray2(String ransomNote, String magazine) {
int[] count = new int[26];
for (char c : ransomNote.toCharArray())
count[c - 'a']++;
for (char c : magazine.toCharArray())
count[c - 'a']--;
for (int i : count) {
if (i > 0) return false;
}
return true;
}
public static boolean canConstructUsingIntArray3(String ransomNote, String magazine) {
int[] count = new int[26];
for (char c : magazine.toCharArray())
count[c - 'a']++;
for (char c : ransomNote.toCharArray())
if(count[c-'a']-- == 0) return false; // i.e before decrement
return true;
}
public static boolean canConstructUsingIntArray4(String ransomNote, String magazine) {
int[] check = new int[26];
for (char c : ransomNote.toCharArray()) {
int index = magazine.indexOf(c, check[c % 26]);
if (index < 0)
return false;
check[c % 26] = index+1;
}
return true;
}
public boolean canConstructBruteForce(String ransomNote, String magazine) {
for (int i = 0; i < ransomNote. length(); i++) {
char r= ransomNote. charAt(i);
int matchingIndex = magazine.indexOf(r);
if (matchingIndex == -1)
return false;
magazine = magazine.substring(0, matchingIndex)
+ magazine. substring(matchingIndex + 1); // skip that char and we can have duplicate chars
}
return true;
}
}