Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# CCAssignment
Hakim Sidahmed
hsidahme
76 changes: 76 additions & 0 deletions ch1/Solution01.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import java.util.List;

/**
* Question 1.1
* Created by hsidahme on 9/8/15.
*/
public class Solution01 {
public static void main(String[] args){
Solution01 solution01 = new Solution01();

String s1 = "asfgte";
String s2 = "aasded";

System.out.println("isUnique(" + s1 + "): " + solution01.isUnique(s1));
System.out.println("isUnique(" + s2 + "): " + solution01.isUnique(s2));

System.out.println("isUniqueV2(" + s1 + "): " + solution01.isUniqueV2(s1));
System.out.println("isUniqueV2(" + s2 + "): " + solution01.isUniqueV2(s2));

}

/**
* Determine if String s has all unique characters.
* @param s - Input String
* @return true if all the characters in s are unique. false otherwise
*/
public boolean isUnique(String s){
// Assume 8-bit ascii encoding (256 possible characters)
if (s.length() > 256){
return false;
}

// Create an array in which each element stores the count of the corresponding character
int[] elements = new int[256];

for (int i=0; i<s.length();i++){
elements[s.charAt(i)]++;
}

for (int i=0; i<256; i++){
if (elements[i] > 1){
return false;
}
}

// None of the characters is repeated, return true
return true;
}

// Follow up: What if you cannot use additional data structures

/**
* Second version of isUnique that does not use any extra data structure.
* @param s - Input String
* @return true if s has unique characters only. false otherwise
*/
public boolean isUniqueV2(String s){
// O(n^2) complexity: For each character in s, check that none of the next characters is the same

if (s == null || s.length() <= 1){
return true;
}

for (int i=0; i<s.length()-1; i++){
for (int j=i+1; j<s.length(); j++){
if (s.charAt(i) == s.charAt(j)){
return false;
}
}
}

// None of the characters is repeated, return true
return true;
}

}
49 changes: 49 additions & 0 deletions ch1/Solution02.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Created by Hakim on 9/8/15.
*/

public class Solution02 {
public static void main(String[] args){
Solution02 solution02 = new Solution02();

String s1 = "qwerty";
String s2 = "wrtqey";
String s3 = "qwerta";

System.out.println("isPermutation(" + s1 + "," + s2 + "): " + solution02.isPermutation(s1,s2));
System.out.println("isPermutation(" + s1 + "," + s3 + "): " + solution02.isPermutation(s1,s3));
}

/**
* Check whether Strings s and t are permutations of each other.
* @param s
* @param t
* @return true if s and t are permutations of each other. false otherwise
*/
public boolean isPermutation(String s, String t){
// If the two Strings have different lengths, they cannot be permutations of each other
if (s.length() != t.length()){
return false;
}

// Assume the Strings are 8-bit ascii encoded
int[] elements = new int[256];

// Count the number of occurrences for each character in s and subtract the number of occurrences
// of this character in t
for (int i=0; i<s.length(); i++){
elements[s.charAt(i)]++;
elements[t.charAt(i)]--;
}

// If any element in the elements arrays is non zero, than its count is not the same in s and t
for (int i=0; i<256; i++){
if (elements[i] != 0){
return false;
}
}

// All characters have same counts in s and t. They are permutations of each other
return true;
}
}
43 changes: 43 additions & 0 deletions ch1/Solution03.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Created by Hakim on 9/8/15.
*/

/**
* URLify a String, replacing all whitespaces by "%20".
*/
public class Solution03 {
public static void main(String[] args){
Solution03 urlify = new Solution03();
String s = "Mr John Smith ";
char[] c = s.toCharArray();
urlify.urlify(c,13);
System.out.println(c);
}

/**
* URLify a character array by replacing all the spaces by "%20".
* Assume the character array contains enough trailing whitespaces to contain the new array
* @param c
* @param actualLength
*/
public void urlify(char[] c, int actualLength){
int pointer1 = actualLength-1;
int pointer2 = c.length-1;

while (pointer1 >= 0){
if (c[pointer1] == ' '){
c[pointer2] = '0';
c[pointer2-1] = '2';
c[pointer2-2] = '%';

pointer2 -= 3;
pointer1--;
}
else{
c[pointer2] = c[pointer1];
pointer2--;
pointer1--;
}
}
}
}
54 changes: 54 additions & 0 deletions ch1/Solution04.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Created by Hakim on 9/8/15.
*/

