Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
1.6 kB
2
Indexable
Never
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>

class Solution {
public:
    std::vector<std::string> tokenize(const std::string& str, char delimiter) {
        std::stringstream ss(str);
        std::string token;
        std::vector<std::string> tokens;
        while (std::getline(ss, token, delimiter)) {
            if (!token.empty()) {
                tokens.push_back(token);
            }
        }
        return tokens;
    }

    std::string relative_path(std::string string1, std::string string2) {
        std::string result = "cd ";
        
        std::vector<std::string> tokens1 = tokenize(string1, '/');
        std::vector<std::string> tokens2 = tokenize(string2, '/');
        
        int common_length = 0;
        while (common_length < std::min(tokens1.size(), tokens2.size()) && 
               tokens1[common_length] == tokens2[common_length]) {
            common_length++;
        }
        
        for (int i = common_length; i < tokens1.size(); ++i) {
            result += "../";
        }
        for (int i = common_length; i < tokens2.size(); ++i) {
            result += tokens2[i];
            if (i < tokens2.size() - 1) {
                result += "/";
            }
        }
        
        return result;
    }
};

int main() {
    Solution solution;
    std::string s1 = "/home/ubuntu/finplus/cards/order";
    std::string s2 = "/home/ubuntu/finplus/expenses/records";
    
    std::string rel_path = solution.relative_path(s1, s2);
    std::cout << rel_path << std::endl;
    
    return 0;
}