Find the substring
Given s and
x , determine the zero-based index of the first occurrence of x in s
String s
consists of lowercase letters in the range ascii(a-z)
String x consists
of lower case letters and may also contain a single wildcard character ‘*’ that
represents any one character.
Example:
S=”xabcdey”
X=”ab*de”
The first
match is at index 1.
Function description:
Complete the
function firstOccurence in the editor below. The function must return an
integer denoting the zero based index of the first occurrence of string x in s.return
-1 instead.
In this code we used some inbuilt functions in python such as index() and find()
We have provided code in python.
Code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string=input() | |
sub=input() | |
if '*' in sub: | |
index=sub.index('*') | |
before=string.find(sub[:index]) | |
after=string.find(sub[index+1:]) | |
if string[before+len(sub[:index])+1]==string[after]: | |
print(before) | |
else: | |
print(string.find(sub)) |
0 Comments
Post a Comment