Skip to content

Commit 8ea7134

Browse files
committed
Updated Chp.1 Solutions
1 parent 24b17b5 commit 8ea7134

File tree

5 files changed

+21
-8
lines changed

5 files changed

+21
-8
lines changed

Chp. 01 - Arrays and Strings/_1_1_Is_Unique/IsUnique.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
// Should ask interviewer if String is ASCII or Unicode (We assume ASCII)
66

77
public class IsUnique {
8-
private static final int NUM_CHARS = 256; // number of ASCII characters
8+
private static final int NUM_ASCII_CHARS = 256; // number of ASCII characters
99

1010
public static boolean uniqueCharacters(String str) {
11-
if (str.length() > NUM_CHARS) {
11+
if (str.length() > NUM_ASCII_CHARS) {
1212
return false;
1313
}
14-
HashSet<Character> mySet = new HashSet<>(NUM_CHARS);
14+
HashSet<Character> mySet = new HashSet<>(NUM_ASCII_CHARS);
1515
for (int i = 0; i < str.length(); i++) {
1616
if (mySet.contains(str.charAt(i))) {
1717
return false;

Chp. 01 - Arrays and Strings/_1_4_Palindrome_Permutation/PalindromePermutation.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
import java.util.HashMap;
44

55
public class PalindromePermutation {
6+
7+
private static final int NUM_LOWERCASE_LETTERS = 26;
8+
69
public static boolean palPerm(String str) {
710
str = str.toLowerCase().replaceAll("\\s", "");
8-
HashMap<Character, Integer> map = new HashMap<>(26);
11+
HashMap<Character, Integer> map = new HashMap<>(NUM_LOWERCASE_LETTERS);
912
for (int i = 0; i < str.length(); i++) {
1013
Character ch = str.charAt(i);
1114
if (Character.isLetter(ch)) {
@@ -27,3 +30,6 @@ public static boolean palPerm(String str) {
2730
return true;
2831
}
2932
}
33+
34+
// Time Complexity: O(n)
35+
// Space Complexity: O(1)

Chp. 01 - Arrays and Strings/_1_5_One_Away/OneAway.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ private static boolean oneEditReplace(String s1, String s2) {
2525
return true;
2626
}
2727

28-
// Function assumes s1.length() == s2.length() + 1, that is, s1 is longer than s2 by 1 character
28+
// Function assumes s1.length() == s2.length() + 1,
29+
// meaning s1 is longer than s2 by 1 character
2930
private static Boolean oneEditInsert(String s1, String s2) {
3031
if (s1.length() != s2.length() + 1) {
3132
return null;
@@ -48,3 +49,6 @@ private static Boolean oneEditInsert(String s1, String s2) {
4849
return true;
4950
}
5051
}
52+
53+
// Time Complexity: O(n+m)
54+
// Space Complexity: O(1)

Chp. 01 - Arrays and Strings/_1_7_Rotate_Matrix/RotateMatrix.java

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

3-
// Time Complexity: O(n^2). Impossible to do better since must touch all n^2 elements.
4-
// Space Complexity: O(1)
5-
63
public class RotateMatrix {
74
/* Rotates square image[][] 90 degrees clockwise */
85
public static void rotate90clockwise(int[][] image) {
@@ -47,3 +44,7 @@ private static void swap(int[][] image, int r1, int c1, int r2, int c2) {
4744
image[r2][c2] = temp;
4845
}
4946
}
47+
48+
// Time Complexity: O(n^2). Impossible to do better since must touch all n^2 elements.
49+
// Space Complexity: O(1)
50+
// Time/Space complexities are the same for both the 90-degree rotation and 180-degree rotation.

Chp. 01 - Arrays and Strings/_1_9_String_Rotation/StringRotation.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ public static boolean isRotation(String s1, String s2) {
99
return doubledString.contains(s2);
1010
}
1111
}
12+
13+
// Time/Space Complexities are same as .contains() function

0 commit comments

Comments
 (0)