Skip to content

Commit

Permalink
anagram
Browse files Browse the repository at this point in the history
  • Loading branch information
summerzhao committed Jul 14, 2013
1 parent cd7ed40 commit 7a1ff56
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="lib" path="libs/algs4.jar"/>
<classpathentry kind="lib" path="libs/stdlib.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
3 changes: 1 addition & 2 deletions .settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
#Sun Jul 29 08:17:20 CST 2012
eclipse.preferences.version=1
encoding//src/com/interview/algorithms/array/RandomShuffle.java=UTF-8
encoding/README=UTF-8
9 changes: 5 additions & 4 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ C1: General
3) Write a function f(a, b) which takes two character string arguments and returns a string containing only the characters found in both strings in the order of a. Write a version which is order N-squared and one which is order N.
4) Write a function (with helper functions if needed) called to Excel that takes an excel column value (A,B,C,D��A,AB,AC,��AAA..) and returns a corresponding integer value (A=1,B=2,��AA=26..).
5) You have a stream of infinite queries (ie: real time Google search queries that people are entering). Describe how you would go about finding a good estimate of 1000 samples from this never ending set of data and then write code for it.
6) Write some code to reverse a string.

7) Write some code to find all permutations of the letters in a particular string.
8) What method would you use to look up a word in a dictionary?
9) You have eight balls all of the same size. 7 of them weigh the same, and one of them weighs slightly more. How can you fine the ball that is heavier by using a balance and only two weighings?
Expand All @@ -26,8 +26,7 @@ C1: General
17) Given a set of coin denominators, find the minimum number of coins to give a certain amount of change.
18) Given that you have one string of length N and M small strings of length L. How do you efficiently find the occurrence of each small string in the larger one?
19) How would you reverse the image on an n by n matrix where each pixel is represented by a bit?
20) Rotate a string
21) Rote an array

22) How to find the median among the given numbers whose values are unknown but falls into a narrow range.
23) Given a set of ranges, find the two ranges with the greatest overlap.
24) Verify whether an arithmatic expression is valid, suppose the expression only contains "+", "-", "*", "/", "(", ")" and numbers are integers.
Expand Down Expand Up @@ -69,6 +68,7 @@ C4: Array
13) Given an array containing both positive and negative numbers, find the sub array whose elements' sum is maximum.
14) Given a set S, find all the maximal subsets whose sum <= k.
15) Find the uniq amount of absolute values in a given sorted array
16) Write code to reverse a array of int

C5: Tree
1) Describe the algorithm for a depth-first graph traversal.
Expand Down Expand Up @@ -103,4 +103,5 @@ C10: System Design
C11: String
1) Implement an algorithm to determine if a string contains all 26 characters. What if you can not use additional data structures?
2) Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures?

Addition, remove all the duplicate characters
3) Write a method to decide if two strings are anagrams or not.
88 changes: 88 additions & 0 deletions src/com/interview/datastructures/string/C11_3_AnagramString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.interview.datastructures.string;

/**
* Problem:
* Write a method to decide if two strings are anagrams or not.
*
* Solution:
* 1. sort the two strings, and check if equal
* time: O(NlogN), space: O(1)
* 2. check contained character is same or not
* time: O(N^2), space: O(1)
* @author stefanie
*
*/
public class C11_3_AnagramString {
public boolean check_solution1(String str1, String str2){
str1 = sortStr(str1);
str2 = sortStr(str2);
return str1.equals(str2);
}

public boolean check_solution2(String str1, String str2){
char[] arr1 = str1.toCharArray();
char[] arr2 = str2.toCharArray();

for(char ch : arr1){
int num1 = 0;
for(char ch1 : arr1){
if(ch1 == ch) num1++;
}
int num2 = 0;
for(char ch2 : arr2){
if(ch2 == ch) num2++;
}
if(num1 != num2) return false;
}
return true;
}

public String sortStr(String str){
char[] array = str.toCharArray();
//quick sort
quicksort(array, 0, array.length-1);
return String.valueOf(array);
}

public void quicksort(char[] array, int start, int end){
if(end <= start) return;
int key = partition(array, start, end);
quicksort(array, start, key - 1);
quicksort(array, key + 1, end);
}

public int partition(char[] array, int start, int end){
char key = array[start];
int i = start + 1;
int j = end;
while(true){
while(array[i] <= key && i < end) i++;
while(array[j] > key && j > start) j--;
if(i >= j) break;
char temp = array[i];
array[i] = array[j];
array[j] = temp;
}
array[start] = array[j];
array[j] = key;
return j;
}

public static void main(String[] args){
C11_3_AnagramString checker = new C11_3_AnagramString();
String str1 = "abcde";
String str2 = "cdbae";
System.out.println(checker.check_solution1(str1, str2));
System.out.println(checker.check_solution2(str1, str2));

String str3 = "abcdeabsyy";
String str4 = "bcydybaeas";
System.out.println(checker.check_solution1(str3, str4));
System.out.println(checker.check_solution2(str3, str4));

String str5 = "abcdeabsey";
String str6 = "bcydybaeas";
System.out.println(checker.check_solution1(str5, str6));
System.out.println(checker.check_solution2(str5, str6));
}
}

0 comments on commit 7a1ff56

Please sign in to comment.