Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Split String
EASY
12 mins
Greedy
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Greedy
-
-
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Split String

Contributed by
Tushar Gupta
Easy
yellow-spark
0/40
Avg time to solve 12 mins
Success Rate 80 %
Share
0 upvotes

Problem Statement

You are given a string 'S'. A string is called a split string if it follows the conditions define below.

1. It can be divided into 'K' contiguous substrings such that the first letter of these substrings is the same for all the substrings.
2. It can be divided into 'K' contiguous substrings such that the first letter of these substrings is distinct for all the substrings.

Return 1 if 'S' is a split string otherwise return 0.

For Example:-
Let 'N' = 6, 'K' = 2, 'S' = "pppdpa".
It can be divided into "pp" and "pdpa", the first letter of the strings is same.
It can be divided into "ppp" and "dpa", the first letter of the strings is distinct.
So our answer is 1. 
Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1:-
2
3 4 
xyz
5 3
abaca
Sample Output 1:-
0
1
Explanation of sample input 1:-
First test case:-
It can be proven that the string 'S' cannot follow the two conditions.
So our answer is 0. 

Second test case:-
It can be divided into "ab", "ac", and "a", the first letter of the strings is the same.
It can be divided into "a", "ba", and "ca", the first letter of the strings is distinct.
So our answer is 1. 
Sample Input 2:-
2
4 1
abcb
5 3
pqpsr
Sample Output 2:-
1
0
Reset Code
Full screen
Auto
copy-code
Console