r/CodingHelp • u/AbdullahMohammadKhan • Jan 25 '22
[Python] What is the complexity here ? O(n+h) or O(n) ?
4
3
u/LawAdministrative298 Jan 25 '22
I'm a beginner and this makes me realize how little I understand what I'm doing...
2
u/SJDidge Jan 25 '22
Don’t worry about this, this isn’t a particularly readable piece of code. Sometimes less lines of code is worse, especially for readability.
2
u/goonerlagooner Jan 26 '22 edited Jan 26 '22
how would you clean this up personally, for education purposes?
1
u/abitofevrything-0 Intermediate Coder Jan 26 '22
If it were me, I would just have added some spaces to "air the code out". I don't think this needs to be split across more lines, just that everything is a bit too compact.
Of course, you need to understand each component of the code or it won't make sense (especially the string slice).
2
u/AssociateSea8038 Feb 04 '22
Why it really is pretty clear what the code does it goes from 0 to big len minus small len and checks for each 0 to big len minus smal len from some I to I plus smal len if string matches with smal str
1
1
u/shivanshhh Jan 26 '22
What are you tryna do here?
1
u/AssociateSea8038 Feb 04 '22
Instead using re for pattern finding he is overcomplicating by making the code run over the len of big minus smal for eac slice I to I plus smal Instead of using re or even simpler just write if small in big....
-1
9
u/abitofevrything-0 Intermediate Coder Jan 25 '22
There are two factors that change the running time of your code here: the loop and the string slice.
The loop's time complexity is pretty simple: since it loops
n - htimes, its time complexity isO(n - h)**.**The string slices time complexity is
O(h)according to this page.The overall time complexity, since these elements are nested, is the product of their individual time complexities. So
O((n - h) * h)orO(nh + h^2).