Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Compare Version Numbers Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Compare Version Numbers | Practice Problem

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given two version numbers, compare them. 

A version number consists of one or more revisions connected by a dot. Each revision consists of digits and may contain leading zeroes. Each revision consists of at least one digit. 

Revisions are 0-indexed from left to right. 

To compare two versions, compare revisions in the left-to-right order. Revisions are compared using their integer value ignoring any leading zeroes.

Approach

Since there are multiple dots in the given versions, comparing them directly is not possible. Therefore to compare the two versions we have to compare the substrings which are partitioned by dots.

  • Traverse both the strings simultaneously and store each string’s corresponding substring (excluding the leading zeroes).
  • If the length of the substring of s1 is greater than the length of the substring of s2, return 1
  • Else if the length of the substring of s1 is less than then the length of the substring of s2, return -1.
  • If both the substrings have equal length:
    • compare the elements of both the substrings
    • if at any point the value of two corresponding sections differs then return the answer accordingly.
  • If both the substrings are identical, move to the next substrings.
  • Return 0 as the default case.

Analysis

  • Time Complexity: O(n)
  • Auxiliary Space Complexity: O(1)

Implementation

C++
int compareVersion(string version1, string version2) {
   string subVersion1 = "", subVersion2 = "";
   int i = 0, j = 0;
   while (i < version1.size() || j < version2.size()) {
       while (i < version1.size() && version1[i] == '0') {
           i++;
       }
       while (i < version1.size() && version1[i] != '.') {
           subVersion1 += version1[i];
           i++;
       }
       while (j < version2.size() && version2[j] == '0') {
           j++;
       }
       while (j < version2.size() && version2[j] != '.') {
           subVersion2 += version2[j];
           j++;
       }
       if (subVersion1.size() > subVersion2.size()) {
           return 1;
       } else if (subVersion1.size() < subVersion2.size()) {
           return -1;
       }
       for (int k = 0; k < subVersion1.size(); k++) {
           if (subVersion1[k] > subVersion2[k]) {
               return 1;
           } else if (subVersion1[k] < subVersion2[k]) {
               return -1;
           }
       }
       subVersion1 = "";
       subVersion2 = "";
       i++;
       j++;
   }
   return 0;
}
Java
class Solution {
   int compareVersion(String version1, String version2) {
       String subVersion1 = "", subVersion2 = "";
       int i = 0, j = 0;
       while (i < version1.length() || j < version2.length()) {
           while (i < version1.length() && version1.charAt(i) == '0') {
               i++;
           }
           while (i < version1.length() && version1.charAt(i) != '.') {
               subVersion1 += version1.charAt(i);
               i++;
           }
           while (j < version2.length() && version2.charAt(j) == '0') {
               j++;
           }
           while (j < version2.length() && version2.charAt(j) != '.') {
               subVersion2 += version2.charAt(j);
               j++;
           }
           if (subVersion1.length() > subVersion2.length()) {
               return 1;
           } else if (subVersion1.length() < subVersion2.length()) {
               return -1;
           }
           for (int k = 0; k < subVersion1.length(); k++) {
               if (subVersion1.charAt(k) > subVersion2.charAt(k)) {
                   return 1;
               } else if (subVersion1.charAt(k) < subVersion2.charAt(k)) {
                   return -1;
               }
           }
           subVersion1 = "";
           subVersion2 = "";
           i++;
           j++;
       }
       return 0;
   }
}
Related Content
Anagrams
Count And Say
Integer to Roman Numeral
Longest Common Prefix
Longest Palindrome in String
Insert Minimum To Make Palindrome
Reverse Words in String
Roman Numeral to Integer
Substring Search - I
Substring Search - III
Substring Search - II
Substring Search - IV
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet