Google Interview - Check String Order

given an order string "abc" check if "aabdccd" maintain the order

"aabdccd" -> true;

"abbca" -> false;

note:order does not contain all chars in s

public boolean checkOrder(String pat, String s) {
	int[] pos = new int[256];
	Arrays.fill(pos, -1);
	for(int i=0; i<pat.length(); i++) {
		pos[pat.charAt(i)] = i;
	}
	int last = 0;
	for(int i=0; i<s.length(); i++) {
		int val = pos[s.charAt(i)];
		if(val == -1) continue;
		if(val < last) return false;
		last = val;
	}
	return true;
}

猜你喜欢

转载自yuanhsh.iteye.com/blog/2231070