Skip to content

Commit 900ecc8

Browse files
committed
Updated solutions
1 parent bd71d25 commit 900ecc8

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,7 @@ public static boolean uniqueCharacters(String str) {
2929

3030
// Follow-up Question: What if we're not allowed to use additional data structures?
3131
//
32-
// Answer: Can do brute-force O(n^2) runtime O(1) space solution by comparing all pairs
32+
// Answer: Can do brute-force solution by comparing all pairs
33+
// Time Complexity: O(n^2), but since strings above NUM_ASCII_CHARS=256 immediately
34+
// return false, time complexity becomes O(1)
35+
// Space Complexity: O(1)

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@ public static boolean palPerm(String str) {
1515
map.merge(ch, 1, Integer::sum);
1616
}
1717
}
18-
19-
// Even length strings: can not have any characters an odd # of times
20-
// Odd length strings: can have at most 1 character an odd # of times
21-
int exceptions = (str.length() % 2 == 0) ? 0 : 1;
18+
19+
// Odd length strings: Can have at most 1 character an odd # of times
20+
// Even length strings: Can have either 0,2,4,6... number of odd characters.
21+
// Anything above 1 means not a palindrome.
22+
int oddCount = 0;
2223
for (Integer value : map.values()) {
2324
if (value % 2 != 0) {
24-
exceptions--;
25+
oddCount++;
2526
}
26-
if (exceptions < 0) {
27+
if (oddCount > 1) {
2728
return false;
2829
}
2930
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public static boolean oneAway(String s1, String s2) {
1313
}
1414

1515
private static boolean oneEditReplace(String s1, String s2) {
16+
if (s1.length() != s2.length()) {
17+
return false;
18+
}
1619
int mismatches = 0;
1720
for (int i = 0; i < s1.length(); i++) {
1821
if (s1.charAt(i) != s2.charAt(i)) {

0 commit comments

Comments
 (0)