File tree Expand file tree Collapse file tree 2 files changed +5
-21
lines changed
Chp. 01 - Arrays and Strings
_1_4_Palindrome_Permutation Expand file tree Collapse file tree 2 files changed +5
-21
lines changed Original file line number Diff line number Diff line change 1
- /** Tips **/
2
-
3
- // Should ask interviewer if String is ASCII or Unicode (We assume ASCII)
4
-
5
- /** Algorithm **/
6
-
7
- // Use a HashMap to store characters and detect duplicates.
8
-
9
- /** Solution **/
10
-
11
1
package _1_1_Is_Unique ;
12
2
13
3
import java .util .HashSet ;
14
4
5
+ // Should ask interviewer if String is ASCII or Unicode (We assume ASCII)
6
+
15
7
public class IsUnique {
16
8
private static final int NUM_CHARS = 256 ; // number of ASCII characters
17
9
@@ -31,17 +23,10 @@ public static boolean uniqueCharacters(String str) {
31
23
}
32
24
}
33
25
34
- /** Time/Space Complexity **/
35
-
36
26
// Time Complexity: O(1)
37
27
// Space Complexity: O(1)
38
28
// Checking for str.length() > 256 lowered our time/space complexity from O(n) to O(1)
39
29
40
- /** Follow-up Question **/
41
-
42
- // What if we're not allowed to use additional data structures?
43
-
44
- /** Follow-up Answer **/
45
-
46
- // Do a brute-force solution using nested loops to compare all pairs.
47
- // This will be O(n^2) time complexity and O(1) space complexity.
30
+ // Follow-up Question: What if we're not allowed to use additional data structures?
31
+ //
32
+ // Answer: Can do brute-force O(n^2) runtime O(1) space solution by comparing all pairs
Original file line number Diff line number Diff line change 4
4
5
5
public class PalindromePermutation {
6
6
public static boolean palPerm (String str ) {
7
- /* Create HashMap to count characters in String */
8
7
str = str .toLowerCase ().replaceAll ("\\ s" , "" );
9
8
HashMap <Character , Integer > map = new HashMap <>(26 );
10
9
for (int i = 0 ; i < str .length (); i ++) {
You can’t perform that action at this time.
0 commit comments