AUTHOR: github.com/Sanjay235 LOGIC: ================================================================================================ 1. For every level `i` we get the corresponding number for both version strings by iterating untill a '.' dot delimiter is encountered. 2. Now we check if number for either version strings is greater than other and return accordingly. 3. Reset the numbers of l
| 21 | ================================================================================================ |
| 22 | */ |
| 23 | class Solution { |
| 24 | public: |
| 25 | int compareVersion(string version1, string version2) { |
| 26 | |
| 27 | // Get the length of each version strings. |
| 28 | int n1 = version1.length(), n2 = version2.length(); |
| 29 | |
| 30 | // Maintain variables for corresponding level number in each version string. |
| 31 | int num1 = 0, num2 = 0; |
| 32 | |
| 33 | // Pointers for iterating two version strings. |
| 34 | int i = 0, j = 0; |
| 35 | |
| 36 | // Continue iterating till each of the version strings are traversed completely. |
| 37 | while(i < n1 || j < n2){ |
| 38 | |
| 39 | // Get the number for current level `i` of version string - 1. |
| 40 | while(i < n1 && version1[i] != '.'){ |
| 41 | num1 = num1 * 10 + version1[i]-'0'; |
| 42 | ++i; |
| 43 | } |
| 44 | |
| 45 | // Get the number for current level `i` of version string - 2. |
| 46 | while(j < n2 && version2[j] != '.'){ |
| 47 | num2 = num2 * 10 + version2[j]-'0'; |
| 48 | ++j; |
| 49 | } |
| 50 | |
| 51 | // If at anytime number for level `i` of version1 is greater than |
| 52 | // number for level `i` of version2, |
| 53 | // then we can safely return `1` version1 is greater than version 2. |
| 54 | if(num1 > num2) |
| 55 | return 1; |
| 56 | |
| 57 | // If at anytime number for level `i` of version1 is less than |
| 58 | // number for level `i` of version2, |
| 59 | // then we can safely return `-1` i.e version1 is less than version 2. |
| 60 | if(num1 < num2) |
| 61 | return -1; |
| 62 | |
| 63 | // Reset the numbers for next level `i+1` |
| 64 | num1 = num2 = 0; |
| 65 | |
| 66 | // Move ahead for next level number comparision. |
| 67 | ++i, ++j; |
| 68 | } |
| 69 | |
| 70 | // We return 0 since the numbers at all levels of version1 and version2 are same. |
| 71 | // i.e both version are same. |
| 72 | return 0; |
| 73 | } |
| 74 | }; |
nothing calls this directly
no outgoing calls
no test coverage detected