Code 129: TCS NQT - Check First and last character and counter | TCS Coding | 365 Days of Code

Опубликовано: 30 Сентябрь 2024
на канале: Code House
188
2

You can read the question in description
"""
Given a string Str of length N and a character Char. The task is to check whether starting character and ending character is same. If both are matching find the occurrence of a given Char.

Instruction:
If starting and ending characters are matching then print occurrence of a given character else print 0 if the input string length is less than 2, print -1

Example 1:
school - input string str
o - Character Chr
Ouput:
0

Example 2:
noon - input string str
o - Character Chr
Ouput:
2

Expanation:
In Example 1,
Str = "school"
starting character of Str is "s" and ending character is "l". So they both are not the same. Hence the output is zero.

In Example 2,
Str = "noon"
Both the start and end character of Str is 'n' that's why we have to find the occurrence of the given character 'o'. 'o' is two times in noon hence the output is 2.
"""

str = input()
char = input()
if len(str)<2:
print(-1)
elif str[0] == str[-1]:
print(str.count(char)) # It will print the occurence of the char
else:
print(0)

Thanks