-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion165.java
More file actions
81 lines (68 loc) · 1.79 KB
/
Copy pathQuestion165.java
File metadata and controls
81 lines (68 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
Author: Ananthanarayanan R
Section: Algorithms
Question: 165
*/
import java.util.*;
public class Question165
{
public static void removeLeadingZeros(String[] versionRevisions)
{
for(int i = 0;i<versionRevisions.length;i++)
{
StringBuilder sb = new StringBuilder(versionRevisions[i]);
while(sb.charAt(0)=='0' && sb.length()>1)
sb.deleteCharAt(0);
versionRevisions[i] = sb.toString();
}
}
public static List<Integer> extractRevisions(String version)
{
String[] versionRevisions = version.split("\\.");
removeLeadingZeros(versionRevisions);
//Create a List
List<Integer> revisionList = new ArrayList<>();
for(String revision:versionRevisions)
revisionList.add(Integer.parseInt(revision));
return revisionList;
}
public static int compareVersion(String version1, String version2)
{
List<Integer> list1 = extractRevisions(version1);
List<Integer> list2 = extractRevisions(version2);
int diff = Math.abs(list1.size()-list2.size());
//Match the size of the two lists
if(list1.size()<list2.size())
{
//add 0s to list1 till size is the same
for(int i = 0;i<diff;i++)
{
list1.add(0);
}
}
else if(list2.size()<list1.size())
{
//add 0s to list1 till size is the same
for(int i = 0;i<diff;i++)
{
list2.add(0);
}
}
//Compare the elements of the two lists from the left
for(int i = 0;i<list1.size();i++)
{
if(list1.get(i)<list2.get(i))
return -1;
else if(list1.get(i)>list2.get(i))
return 1;
}
return 0;
}
public static void main(String[] args)
{
String version1 = "2.5.33";
String version2 = "2.0006.00001.02.0.3.7";
int result = compareVersion(version1,version2);
System.out.println(result);
}
}