Re: [問題] Google Interview Question (1)

作者: Leon (Achilles)   2013-02-11 13:33:27
※ 引述《RockLee (Now of all times)》之銘言:
: 原始網址:
: http://www.careercup.com/question?id=14539805
: 題目:
: Three strings say A, B, C are given to you.
: Check weather 3rd string is interleaved from string A and B.
: Ex: A="abcd" B="xyz" C="axybczd". answer is yes. o(n)
: 用 Dynamic Programming 應該可在 O(n^2) 的時間內解決
: 但要在 O(n) 的時間內解決就想不出來了 Orz...
: CareerCup 上的討論看來都無法在 O(n) 的時間內正確的解決
: 不知道板上有沒有人有什麼 idea?
這是一個很快的想法, 我不知道對不對,
反正大家討論一下.
You can put two flags on string A and B,
then scan every character in C.
for example, the first step,
it will be, A = a | bcd and B = | xyz.
then it's not hard to see if the flag of A,B move to the end.
One problem is the one it points out in the webpage.
if A = 'ca', B = 'cb' , C = 'cacb' or 'cbca' should be both true.
So, when you scan the character, you need to put an additional index
to handle the duplicate character.
In this situation, C = 'cacb', when you do the scan of the fist char
A = c | a and B = c | b, but both has index = 1.
In the next step, you scan second char, which is a,
Now, A = 'c a |' and you reset the index of A into 0.
at the same time you push the flag of B back with 1.
So B = '| c b'.
This should be running in a linear time?
作者: isnoneval (虛物之海)   2013-02-11 14:02:00
A=xy, B=xxxy, C=xxyxxy?
作者: Leon (Achilles)   2013-02-11 16:35:00
恩, 妳是對的, 這個情況會越長越多, 不能只記一個不過, 我猜 duplicate char 可以另外的方式處理, 我想想

Links booklink

Contact Us: admin [ a t ] ucptt.com