MCPcopy Create free account
hub / github.com/Vishruth-S/CompetitiveCode / Solution

Class Solution

LeetCode_problems/Compare Version Number/solution.cpp:15–59  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

13
14
15class Solution {
16public:
17 int compareVersion(string version1, string version2) {
18 string s = "", t = "";
19 vector<int> v1,v2;
20
21 for(char c: version1){
22 if(c != '.'){
23 s += c; // store each number substring from original string.
24 }
25 else{
26 v1.push_back(stoi(s)); // if dot('.') convert string to number and append it to the vector, clear the string.
27 s = "";
28 }
29 }
30 v1.push_back(stoi(s)); // for last set of number substring
31
32 for(char c: version2){
33 if(c != '.'){
34 t += c;
35 }
36 else{
37 v2.push_back(stoi(t));
38 t = "";
39 }
40 }
41 v2.push_back(stoi(t));
42
43 while(v1.size() < v2.size()){
44 v1.push_back(0); // if size of v1 is less than v2 then append extra zeros(0) to v1
45 }
46
47 while(v2.size() < v1.size()){
48 v2.push_back(0); // if size of v2 is less than v1 then append extra zeros(0) to v2
49 }
50
51 for(int i = 0; i < v1.size(); i++){ // compare the vector index wise
52 if(v1[i] < v2[i])
53 return -1; // if at any index, value of v1 is less than v2 return -1
54 else if(v2[i] < v1[i])
55 return 1; // if at any index, value of v2 is less than v1 return 1
56 }
57 return 0; // if all the values at all the indices are equal then return 0
58 }
59};

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected