`

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;
}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics