Dsa
Backtracking
Medium
String Partition
Given a string s consisting of lowercase letters, your task is to partition this string into substrings such that each substring must contain distinct characters. Write a program that counts all possible ways to partition the string into such substrings.
Input format:
- A single line containing the string s (1 ≤ |s| ≤ 15).
Output format:
- A single integer representing the total number of distinct partitions.
Example:
Input:
abbc
Output:
5
In this example, the valid partitions are: ['a', 'b', 'b', 'c'], ['a', 'b', 'bc'], ['ab', 'b', 'c'], ['a', 'bb', 'c'], ['abb', 'c'].
Key concepts
backtrackingstring manipulationsubsets
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now