find a string hackereank

def count_substring(string, sub_string):
    count=0  #initialise count variable
    for i in range(0,len(string)):
        if string[i:].startswith(sub_string): # basically it traverses from left to right and looks for occurence of substring
            count+=1 #every time count will increase by 1

    return count

4
1
Garry Gomez 105 points

                                    #(method 1)
import re
def count_substring(string,sub_string)
count = re.findall('(?='+sub_string+')',string)
return len(count)

# (method 2) lengthy but easier for beginners
def count_substring(string, sub_string): 
    zero=0
    astring=string + "@"
    sub_count=0
    count=0
    for i in range (len(astring)-1):
        if astring[i]==sub_string[0]:
            for j in range(1,len(sub_string)):
                if astring[i+j]==sub_string[j]:
                    sub_count+=1
                elif astring[j+i]=='@':
                    break
            if sub_count==len(sub_string)-1:
                count+=1
            sub_count=0
    return count

4 (1 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
find a string hackerrank solution in python 3 find a string hackerrank hackerrank in string solution string function calculation hackerrank solution find a string hackerrank solution explaination Strings hackerrank solution hackerrank python find a string solution HackerRank in a String! hackerrank solution in java Find a string hacker rank solution check if the string phone start with string start and print the result hackerrank hackerrank find a string find strings hackerrank solution find a string hackerrank solution in python github Find a string heckerrank hackerrank solutions The first line of input contains the original string. The next line contains the substring. find substring in hackerrank solution python findsubstring in hackerrank solution python 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. count occurrences of substring in string python hackerrank solution 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 print number of times the string strfind appears in the string (first second parent city) hackerrank Find a string hacker rnak find a string python hackerrank solution comparing strings practice python hackerrank solution find a string in a string python hackerrank solution find string hackerrank solution hackerrank find a string solution rookie compare strings hackerrank solution in python find a string hackerrank solution in python find a string hackerrank solution python 3 count substring in python hackerrank solution hackerrank find a string find a string hackereank find a string hackerank find a string hackeran find a string hackerrank solution
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source