Problem 261928 · easy · Level 02 Linear Data Structures

How Many Sentences?

strings · counting · iteration

Write count_sentences(text) that counts the sentences in a piece of text. A sentence ends at a run of one or more terminator characters ., !, ? (so "Really?!" and "Wait..." each end one sentence). Text after the last terminator counts as one more sentence if it contains any non-whitespace character. A stretch that contains only whitespace between terminators is not a sentence.

Examples

Input:  text = "Hello there. How are you?! Fine..."
Output: 3

Input:  text = "no ending"
Output: 1

Input:  text = "..."
Output: 0

Constraints

  • 0 <= len(text) <= 10**5, printable ASCII and whitespace
  • Target: O(n) time

Goals

  • Recognise sentence terminators
  • Treat a run of terminators as one boundary
  • Track whether the current sentence has any content
Starting Python…