Access time
Anonymous User
35
May 13, 2021
May 13, 2021

Does defining a function as a private member of a class makes the code faster than defining that same function as a public member?
For e.g.
This code was not getting submitted due to runtime error.

class Solution {
public:
     vector<string> helper(string num) {
        vector<string> res;
        int n = num.size();
        
        if (n == 1) 
            return {num};
        
        if (num[0] != '0') 
            res.push_back(num);
        
        if (num[0] == '0') {
            if (num.back() == '0') return {0};
            return {"0." + num.substr(1)};
        }
        
        for (int i = 1; i < n; i++) {
            if (num.substr(i).back() == '0') continue;
            res.push_back(num.substr(0, i) + "." + num.substr(i));
        }
        
        return res;
    }

    
    
    vector<string> ambiguousCoordinates(string s) {
        
        vector<string> answer;
        
        s= s.substr(1,s.length()-2);
        
        if(s.length()==0 || s.length()==1)
        {
            answer.push_back(s);
            return answer;
        }
        
        for(int i=1;i<s.length();i++)
        {
            vector<string> left = helper(s.substr(0,i));
            vector<string> right = helper(s.substr(i));
            
            for (int j = 0; j < left.size(); j++) {
                for (int k = 0; k < right.size(); k++) {
                    answer.push_back("(" + left[j] + ", " + right[k] + ")");
                }
            }
        }
        
        return answer;
    }
};

However, the code was submitted after the function 'helper' was defined as the private member of the class.

class Solution {
public:
   
    vector<string> ambiguousCoordinates(string s) {
        
        vector<string> res;
        
        string s2= s.substr(1,s.size()-2);
        
        int n = s2.size();
        for (int i = 1; i < n; i++) {
            vector<string> first = helper(s2.substr(0, i));
            vector<string> second = helper(s2.substr(i));
            
            for (int j = 0; j < first.size(); j++) {
                for (int k = 0; k < second.size(); k++) {
                    res.push_back("(" + first[j] + ", " + second[k] + ")");
                }
            }
        }
        
        return res;
    }
    
    
     private:    vector<string> helper(string num) {
        vector<string> res;
        int n = num.size();
        
        if (n == 1) 
            return {num};
        
        if (num[0] != '0') 
            res.push_back(num);
        
        if (num[0] == '0') {
            if (num.back() == '0') return {};
            return {"0." + num.substr(1)};
        }
        
        for (int i = 1; i < n; i++) {
            if (num.substr(i).back() == '0') continue;
            res.push_back(num.substr(0, i) + "." + num.substr(i));
        }
        
        return res;
    }

    
    
};

For reference: This question is 816. Ambiguous coordinates.

Comments (1)