What the Python!


It's 9 in the morning, I just got a fairly interesting problem on HackerRank problem solving section, it's an easy level problem but the setup is a little bit dubious which can maybe confusing for some people if they don't focus on the main requirement of the problem, well I was lucky that I got the idea just on seeing the problem statement. 

It's the "String Construction" Problem of HackerRank Problem Solving Section.

You can check it out and try to solve on your own, just focus on the main idea of the problem. Don't read further if you still haven't visited the problem. 

So, the problem sets the scene by introducing us with 2 strings, we just need to copy the 1st string in the 2nd, with 2 rules, firstly, we'll have to pay 1 dollar when copying a char from str1 to str2, and secondly when copying char or any substr from 2nd str into it only, it will be free of cost. We have to return the minimum cost that we will have to pay for copying the 1st str into 2nd. 

The main thing to understand here is that we just need to get all the chars from str1 to str2 just once(each) and then to make the str2 same as str1 we can use them as required from the str2 only which will be free of cost, so we don't have to worry about that. It's just that we need the no. of unique elements of str1 because we will have to pay 1 dollar for each of the unique elems of str1. So, in my algos I am just finding the number of unique elems in str1, as in Python because of sets, it becomes easy as hell, whereas in Cpp I had to check for every elem if I have touched that elem before and if not then just count it as a new elem and add its cost.

I think I'll be taking few more problems for today, yesterday was quite a headache, I worked so much to make an efficient algo for the length of palindromic substr, but at the end still got TLC! It's still unclear what I should do with it.

Just look at the difference between the lines of code between the two languages.

/** Python Version **/
def stringConstruction(s):
    return len(set(s))

/** Cpp Version-1 **/
int stringConstruction(string s) {
    int ans=0;
    int s_size=s.size();
    for(int i=0;i<s_size;i++){
        int chk=0;
        if(i!=0){
            for(int j=i-1;j>=0;j--){
                if(s[i]==s[j]){
                    chk=1;
                    break;
                }
            }   
        }
        if(chk==0){
            ans++;
        }
    }
    return ans;
}

/** Cpp Version-2 **/
int stringConstruction(string s) {
    vector<char> vec;
    int s_size=s.size();
    for(int i=0;i<s_size;i++){
        int chk=0;
        for(int j=0;j<vec.size();j++){
            if(s[i]==vec[j]){
                chk=1;
                break;
            }
        }
        if(chk==0){
            vec.push_back(s[i]);
        }
    }
    return vec.size();
}


/** Another SuperEasy Python Program **/
"""The below idea would have required atleast 20 lines of code in C++"""
def marcsCakewalk(calorie):
    return sum([pow(2,i)*sorted(calorie,reverse=True)[i] for i in range(len(calorie))])

Comments

Popular Posts