Skip to content

165. Compare Version Numbers #107

Answered by topugit
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We can use a two-pointer approach to traverse each version number and compare their segments.

  1. Split the Versions: Use explode to split each version number by the dot '.' character into arrays of segments.
  2. Traverse and Compare Segments: Use two pointers to traverse the segments of each version. Compare corresponding segments as integers, taking care of leading zeros and treating missing segments as 0.

Let's implement this solution in PHP: 165. Compare Version Numbers

<?php
function compareVersion($version1, $version2) {
    // Split the version numbers into arrays of segments
    $v1 = explode('.', $version1);
    $v2 = explode('.', $version2);
    
    // Get the maximum length to itera…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@mah-shamim
Comment options

mah-shamim Aug 19, 2024
Maintainer Author

Answer selected by mah-shamim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants