hackerrank String 3 Find a string

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


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.

Sample Input

ABCDCDC
CDC

Sample Output

2
def count_substring(string, sub_string):
    count =0
    totallen = len(string)
    sublen = len(sub_string)
    for i in range(0, totallen-sublen+1):
        if sub_string == string[i:(i+sublen)]:
            count = count +1
    return count


if __name__ == '__main__':
    string = raw_input().strip()
    sub_string = raw_input().strip()

    count = count_substring(string, sub_string)
    print count

 

發佈了27 篇原創文章 · 獲贊 1 · 訪問量 5464
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章