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
Concept
Some string processing examples, such as these, might be useful.
There are a couple of new concepts:
In Python, the length of a string is found by the function
To traverse through the length of a string, use a for loop:
There are a couple of new concepts:
In Python, the length of a string is found by the function
len(s)
, where is the string.To traverse through the length of a string, use a for loop:
for i in range(0, len(s)):
print (s[i])
A range function is used to loop over some length:
range (0, 5)
Here, the range loops over to . is excluded.
Find a string - Hacker Rank Solution
Approach 1
Slice an
Approach 2
This can be solved by using a regex.
Slice an
x
amount of string in each iteration of the loop.Approach 2
This can be solved by using a regex.
Problem Tester's code:
Approach 1
A = raw_input().strip()
x = raw_input().strip()
count = 0
for i in range(len(A) - len(x) + 1):
if A[i:i+len(x)] == x:
count += 1
print count
Approach 2
import re
a = raw_input()
b = raw_input()
match = re.findall('(?='+b+')',a)
print len(match)
but there we are given a buit func where we have to complete them only. so it will not work there.
ReplyDeletebut you can understand from here then do it as you like , you dont have to always copy paste
Deleteand for your help
Deletedef count_substring(string, sub_string):
c=0
for i in range(len(string)):
if string[i:].startswith(sub_string):
c +=1
return c
if __name__ == '__main__':
string = input().strip()
sub_string = input().strip()
count = count_substring(string, sub_string)
print(count)
WHAT WILL BE runtime complexibility ??
Deletedef count_substring(string, sub_string):
ReplyDeletefor i in string:
co=string.count(sub_string)
co+=1
return co
if __name__ == '__main__':
string = input().strip()
sub_string = input().strip()
count = count_substring(string, sub_string)
print(count)
wrong
Deletedef count_substring(string, sub_string):
ReplyDeletecount_ = 0
a = list(sub_string)
b = list(string)
l = len(sub_string)
c = 0
for i in range(len(string) - l + 1):
for j in range(l):
if a[j] != b[i+j]:
c = 0
break
c = c + 1
if c == l:
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
def count_substring(string, sub_string):
ReplyDeleteres = sum(1 for i in range(len(string))
if string.startswith(sub_string, i))
return res
if __name__ == '__main__':
string = raw_input().strip()
sub_string = raw_input().strip()
count = count_substring(string, sub_string)
print count
x = "abcdef"
ReplyDeletey = "cde"
for i in range(len(x)):
if x[i] in y:
print(i)
break