Time Conversion

  • + 0 comments

    Simple solution in C++

    `
    string timeConversion(string s) {
        int n = s.size();
        string hourStr = s.substr(0,2);
        int hour = std::stoi( hourStr);
        if (s[n-2]=='P' && hour<12) {
            hour+=12 ;
            hourStr = to_string(hour);
        }
        if (s[n-2]=='A' && hour==12) {
            hourStr = "00";
        }
        s = hourStr.append(s.substr(2,n-4));
        
        return s;
    
    }