Difference in Python and CPP Programs

 The four programs below do the exact same thing, it's just the difference of the languages.

/*************makingAnagrams(Cpp version)*************/
int makingAnagrams(string s1,string s2){
    int ans=0;
    int s1_size=s1.size();
    int s2_size=s2.size();
    vector<char> v;
    for(int i=0;i<s1_size;i++){
        int current_eCountS1=1;
        int current_eCountS2=0;
        
        int chk=0;
        for(int l=0;l<v.size();l++){
            if(s1[i]==v[l]){
                chk=1;
                break;
            }
        }
        if(chk==1){
        }
        else{
            v.push_back(s1[i]);
        }
        if(chk==0){
            for(int k=i+1;k<s1_size;k++){
                if(s1[k]==s1[i]){
                    current_eCountS1+=1;
                }
            }
            for(int z=0;z<s2_size;z++){
                if(s1[i]==s2[z]){
                    current_eCountS2+=1;
                }
            }
        }
        if(chk==0){
            if(current_eCountS2==0){                
                ans+=1;
            }
            if(current_eCountS2!=0){
                
            if(current_eCountS1>current_eCountS2){
                ans+=current_eCountS1-current_eCountS2;
            }
            else{
                ans+=current_eCountS2-current_eCountS1;
            }
            }
        }
    }
        int chk1=0;
        for(int b=0;b<s2_size;b++){
            for(int c=0;c<s1_size;c++){
                if(s2[b]==s1[c]){
                    chk1=1;
                }
            }
            if(chk1==0){
                ans+=1;
            }
        }
    cout<<"The ans is "<<ans<<endl;
    return ans;
}

/****The Below is the Python Implementation of above code****/
def makingAnagrams(s1, s2):
    '''Return the minimum number of deletions required to make 
the strings 
anagrams'''
    ans=0
    seen_elements=[]
    for elem in s1:
        if elem not in s2:
            ans+=1
        else:
            if elem not in seen_elements:
                ans+=abs(s1.count(elem)-s2.count(elem))
                seen_elements.append(elem)
    for elem in s2:
        if elem not in s1:
            ans+=1
    print("The number of deletions required are ",ans)


/**** Another Problem from HackerRank****/

/*** The Below is the Cpp version ***/
int anagram(string s) {
    int s_size=s.size();
    if(s_size%2!=0){return -1;}
    int ans=0;
    string str1=s.substr(0,s_size/2);
    string str2=s.substr(s_size/2);
    for(int i=0;i<s_size/2;i++){
        int chk=0;
        for(int j=0;j<s_size/2;j++){
            if(str1[i]==str2[j]){
                chk=1;   
                str2[j]='+';
                break; 
            }
        }
        if(chk==0){
            ans+=1;
        }
    }
    return ans;
}

/*** Python Version of the above Problem ***/
def anagram(s):
    if len(s)%2!=0:return -1
    from collections import Counter
    return (sum((Counter(s[:len(s)//2])-Counter(s[len(s)//2:])).values()))

Comments

Popular Posts