We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Python
  3. Strings
  4. Find a string
  5. Discussions

Find a string

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 3022 Discussions, By:

recency

Please Login in order to post a comment

  • harshadagrawal02
    5 days ago+ 0 comments
    def count_substring(string, sub_string):
    		count = 0
        str1 = ''
        for i in range(len(string)):
            str1 = str1 + string[i]
            if len(str1) == len(sub_string)+1:
                str1 = str1[ 1 : len(sub_string)+1 ]   
            if str1 == sub_string:
                count +=1
            
        return count
    
    0|
    Permalink
  • kylelombardo888
    5 days ago+ 0 comments
    def count_substring(string, sub_string):
        count = 0
        for i in range(len(string) - len(sub_string) + 1):
    # string.find returns -1 if false, but some other value if true. 
            if string.find(sub_string, i, len(sub_string)+i) != -1:
                count += 1
        return count
    
    0|
    Permalink
  • tsedojee5
    6 days ago+ 0 comments
    def count_substring(string, sub_string):
        count = 0
        for i in range(len(string)):
            if sub_string == string[i:len(sub_string) + i]:
                count += 1
        return count
    
    2|
    Permalink
  • g_hiwetmit
    1 week ago+ 0 comments
    def count_substring(string, sub_string):
        count = 0;
        str_len = len(string)
        sub_len = len(sub_string)
        i=0
        while i< (str_len-sub_len+1):
            index = string.find(sub_string, i)
            if index != -1:
                count = count + 1 
                i = index   
            i = i+1
            
        return count
    
    0|
    Permalink
  • maindarkarrohan2
    2 weeks ago+ 2 comments

    def count_substring(string, sub_string):

    count=0
    
    for i in range(0,len(string)):
    
        count+=string.count(sub_string,i,i+len(sub_string))
    
    return count
    
    1|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy