Find a string Hackerrank

 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

## Solutions:--

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


#Sloution:--2

def count_substring(string, sub_string): counter,sum = 0,0 for _ in range(0, len(string)): if matcher(string[counter:(len(sub_string)+counter)], sub_string): sum = sum + 1 counter=counter + 1 return sum def matcher(sliced_str, sub_string): return sliced_str == sub_string


Comments

Popular posts from this blog

XAMPP, SQL BASIC COMMANDS

The Minion Game Hackerrank Solution

Arrays - DS | HackerRank Solutions