http://shibaili.blogspot.com/2015/07/google-interview-questions-2.html
REF http://www.mitbbs.com/article_t/JobHunting/32996813.html
phone: 1.given an order string "abc" check if "aabdccd" maintain the order
"aabdccd" -> true;
"abbca" -> false;
note:order does not contain all chars in s
REF http://www.mitbbs.com/article_t/JobHunting/32996813.html
phone: 1.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;
- }
bool
checkOrder(string order, string s) {
vector<
int
> v(256,-1);
for
(
int
i = 0; i < order.size(); i++) {
v[order[i]] = i;
}
int
curOrder = 0;
for
(
int
i = 0; i < s.length(); i++) {
if
(v[s[i]] == -1)
continue
;
if
(v[s[i]] < curOrder) {
return
false
;
}
curOrder = v[s[i]];
}
return
true
;
}