-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd7ed40
commit 7a1ff56
Showing
4 changed files
with
96 additions
and
6 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
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 |
---|---|---|
@@ -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 |
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
88 changes: 88 additions & 0 deletions
88
src/com/interview/datastructures/string/C11_3_AnagramString.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,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)); | ||
} | ||
} |