Skip to content

Commit ef68371

Browse files
committed
Updated solutions
1 parent 807606e commit ef68371

File tree

7 files changed

+40
-35
lines changed

7 files changed

+40
-35
lines changed
Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,38 @@
11
package _1_2_Check_Permutations;
22

3-
// Should ask interviewer if String is ASCII or Unicode (We assume ASCII)
3+
import java.util.HashMap;
44

5-
// Algorithm:
6-
// Count the different characters using an array (as shown below) or a HashMap.
7-
// For each String, we can save the count of each character in an array, then compare these 2 arrays.
8-
// However, instead of using 2 arrays, we can do it with 1, as shown below.
5+
// Should ask interviewer if String is ASCII or Unicode (We assume ASCII)
96

7+
// Algorithm
8+
//
9+
// Do a quick check on string lengths. If the lengths differ, the strings can't be anagrams
10+
// Save `String s` as `HashMap` of character counts
11+
// For `String t`, see if it can be made up of the characters of the previous `HashMap`
1012

1113
public class CheckPermutations {
12-
private static final int NUM_ASCII_CHARS = 256; // number of ASCII characters
13-
public static boolean isPermutation(String s1, String s2) {
14-
if (s1.length() != s2.length()) {
14+
15+
private static final int NUM_ASCII_CHARS = 256;
16+
17+
public static boolean isPermutation(String s, String t) {
18+
if (s.length() != t.length()) {
1519
return false;
1620
}
17-
int[] letters = new int[NUM_ASCII_CHARS];
18-
for (int i = 0; i < s1.length(); i++) {
19-
char ch = s1.charAt(i);
20-
letters[ch]++;
21+
HashMap<Character, Integer> map = new HashMap<>(NUM_ASCII_CHARS);
22+
for (int i = 0; i < s.length(); i++) {
23+
char ch = s.charAt(i);
24+
map.merge(ch, 1, Integer::sum);
2125
}
22-
for (int i = 0; i < s2.length(); i++) {
23-
char ch = s2.charAt(i);
24-
if (--letters[ch] < 0) {
26+
for (int i = 0; i < t.length(); i++) {
27+
char ch = t.charAt(i);
28+
if (!map.containsKey(ch) || map.get(ch) == 0) {
2529
return false;
2630
}
31+
map.merge(ch, -1, Integer::sum);
2732
}
2833
return true;
2934
}
3035
}
3136

32-
3337
// Time Complexity: O(n)
3438
// Space Complexity: O(1)
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
The code is an efficient way to check to see if n is a power of 2
1+
Assuming n is positive, the code is an efficient way to check to see if n is a power of 2
2+

Chp. 08 - Recursion and Dynamic Programming/_8_04_Power_Set/PowerSet.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
package _8_04_Power_Set;
22

3-
import java.util.ArrayList;
3+
import java.util.*;
44

55
public class PowerSet {
6-
public static ArrayList<ArrayList<Integer>> getSubsets(ArrayList<Integer> set) {
7-
ArrayList<ArrayList<Integer>> powerSet = new ArrayList<>();
6+
public static List<List<Integer>> getSubsets(List<Integer> set) {
7+
List<List<Integer>> powerSet = new ArrayList<>();
88
int max = (int) Math.pow(2, set.size());
99
for (int i = 0; i < max; i++) { // this is the main trick
10-
ArrayList<Integer> subset = createSubset(set, i);
10+
List<Integer> subset = createSubset(set, i);
1111
powerSet.add(subset);
1212
}
1313
return powerSet;
1414
}
1515

16-
private static ArrayList<Integer> createSubset(ArrayList<Integer> set, int num) {
17-
ArrayList<Integer> subset = new ArrayList<>();
16+
private static List<Integer> createSubset(List<Integer> set, int num) {
17+
List<Integer> subset = new ArrayList<>();
1818
int indexInSet = 0;
1919
while (num != 0) {
2020
if ((num & 1) == 1) {

Chp. 08 - Recursion and Dynamic Programming/_8_04_Power_Set/Tester.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package _8_04_Power_Set;
22

3-
import java.util.ArrayList;
3+
import java.util.*;
44

55
public class Tester {
66
public static void main(String[] args) {
77
System.out.println("*** Test 8.4: Power Set\n");
8-
ArrayList<Integer> set = new ArrayList<>();
8+
List<Integer> set = new ArrayList<>();
99
set.add(3);
1010
test(set);
1111
set.add(4);
@@ -14,8 +14,8 @@ public static void main(String[] args) {
1414
test(set);
1515
}
1616

17-
private static void test(ArrayList<Integer> set) {
18-
ArrayList<ArrayList<Integer>> subsets = PowerSet.getSubsets(set);
17+
private static void test(List<Integer> set) {
18+
List<List<Integer>> subsets = PowerSet.getSubsets(set);
1919
System.out.println("Original set: " + set);
2020
System.out.println("Subsets: " + subsets + "\n");
2121
}

Chp. 08 - Recursion and Dynamic Programming/_8_08_Permutations_with_Dups/Tester.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package _8_08_Permutations_with_Dups;
22

3-
import java.util.ArrayList;
3+
import java.util.*;
44

55
public class Tester {
66
public static void main(String[] args) {
@@ -10,7 +10,7 @@ public static void main(String[] args) {
1010
}
1111

1212
private static void test(String str) {
13-
ArrayList<String> permutations = PermutationsWithDups.getPermutations(str);
13+
List<String> permutations = PermutationsWithDups.getPermutations(str);
1414
System.out.println("Original string: " + str);
1515
System.out.println("Permutations: " + permutations + "\n");
1616
}

Chp. 10 - Sorting and Searching/_10_09_Sorted_Matrix_Search/SortedMatrixSearch.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public class SortedMatrixSearch {
1313
// - Always either move down (eliminating current row), or move left (eliminating current col)
1414
// - Alternate solution: start in bottom left and move to top right
1515
public static boolean findElement(int[][] grid, int elem) {
16+
if (grid == null || grid.length == 0 || grid[0].length == 0) {
17+
return false;
18+
}
1619
int rows = grid.length;
1720
int cols = grid[0].length;
1821

Common/common/ListFunctions.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,11 @@ public static Node deleteNode(Node head, int d) { // "d" is the node we want to
3939

4040
/* Returns size of linked list */
4141
public static int calculateSize(Node head) {
42-
if (head == null) {
43-
return 0;
44-
}
45-
int size = 1;
4642
Node n = head;
47-
while (n.next != null) {
48-
n = n.next;
43+
int size = 0;
44+
while (n != null) {
4945
size++;
46+
n = n.next;
5047
}
5148
return size;
5249
}

0 commit comments

Comments
 (0)