Facebook interview - Suffix Array Order - 我的博客 - ITeye技术网站
第二题: input: int[] text, int[] subText
output: boolean isExist;
检查text数组中有没有一个subarray 是subText。要求时间小于O(N^2), N == text.length;
这里假设我们有了第一题的 suffix_array_order.
(做法是binary search)
http://www.geeksforgeeks.org/suffix-array-set-1-introduction/
就是相当于把所有的substring 排序,然后binary search
You don't need to create an LIst<List> for all the suffix arrays since that will take extra space and time to create. What I did is to create an wrapper class:
Class Index{
public int index;
public int[] array;
}
Just pass the reference of whole input array to the wrapper class object.
I answered O(n log n), n is the length of the array at the interview and passed.
After that, I think if we consider the worse case of comparing two string, the worst case time should be O(n^2 log n).
Read full article from Facebook interview - Suffix Array Order - 我的博客 - ITeye技术网站
首先定义了suffix string 或者说suffix arrary
如果有个数组是 int[] text = {10, 20, 30, 25}
那么 suffix[0] = {10, 20, 30, 25}
suffix[1] = {20, 30, 25}
suffix[2] = {30, 25}
suffix[3] = {25}
如果对这些数组进行lexical order 的排序,我们可以得到
suffix[0] < suffix[1] < suffix[3] < suffix[2]
问题是:
input: int[] text
output: int[] suffix_array_order
e.g.
input: int[] text = {10, 20, 30, 25}
output: int[] suffix_array_order = {0, 1, 3, 2}
如果有个数组是 int[] text = {10, 20, 30, 25}
那么 suffix[0] = {10, 20, 30, 25}
suffix[1] = {20, 30, 25}
suffix[2] = {30, 25}
suffix[3] = {25}
如果对这些数组进行lexical order 的排序,我们可以得到
suffix[0] < suffix[1] < suffix[3] < suffix[2]
问题是:
input: int[] text
output: int[] suffix_array_order
e.g.
input: int[] text = {10, 20, 30, 25}
output: int[] suffix_array_order = {0, 1, 3, 2}
Solution:
注意: Arrays.sort(T[], Comparator)只能用于排序对象数组,不能用于排序int, long之类的原始值数组!
- public Integer[] suffixArrayOrder(int[] A) {
- int n = A.length;
- Integer[] order = new Integer[n];
- for(int i=0; i<n; i++) {
- order[i] = i;
- }
- Arrays.sort(order, new Comparator<Integer>(){
- @Override
- public int compare(Integer o1, Integer o2) {
- int res = A[o1] - A[o2];
- while(res == 0 && o1 < n && o2 < n) {
- res = A[o1++] - A[o2++];
- }
- return res;
- }
- });
- System.out.println(Arrays.toString(order));
- return order;
- }
第二题: input: int[] text, int[] subText
output: boolean isExist;
检查text数组中有没有一个subarray 是subText。要求时间小于O(N^2), N == text.length;
这里假设我们有了第一题的 suffix_array_order.
(做法是binary search)
http://www.geeksforgeeks.org/suffix-array-set-1-introduction/
就是相当于把所有的substring 排序,然后binary search
You don't need to create an LIst<List> for all the suffix arrays since that will take extra space and time to create. What I did is to create an wrapper class:
Class Index{
public int index;
public int[] array;
}
Just pass the reference of whole input array to the wrapper class object.
I answered O(n log n), n is the length of the array at the interview and passed.
After that, I think if we consider the worse case of comparing two string, the worst case time should be O(n^2 log n).
- public class Index {
- int[] suff;
- int idx;
- Index(int[] _suff, int _idx) {
- suff = _suff;
- idx = _idx;
- }
- }
- //Sort
- int[] suffixOrder(int[] text, int n) {
- int[] res = new int[n];
- Index[] indices = new Index[n];
- for (int i=0; i < n; i++) {
- int[] suffix = Arrays.copyOfRange(text, i, text.length);
- indices[i] = new Index(suffix, i);
- }
- Arrays.sort(indices, new Comparator<Index>() {
- public int compare(Index i1, Index i2) {
- int p1 = 0;
- int p2 = 0;
- while (p1 < i1.suff.length && p2 < i2.suff.length) {
- if(i1.suff[p1] != i2.suff[p2]) {
- return i1.suff[p1] - i2.suff[p2];
- } else {
- p1 ++;
- p2 ++;
- }
- }
- if (p2 == i2.suff.length) {
- return 1;
- }
- return -1;
- }
- });
- for (int i = 0; i < n; i++) {
- res[i] = indices[i].idx;
- }
- return res;
- }
- // Search
- boolean search(int[] text, int n, int[] sub_text, int m) {
- int[] suffix_order = suffixOrder(text, n);
- Comparator<Index> textCompare = new Comparator<Index>(){
- @Override
- public int compare(Index i1, Index i2){
- while (a < text.length && b < sub_text.length) {
- if (text[a] != sub_text[b]) {
- return text[a] - sub_text[b];
- } else {.
- a ++;
- b ++;.
- }
- }
- if(a == text.length) return -1;
- return 0;.
- }
- };
- int lo = 0;
- int hi = n - 1;
- while (lo <= hi) {
- int mid = (lo + hi) / 2;
- int[] suffix = Arrays.copyOfRange(text, suffix_order[mid], text.length);
- int cmp = textCompare(suffix, sub_text);
- if (cmp == 0) {
- return true;
- } else if(cmp < 0) {
- lo = mid + 1;
- } else {}
- hi = mid - 1;
- }
- return false;
- }
Read full article from Facebook interview - Suffix Array Order - 我的博客 - ITeye技术网站