CC150: isSubstring VS isRotate

I ran into a quite interesting problem several days ago. I had a hard time solving it. So I post it here, FYI.

Assume you have a method "isSubstring" which checks if one word is a substring of 
another. Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 
using only one call to "isSubstring". 
(e.g., "waterbottle" is a rotation of "erbottlewat").

Any ideas?

What dose "rotation" mean? Well, you find a break point in a string, you break the string into two parts, you recombine the two parts in reversed order, and now you get a rotation of the original string. For the example given in the problem definition:

wat ^ erbottle   ————>  erbottle + wat  ————> erbottlewat

Given a string S, we break it into two parts which are labeledX and Y. That is, S=XY (figuratively). After rotating, we get another string T=YX. It's so damn easy if you are only asked to rotate a string, huh~ What about the other way around? Of course you can immediately think of enumerating through all the possible break points and call "isSubstring" at each break point. Since the problem limits the call of "isSubstring" to only once, this approach is not desirable. Instead, we try to figure out the underlying pattern regardless of the actual position of the break point.
One thing worthing noticing is that T=YX is actually a substring of SS=XYXY, obviously. That is to say, by making a rotation of S, you can get a substring of SS=XYXY. And it's quite convenient to construct the string U=XYXY, just concatenate an S after the original S. So without sweating, we can tell weather the string you wanna check, say S', is a substring of SS=XYXY. So how's this related to our problem?

Theorem: If S' is a substring of SS, then S` is a rotation of S.

Proof:

Negative assumption: S` is not a rotation of S.
    Let S`=BC
since S` is a substring of SS, we can get:
    SS=aBCd,  where a,d may be empty strings.
according to the assumption:
    CB!=S
and consequently:
    CBCB!=SS
thus
    CS`B!=SS
as a result, S` is not a substring of SS, which conflicts the known condition,
and the assumption is denied, S` is a rotation of S.

One more thing: the relation is a rotation of is equivalent, which means:

[1]Reflexivity: S is a rotation of S itself;
[2]Symmetry: if S is a rotation of T, then T is a rotation of S;
[3]Transitivity:if R is a rotation of S and S is a rotation of T, then R is a rotation of T.

Now it seems trivial to write any code.

#python
def isRotate(s1, s2):
    return isSubstring(s1, s2+s2)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末策肝,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子缰趋,更是在濱河造成了極大的恐慌弱睦,老刑警劉巖麦乞,帶你破解...
    沈念sama閱讀 210,835評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡蓝翰,警方通過查閱死者的電腦和手機徐伐,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 89,900評論 2 383
  • 文/潘曉璐 我一進店門贯钩,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人办素,你說我怎么就攤上這事角雷。” “怎么了性穿?”我有些...
    開封第一講書人閱讀 156,481評論 0 345
  • 文/不壞的土叔 我叫張陵勺三,是天一觀的道長。 經(jīng)常有香客問我需曾,道長吗坚,這世上最難降的妖魔是什么祈远? 我笑而不...
    開封第一講書人閱讀 56,303評論 1 282
  • 正文 為了忘掉前任,我火速辦了婚禮商源,結(jié)果婚禮上车份,老公的妹妹穿的比我還像新娘。我一直安慰自己牡彻,他們只是感情好扫沼,可當(dāng)我...
    茶點故事閱讀 65,375評論 5 384
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著庄吼,像睡著了一般充甚。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上霸褒,一...
    開封第一講書人閱讀 49,729評論 1 289
  • 那天伴找,我揣著相機與錄音,去河邊找鬼废菱。 笑死技矮,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的殊轴。 我是一名探鬼主播衰倦,決...
    沈念sama閱讀 38,877評論 3 404
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼旁理!你這毒婦竟也來了樊零?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,633評論 0 266
  • 序言:老撾萬榮一對情侶失蹤孽文,失蹤者是張志新(化名)和其女友劉穎驻襟,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體芋哭,經(jīng)...
    沈念sama閱讀 44,088評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡沉衣,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,443評論 2 326
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了减牺。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片豌习。...
    茶點故事閱讀 38,563評論 1 339
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖拔疚,靈堂內(nèi)的尸體忽然破棺而出肥隆,到底是詐尸還是另有隱情,我是刑警寧澤稚失,帶...
    沈念sama閱讀 34,251評論 4 328
  • 正文 年R本政府宣布栋艳,位于F島的核電站,受9級特大地震影響墩虹,放射性物質(zhì)發(fā)生泄漏嘱巾。R本人自食惡果不足惜憨琳,卻給世界環(huán)境...
    茶點故事閱讀 39,827評論 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望旬昭。 院中可真熱鬧篙螟,春花似錦、人聲如沸问拘。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,712評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽骤坐。三九已至绪杏,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間纽绍,已是汗流浹背蕾久。 一陣腳步聲響...
    開封第一講書人閱讀 31,943評論 1 264
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留拌夏,地道東北人僧著。 一個月前我還...
    沈念sama閱讀 46,240評論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像障簿,于是被迫代替她去往敵國和親盹愚。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,435評論 2 348

推薦閱讀更多精彩內(nèi)容