We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
b = substring
i : current index
len(b): length of substring
let s = abcdcdc
b = cdc
len(b) = 3
Iteration be like
i = 0 s[0:3] == b => abc ==cdc false
i = 1 s[1:4] == b => bcd ==cdc false
i = 2 s[2:5] == b => cdc == cdc True
i = 3 s[3:6] == b => dcd == cdc False
i = 4 s[4:7] == b =>cdc == cdc True
After that iteration should stop as the complete string is search. Hence loop should go from len(s) - len(b) +1
For More information learn about string slicing and range function in python.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Find a string
You are viewing a single comment's thread. Return to all comments →
b = substring i : current index len(b): length of substring let s = abcdcdc b = cdc len(b) = 3 Iteration be like i = 0 s[0:3] == b => abc ==cdc false i = 1 s[1:4] == b => bcd ==cdc false i = 2 s[2:5] == b => cdc == cdc True i = 3 s[3:6] == b => dcd == cdc False i = 4 s[4:7] == b =>cdc == cdc True After that iteration should stop as the complete string is search. Hence loop should go from len(s) - len(b) +1 For More information learn about string slicing and range function in python.