/**
* Check whether a String is the permutation of a palindrome.
*/
public class Solution04 {
public static void main(String[] args){
Solution04 pp = new Solution04();
System.out.println(pp.isPalindromePermutation("Tact Coa"));
}

/**
* Check whether a String is the permutation of a palindrome.
* @param s
* @return true if it is. false otherwise
*/
public boolean isPalindromePermutation(String s){
// A String is a palindrome permutation iif
// it contains an even count for each character except at most
// one character which can be odd
if (s == null || s.length() == 0){
return true;
}

// Lower case the String and remove white spaces
s = s.toLowerCase();
s = s.replaceAll(" ", "");

// Assume 8-bit ascii encoding
boolean[] isOdd = new boolean[256];

// For each character c in the input String, save whether it is
// represented an odd number of times
for (int i=0; i<s.length(); i++){
isOdd[s.charAt(i)] = !isOdd[s.charAt(i)];
}

// Count the number of odd characters
int numberOdds = 0;

for (int i = 0; i < isOdd.length; i++) {
if (isOdd[i]){
numberOdds++;
}
// At most one character can have an odd count
if (numberOdds > 1){
return false;
}
}
return true;
}
}
58 changes: 58 additions & 0 deletions ch1/Solution05.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Created by Hakim on 9/8/15.
* Find whether two Strings are less than one edit away
*/
public class Solution05 {
public static void main(String[] args){
Solution05 oa = new Solution05();
System.out.println(oa.isOneAway("pale","ple"));
System.out.println(oa.isOneAway("pales","pale"));
System.out.println(oa.isOneAway("pale","bale"));
System.out.println(oa.isOneAway("pale","bake"));
System.out.println(oa.isOneAway("",""));
System.out.println(oa.isOneAway("","e"));
}

/**
* Check whether two Strings are at most one edit away.
* Possible edits are insertion, deletion, and replacement
* @param s - First input String
* @param t - Second input String
* @return true if s and t are less than one edit away. False otherwise
*/
public boolean isOneAway(String s, String t){
// If s and t's lengths are different by more than 1, then return false
if (Math.abs(s.length()-t.length()) > 1){
return false;
}
// Make sure the first input String is greater or equal than the second one
if (s.length() < t.length()){
return isOneAway(t,s);
}

int i = 0; // Pointer for the first String
int j = 0; // Pointer for the second String

int diff = 0; // Count the number of differences (edits) between the two Strings
while (i < s.length() && j < t.length()){
// If two characters are different, increment count
if (s.charAt(i) != t.charAt(j)){
diff++;
if (s.length() != t.length()){
// If s and t have different sizes, than edit should be a deletion
i++;
continue;
}
}
i++;
j++;
}

if (diff <= 1){
return true;
}
else{
return false;
}
}
}
77 changes: 77 additions & 0 deletions ch1/Solution06.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* Created by Hakim on 9/9/15.
* Compress a String using the characters count
*/
public class Solution06 {
public static void main(String[] args){
Solution06 sc = new Solution06();
System.out.println(sc.compressString("aabcccccaaa"));
System.out.println(sc.compressString("abccd"));
}

/**
* Perform String compression using the counts of the characters in the String.
* @param s - String to compress
* @return - Compressed String
*/
public String compressString(String s){
// If the compressed String is not actually smaller than s, then return s
if (s.length() <= 2 || !isCompression(s)){
return s;
}

StringBuilder sb = new StringBuilder();
int count = 1;

for (int i=1; i<s.length(); i++){
// If new character is seen, then append the previous one with its count
if (s.charAt(i) != s.charAt(i-1)){
sb.append(s.charAt(i-1));
sb.append(count);
count = 1;
}
else{
count++;
}
}

// Append the last character if it was the same as the previous one
if (s.charAt(s.length()-1) == s.charAt(s.length()-2)){
sb.append(s.charAt(s.length()-1));
sb.append(count);
}

// Return the compressed String only if it is actually a compression
if (sb.length() < s.length()){
return sb.toString();
}
else{
return s;
}
}

/**
* Check whether the compressed String would actually be smaller than the input one.
* @param s - String to compress
* @return - true if the compressed String would be smaller than the input one
*/
public boolean isCompression(String s){
int result = 0;
int count = 1;

for (int i=1; i<s.length(); i++){
if (s.charAt(i) != s.charAt(i-1)){
result += 1 + String.valueOf(count).length();
count = 1;
}
else{
count++;
}
}

if (result < s.length()){
return true;
}
return false;
}
}
Loading