Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Count Sub-sequences in a String
00:00
5 left

Count Sub-sequences in a String

MediumPython

Problem

Write a program to count sub-sequences in a string.

For this task, count all subsequences containing exactly two characters. A subsequence is formed by choosing two distinct character positions while preserving their order. Characters with the same value at different positions are counted separately.

Input: A string s.

Output: Return the number of length-two subsequences as an integer.

For a string of length n, every pair of positions forms one valid subsequence.

Constraints

  • 0 <= len(s) <= 10^5
  • s contains any standard Python string characters
  • Subsequences are counted by index selection, not by distinct resulting text

Function Signature

def count_subsequences(s):
Interviewer

Your question is Count Sub-sequences 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.