Find a string - Hackerrank Solution

 

Find a String


Objective:


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.


Solution:

def count_substring(string, sub_string):
    c=0
    for i in range(len(string)):
        if string[i:].startswith(sub_string):
            c =c+1
    return c

if __name__ == '__main__':
    string = input().strip()
    sub_string = input().strip()
    
    count = count_substring(string, sub_string)
    print(count)

Note:

This Code is Verified by all Test Cases.If any error occurs then Comment correct code Below in comment box.

Disclaimer:-

The above hole problem statement is given by hackerrank.com, but the solution is generated by the CodexRitik . if any of the query regarding this post or website fill the following contact form Thank You.

Find a string - Hackerrank Solution Find a string - Hackerrank Solution Reviewed by CodexRitik on March 24, 2020 Rating: 5

1 comment:

  1. Thank you bro. Hackerrank also good for programming and testing your skills. Whatsapp Web Apk can help you to create multiple whatsapp using single number.

    ReplyDelete

Powered by Blogger.