Q- firx of all this simple is a good problem to partice a pattern called “prefix sum “…
- User
Q- firx of all this simple is a good problem to partice a pattern called “prefix sum “ oky
Q- we instead of computing each prefix and store them we create a separate list of store the upper ( and lower ) bound of each prefix string Q- and when you to compute this prefix string just access them directly by there index
Q- tw 5alina narj3o lel exmple eli fe edian honi oky : esmo “Check if a word Occurs As a prefix of any word in a Sentence oky “
Bahii honi lana list of string esmha ‘sentence” oky feha des words separated by a “single space “
A b3ed 3andik a “searchWord” kelma w7da : Ya sedidk y ben sedik n7bo n3rfoo est ce que el “searchWord” ha4ii : hiya prefix eli ay word Fe sentence wle
—> be na7aya o5ra n7bo n3rfooo est ceque el “searchWord” kelm twjded fe kelma men kelmet ey wle ?
→ ken 9enaha donc direct nrj3o el index mt3ha oky
Bahii ken m9ench el prefix mt3 el “SearchWord” fe 7ate kelma chnowa n3mlo ? Just we return -1 oky :
Q- el “prefix” of a string “s” howa aya leading contiguous susbstring of s
Note by the index here is (1-indexd oky )
Q- awel seoul chnowa el input & output format houni oky ?
→ input howa : sentence : string , searchWord:string → output howa : index : int
Q-can you go through an example ?
El sentence honi = “i love eating burger” , searchWord = “burg”
El kelma el loula honi hiya : “i” -> “i” toll mt3ha 9all men “searchWord” (1<4) donc mch lzem ‘check prefix for it “ ) → you do not to check the prefix of word that already less then the desired prefix one oky El kelma el thaniya honi hiya : “Love” → “burg “ is not a prefix of “love” donc we move to the next one
El kelma el thaltha honi hiya “Eating “ → “burg” much a prefix of “eating “ donc we move to the next one
El kelma el rab3a honi hiya
“Burger” → “burg” lena awl mara l9wa kema “burg” hiya bedha a prefix lel “Burger” okito
→ donc we return idex of “burger”+1 (1-indexed oky)
Q netswer el logic bendy y4a77 honii right ?
Lezman bsh nt5liw el code modular and clean nets3mlo helper function
El role mt3h yukon check_prefix oky
Q- esmha a prefix(t9bal el current word , o zeda el “searchWord” : string) -> return bool oky
// we compute the length of “searchWord” oky n=searchWord.length() Return s[:n]==seachWord okyQ- b3ed nrjo el logic principle oky
PseudoCode : Function (sentence : string , searchWord : string ) -> int : // split convert the input string into a list of words sentence =sentence.split(“”) For i , word in enumerate(sentence ): If helper(word,searchWord): Return i+1 // (1-indexed oky) Return -1
Q- twa netswer lel party mt3 awll feedback oky , Q- lina wselna lel ani nselik bsh t3teni rayik fe my thinking process so far Q- t3tenii honest feedback oky and rej3lii Q- what went right Q- and what went roung
- Assistant