forked from bpilania/interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
two algorithm from cracking the coding interview
- Loading branch information
1 parent
054864f
commit a687087
Showing
3 changed files
with
122 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/com/interview/datastructures/string/C11_1_All26CharacterChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.interview.datastructures.string; | ||
|
||
/** | ||
* Problems: | ||
* Implement an algorithm to determine if a string has all 26 characters. | ||
* What if you can not use additional data structures? | ||
* | ||
* Solutions: | ||
* 1.Define a byte array of 26 characters, and scan the string and mark the flag to 1, | ||
* check if all the byte is marked to 1. | ||
* time: O(N), space: O(26) | ||
* | ||
* | ||
* @author stefanie | ||
* | ||
*/ | ||
public class C11_1_All26CharacterChecker { | ||
public boolean check_solution1(String str){ | ||
int base = (int) 'A'; | ||
boolean[] flag = new boolean[26]; | ||
|
||
for(char character : str.toCharArray()){ | ||
int index = -1; | ||
if(character >= 'A' && character <= 'Z'){ //if capital | ||
index = (int) character - base; | ||
} else if(character >= 'a' && character <= 'z') { | ||
index = (int) character - base - 32; | ||
} else { //if not character omit it | ||
continue; | ||
} | ||
flag[index] = true; | ||
} | ||
|
||
for(boolean each : flag){ | ||
if(each == false){ | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
public static void main(String[] args){ | ||
C11_1_All26CharacterChecker checker = new C11_1_All26CharacterChecker(); | ||
String str1 = "abcdefghijklmnopqrstuvwxyz"; | ||
System.out.println(str1); | ||
System.out.println(checker.check_solution1(str1)); | ||
String str2 = "AbcdefghijkLmnoPqrstuVwxyz"; | ||
System.out.println(str2); | ||
System.out.println(checker.check_solution1(str2)); | ||
String str3 = "QWERTYUIOPASDFGHJKLZXCVBNM"; | ||
System.out.println(str3); | ||
System.out.println(checker.check_solution1(str3)); | ||
String str4 = "sujpoubjwy72bbsj"; | ||
System.out.println(str4); | ||
System.out.println(checker.check_solution1(str4)); | ||
String str5 = "AbcdefghijkLmnoPqstuVwxyz"; | ||
System.out.println(str5); | ||
System.out.println(checker.check_solution1(str5)); | ||
String str6 = "Abcdefghijk67892LmnoPqrstuVwxyz12345"; | ||
System.out.println(str6); | ||
System.out.println(checker.check_solution1(str6)); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/com/interview/datastructures/string/C11_2_UniqueCharacterChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.interview.datastructures.string; | ||
|
||
/** | ||
* Problem: | ||
* Implement an algorithm to determine if a string has all unique characters. | ||
* What if you can not use additional data structures? | ||
* | ||
* Solutions: | ||
* 1. assume char set is ASCII, use a 256 char array as a flag to check if there is duplicate characters | ||
* time: O(N), space: O(256) | ||
* 2. if not allow to use extra space, could check every character in the string if there is duplicate char. | ||
* time: O(N^2), space: 1 | ||
* if could destroy the string, could sort then check if have duplicate char | ||
* time: O(NlogN), space: 1 | ||
* @author stefanie | ||
* | ||
*/ | ||
public class C11_2_UniqueCharacterChecker { | ||
|
||
public boolean check_solution1(String str){ | ||
boolean[] flag = new boolean[256]; | ||
for(int i = 0; i < str.length(); i++){ | ||
int ch = str.charAt(i); | ||
if(flag[ch]) | ||
return false; | ||
else | ||
flag[ch] = true; | ||
} | ||
return true; | ||
} | ||
|
||
public static void main(String[] args){ | ||
C11_2_UniqueCharacterChecker checker = new C11_2_UniqueCharacterChecker(); | ||
String str1 = "stefaniezhao"; | ||
System.out.println(str1); | ||
System.out.println(checker.check_solution1(str1)); | ||
String str2 = "STEFANIezhao"; | ||
System.out.println(str2); | ||
System.out.println(checker.check_solution1(str2)); | ||
String str3 = "1234567890STEFANIezhao"; | ||
System.out.println(str3); | ||
System.out.println(checker.check_solution1(str3)); | ||
} | ||
} |