Two Characters (Hackerrank)
Hello There! Been a long time since i posted anything here, Oh no no, I didn't forget i had a blog, Just got really busy preparing for my Semester end exams and now i am free!
So today we have a question with us from Hackerrank based on String Manipulations.
Question-
Approach- First we define all the conditions which will result into a failed attempt to solve the conditions mentioned, by creating a function called - Bool alternate which gives us False conditions if 2 characters chosen are equal, do not belong in the original string or size constraints are not met.
Next, in the main function,
- Input string s
- Run a loop for char a, iterating over alphabets a-z
- Run a nested loop for char b, iterating over alphabets b-z
- for every iteration, if char a is equal to char b, we move ahead
- we check whether char a or b exists in the string s, if they do, we input the value in a mew string t
- In the end we run alternate function to check false conditions
- After running the loop for all iterations and combinations possible, string t contains exactly 2 characters, char a and b which have alternating values, ex - ababababab or efefefefef
- In the end we print the length of string t
Code-