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, 
  1. Input string s
  2. Run a loop for char a, iterating over alphabets a-z
  3. Run a nested loop for char b, iterating over alphabets b-z
  4. for every iteration, if char a is equal to char b, we move ahead 
  5. we check whether char a or b exists in the string s, if they do, we input the value in a mew string t
  6. In the end we run alternate function to check false conditions 
  7. 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
  8. In the end we print the length of string t
Code- 



Popular posts from this blog

Finding the Subarrays (Hackerearth)

Palindrome Index (Hackerrank)

Sherlock and Array (Hackerrank)