Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Count Repeating Letters in a String
00:00
5 left

Count Repeating Letters in a String

EasyPython

Problem

In an LTIMindtree text-processing utility, implement a function that counts how many times a specified character appears in a string. The comparison must be case-sensitive, so "A" and "a" are different characters.

Formal Specification

Implement count_character(text, target):

  • Input: text, a string, and target, a string containing exactly one character.
  • Output: An integer representing the number of positions in text whose character equals target.
  • Count letters, digits, spaces, punctuation, and any other characters exactly as they appear.
  • Return 0 when the target does not occur or when text is empty.

Constraints

  • 0 <= len(text) <= 10^5
  • target contains exactly one character
  • Matching is case-sensitive
  • text may contain spaces, punctuation, digits, or Unicode characters

Function Signature

def count_character(text, target):
Interviewer

Your question is Count Repeating Letters in a String. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.