|
| 1 | +/** |
| 2 | + * Question: Compare Version Numbers |
| 3 | + * Link: https://leetcode.com/problems/compare-version-numbers/ |
| 4 | + * |
| 5 | + * Description: |
| 6 | + * Given two version strings, version1 and version2, compare them. |
| 7 | + * A version string consists of revisions separated by dots '.'. |
| 8 | + * Each revision is an integer ignoring leading zeros. |
| 9 | + * |
| 10 | + * Return: |
| 11 | + * - If version1 < version2, return -1 |
| 12 | + * - If version1 > version2, return 1 |
| 13 | + * - Otherwise, return 0 |
| 14 | + * |
| 15 | + * ------------------------------------------------------------------ |
| 16 | + * Approach: |
| 17 | + * 1. Split both version strings into integer lists by '.'. |
| 18 | + * 2. Normalize lengths by padding the shorter list with zeros. |
| 19 | + * 3. Compare the lists index by index: |
| 20 | + * - If any revision differs, return -1 or 1 accordingly. |
| 21 | + * - If all are equal, return 0. |
| 22 | + * |
| 23 | + * ------------------------------------------------------------------ |
| 24 | + * Dry Run: |
| 25 | + * version1 = "1.01", version2 = "1.001" |
| 26 | + * list1 = [1, 1], list2 = [1, 1] |
| 27 | + * Compare: |
| 28 | + * index 0: 1 == 1 |
| 29 | + * index 1: 1 == 1 |
| 30 | + * Final → return 0 |
| 31 | + * |
| 32 | + * version1 = "1.0.1", version2 = "1" |
| 33 | + * list1 = [1, 0, 1], list2 = [1, 0, 0] (after padding) |
| 34 | + * Compare: |
| 35 | + * index 0: 1 == 1 |
| 36 | + * index 1: 0 == 0 |
| 37 | + * index 2: 1 > 0 → return 1 |
| 38 | + * |
| 39 | + * ------------------------------------------------------------------ |
| 40 | + * Time Complexity: |
| 41 | + * O(n + m) where n = length of version1, m = length of version2 |
| 42 | + * (we parse both strings fully once). |
| 43 | + * |
| 44 | + * Space Complexity: |
| 45 | + * O(n + m) for storing split parts in two lists. |
| 46 | + */ |
| 47 | + |
| 48 | +import java.util.*; |
| 49 | + |
| 50 | +class Solution { |
| 51 | + public int compareVersion(String version1, String version2) { |
| 52 | + List<Integer> list1 = new ArrayList<>(); |
| 53 | + StringBuilder st = new StringBuilder(); |
| 54 | + for (int i = 0; i < version1.length(); i++) { |
| 55 | + char ch = version1.charAt(i); |
| 56 | + if (ch == '.') { |
| 57 | + list1.add(Integer.parseInt(st.toString())); |
| 58 | + st = new StringBuilder(); |
| 59 | + } else { |
| 60 | + st.append(ch); |
| 61 | + } |
| 62 | + } |
| 63 | + list1.add(Integer.parseInt(st.toString())); |
| 64 | + |
| 65 | + List<Integer> list2 = new ArrayList<>(); |
| 66 | + StringBuilder st2 = new StringBuilder(); |
| 67 | + for (int i = 0; i < version2.length(); i++) { |
| 68 | + char ch = version2.charAt(i); |
| 69 | + if (ch == '.') { |
| 70 | + list2.add(Integer.parseInt(st2.toString())); |
| 71 | + st2 = new StringBuilder(); |
| 72 | + } else { |
| 73 | + st2.append(ch); |
| 74 | + } |
| 75 | + } |
| 76 | + list2.add(Integer.parseInt(st2.toString())); |
| 77 | + |
| 78 | + if (list1.size() > list2.size()) { |
| 79 | + for (int i = 0; i < list1.size() - list2.size()+1; i++) { |
| 80 | + list2.add(0); |
| 81 | + } |
| 82 | + } else if (list1.size() < list2.size()) { |
| 83 | + for (int i = 0; i < list2.size() - list1.size()+1; i++) { |
| 84 | + list1.add(0); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + int i = 0, j = 0; |
| 89 | + while (i < list1.size() && j < list2.size()) { |
| 90 | + int num1 = list1.get(i); |
| 91 | + int num2 = list2.get(j); |
| 92 | + if (num1 < num2) { |
| 93 | + return -1; |
| 94 | + } else if (num1 > num2) { |
| 95 | + return 1; |
| 96 | + } |
| 97 | + i++; |
| 98 | + j++; |
| 99 | + } |
| 100 | + return 0; |
| 101 | + } |
| 102 | +} |
0 commit comments