@.         
                @@         
          :     :@         
          .@     @         
           :@    @+        
            +@   @@        
        `@@@;'@` +@        
       '@@'+@@@@; @        
               @@@@'       
                +@@@.      
                 `@@@+     
       :@@:@.     :@@@     
        .@@@@:     @       
          @#@@@    @:      
          .@ '@@@  @@      
           ;@  #@@@`.      
            @+  @@@@@      
             @   +@@@@:    
        @@@@@@.    +@@@+   
        @@.@@@        .    
         @,                
         `@                
          +@               
        #@@;               
       :@:@@@              
       .@ #+#@             
       `@  @ @+            
        @'  @ @            
        :@  :@@            
         @@  #;            
          @:               
           @:              
                .          
                @+         
        ;:      `@         
       +@@@      ::        
        #@@@'     @        
         :@@@@    :;       
          `@@@@:   @       
           .@@@@#  @`      
             @;@@@`+@      
              @ @@@@@      
               @ '@@#      
                @  ;       
                 @         
         @@@:    :@        
        @@@@@@:   ##       
        +  :@@@@.  @`      
         @   :@@@@ ;@      
         +@    ;@@@@@      
          @#     #@@@      
           @       +.      
        @@@@@              
       @@@@@@+             
       @   ;@@             
       #,   ;@+            
        @`   @@            
        :@# @+@            
         .@@@:`            
                           
           :               
         . #;              
        @@@@@              
       @.`#@@@             
       @    @@.            
       :#    @@            
        @#   @@            
         @@@@##            
          @@@              
                           
                           
                           
       .@@@@@#`            
       @+:;;@@@@:          
       ;@ @ @@ @@@`        
        @@@@ @@` @@#       
         ;@@  :@; :@@`     
          @+    @@  @@+    
          #@     `@+ .@#   
           @:      .@#:@;  
            @         :@   
                     
 
 

Popular Posts

Recently I was asked by my friend to write a simple code to check if two strings are anagram or not.I wrote three different approach to it in python.I was not allowed to change the input format so i had to do some extra work and write a function to get input.
Here is the question that was given:

Anagram

--------------------------
In this problem, you are given two strings S1 and S2, your task is to determine whether one string is an anagram of the other. An anagram of a string is a string obtained by permuting the letters of a string. For example aaba and aaab are anagrams, while abcd and deba are not.


Input

The first line would consist of the number of test cases 'T'. This would be followed by 'T' lines consisting of two space separated strings. The strings would consist of only letters 'a'-'z'. Each string would consist of no more than 20 characters.

Output

You have to print "YES" if one string is an anagram of the other or "NO" otherwise.

Example

Input:
2
aaba aaab
abcd deba

Output:
YES
NO

Here are the different ways of solving it 


1.
'''
Created on May 30, 2012
@author: Avi
'''
class anagramChecker:
    def __init__(self):
        pass
    def getInput(self):
        n=input()
        case=[]
        for i in range(n):
            case.append(raw_input())
        for i in range(len(case)):
            self.check(case[i])
            
    def check(self,ch):
        m1=ch.split()
        print m1
        m=[x for x in m1[0]]
        m2=[x for x in m1[1]]
        c=0
        for x in m:
            for i,y in enumerate(m2):
                if x==y:
                    m2[i]='*'
        for y in m2:
            if y=='*':
                c=c+1
        if c==len(m2)and len(m)==len(m2):
            print 'Yes'
        else:
            print 'NO'
if __name__=='__main__':
    x=anagramChecker()
    x.getInput()
    



2.

'''
Created on May 31, 2012

@author: Avi
'''
class anachecker:
    def __init__(self):
        pass
    def getInput(self):
        n=input()
        case=[]
        for i in range(n):
            case.append(raw_input())
        for i in range(len(case)):
            self.check(case[i])
    def check(self,ch):
        stlist=ch.split()
        s1list=[x for x in stlist[0]]
        s2list=[x for x in stlist[1]]
        m=set(s1list).union(set(s2list))
        if len(m)==len(set(s1list))and len(s1list)==len(s2list):
            print 'YES'
        else:
            print 'No'
if __name__=='__main__':
    x=anachecker()
    x.getInput()
        
        
        

3.
' ' '
Created on May 31, 2012

@author: Avi
'''
class anachecker3:
    def __init__(self):
        pass
    def getInput(self):
        n=input()
        case=[]
        for i in range(n):
            case.append(raw_input())
        for i in range(len(case)):
            self.check(case[i])
    def check(self,ch):
        stlist=ch.split()
        if ''.join(sorted(stlist[0]))==''.join(sorted(stlist[1])):
            print "YES"
        else:
            print "NO"
            
if __name__=='__main__':
    x=anachecker3()
    x.getInput()


        

That's all I hope the logic is not tough to understand but still if you have any doubts you can always post comments to ask. 

0 comments