Hackerrank String Reduction Solution

This is the solution to the program, solved in python. The Question can be found in the Algorithm domain of Hackerrank.
Problem Statement:
Given a string consisting of letters, a, b and c, we can perform the following operation: Take any two adjacent distinct characters and replace them with the third character. For example, if 'a' and 'c' are adjacent, they can replaced by 'b'. Find the smallest string which we can obtain by applying this operation repeatedly?

Screenshots[Demo]



The Code:
def cnt(x):
     m={}
     m['a']=0;m['b']=0;m['c']=0
     for c in x:
  if c not in m:
             m[c]=1
  else:
      m[c]+=1
     return m
def red(x):
     xm=cnt(x)
     print x,xm
     if((xm['a']==0 and xm['b']==0 ) or (xm['a']==0 and xm['c']==0 ) or (xm['b']==0 and xm['c']==0)):
          return len(x);
     if((xm['a']%2==0 and xm['b']%2==0 and xm['c']%2==0) or (xm['a']%2==1 and xm['b']%2==1 and xm['c']%2==1)):
   return 2
     return 1
for i in range(0,input()):
     x=raw_input()
     #print x
     print red(x)


Found Bugs, Comment them here !

Comments

Popular posts from this blog

Non Restoring Division Algorithm Implementation in C

Bit Stuffing Code Implementation in Java

Hackerrank Modified Kaprekar Numbers Solution