Find a string in Python - HackerRank Solution
Problem Statement :
In this challenge, the user enters a string and a substring. You have to print the number of times that the substring occurs in the given string. String traversal will take place from left to right, not from right to left.
NOTE: String letters are case-sensitive.
Input Format:
The first line of input contains the original string. The next line contains the substring.
Constraints:
1<=len(string)<=200
Each character in the string is an ascii character.
Output Format:
Output the integer number indicating the total number of occurrences of the substring in the original string.
Solution:
#the complete code as required for hackerrank challenge.
def count_substring(string, sub_string):
count=0
for i in range(len(string)):
for j in range(len(sub_string)):
if string[i+j]==sub_string[j] and j==(len(sub_string)-1):
count=count+1
if string[i+j]!=sub_string[j]:
break
if i==len(string)-len(sub_string):
break
return count
if __name__ == '__main__':
string = input().strip()
sub_string = input().strip()
count = count_substring(string, sub_string)
print(count)
Disclaimer:-
The above hole problem statement is given by hackerrank.com but the solution is generated by the Hackerranksolution.site authority if any of the queries regarding this post or website fill the following contact form thank you.