Problem:-
You are given an integer n and a string s of size n composed of lower case english letters.
You can perform the following operation on it:
- In one operation, you have to choose any character in the string s, then delete the first character to the left of the chosen character that is equal to the chosen character (if there exists) and delete the first character to the right of the chosen character that is equal to the chosen character (if there exists).
- Note that in one operation, the length of the string s is reduced by a maximum of two characters.
Task
You want to minimize the length of the string s.
Find the minimum number of operations that need to be performed to minimize the length of the string s.
Note:
- Assume 1 based indexing.
Example
Assumptions :
- n = 4
- s = "abaa" (without quotes)
Approach:
- Choose 3rd character in the string for 1st operation, this will delete the 1st character and 4th character in string s, the string becomes "ba".
- The length of the string s can not be reduced further.
- Hence, minimum number of operations needed to reduce the length of the string s to a minimum is 1.
Function Description
Complete the Minimum_Operations function provided in the editor. This function takes the following 2 parameters and returns the required answer:
- n: Represents the length of string s.
- s: Represents the string s.
Input format
Note: This is the input format that you must use to provide custom input (available above the Compile and Test button).
- The first line contains a single integer T, which denotes the number of test cases. T also specifies the number of times you have to run the Minimum_Operations function on a different set of inputs.
- For each test case:
- First line contains an integer n.
- Second line contains a string s.
Output format
For each test case in a new line, print the minimum number of operations required to minimize the length of string s.
Constraints
Code snippets (also called starter code/boilerplate code)
This question has code snippets for C, CPP, Java, and Python.
For first test case:
Given
- n = 7
- s = "babbaaa" (without quotes)
Approach:
- Choose 3rd character in the string for 1st operation, this will delete the 1st character and 4th character in string s, the string becomes "abaaa".
- Choose 3rd character in the string for 2nd operation, this will delete the 1st character and 4th character in string s, the string becomes "baa".
- Choose 3rd character in the string for 3rd operation, this will delete the 2nd character in string s, the string becomes "ba".
- The length of the string s can not be reduced further.
- Hence, the minimum number of operations needed to reduce the length of the string s to a minimum is 3.
For second test case:
Given
- n = 3
- s = "abc" (without quotes)
Approach:
- The length of the string s can not be reduced further.
- Hence, the minimum number of operations needed to reduce the length of the string s to a minimum is 0.
Code:-
Here I am going to give you two solution first one is on the basis of C language and second one is on the basis of c++ language which you can submit in c++14 and c++17 also
Solution 1 ( C language):-
Solution 2 ( C++ language):-
In this solution first three lines of the main function is only for the decreasing the time of execution of the program..
This is your choice that you want to use this or not but in some cases the code may take more time in execution and that time we need it .
keywords:-
,cannibal characters hackerearth solution,cannibal character in silence of the lambs,cannibal characters in video games,
,wrong turn cannibal characters,
,anime cannibal characters,
,dnd cannibal character,
,lego pirates cannibal character,
,cannibal holocaust characters,
,cannibal crossing characters,
,cannibal anime characters,
,cannibalism in the cars characters,
,cannibal movie characters,
,cannibal ferox characters,
,cannibal the musical characters,
,cannibalistic characters,
,cannibal in chinese characters,
,cannibals main characters,
Recommended Post:
- Swap adjacent characters
- Double the vowel characters
- Check valid parenthesis
- Print the characters with their frequencies
- Find closest value
- Word Count
- Program of CaesarCipher
- Program to find the perfect city
- Annual Day | Tech Mahindra coding question
- Find the number of pairs in the array whose sum is equal to a given target.
Full C course:-
Key points:-
- How to set limit in the floating value in python
- What is boolean data type
- How to print any character without using format specifier
- How to check that given number is power of 2 or not
- How to fix limit in double and floating numbers after dot (.) in c++
- How to print a double or floating point number in scientific notation and fixed notation
- How to take input a string in c
- How to reduce the execution time of program in c++.
Cracking the coding interview:-
Array and string:-
Tree and graph:-
Hackerearth Problems:-
- Very Cool numbers | Hacker earth solution
- Vowel Recognition | Hackerearth practice problem solution
- Birthday party | Hacker earth solution
- Most frequent | hacker earth problem solution
- program to find symetric difference of two sets
- cost of balloons | Hacker earth problem solution
- Chacha o chacha | hacker earth problem solution
- jadu and dna | hacker earth solution
- Bricks game | hacker earth problem
- Anti-Palindrome strings | hacker earth solution
- connected components in the graph | hacker earth data structure
- odd one out || hacker earth problem solution
- Minimum addition | Hackerearth Practice problem
- The magical mountain | Hackerearth Practice problem
- The first overtake | Hackerearth Practice problem
- Playing With Characters | Hackerrank practice problem solution
- Sum and Difference of Two Numbers | hackerrank practice problem solution
- Functions in C | hackerrank practice problem solution
- Pointers in C | hackerrank practice problem solution
- Conditional Statements in C | Hackerrank practice problem solution
- For Loop in C | hackerrank practice problem solution
- Sum of Digits of a Five Digit Number | hackerrank practice problem solution
- 1D Arrays in C | hackerrank practice problem solution
- Array Reversal | hackerrank practice problem solution
- Printing Tokens | hackerrank practice problem solution
- Digit Frequency | hackerrank practice problem solution
- Calculate the Nth term | hackerrank practice problem solution
Data structure:-
- Program to find cycle in the graph
- Implementation of singly link list
- Implementation of queue by using link list
- Algorithm of quick sort
- stack by using link list
- program to find preorder post order and inorder of the binary search tree
- Minimum weight of spanning tree
- Preorder, inorder and post order traversal of the tree
MCQs:-
0 Comments