-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathCompareVersions.java
More file actions
69 lines (55 loc) · 1.5 KB
/
CompareVersions.java
File metadata and controls
69 lines (55 loc) · 1.5 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
package String;
/**
* Author - archit.s
* Date - 06/10/18
* Time - 2:14 PM
*/
public class CompareVersions {
public int compareVersion(String A, String B) {
int i=0;
int j=0;
int l1=A.length(),l2=B.length();
while(i < l1 && j < l2){
while(i<l1 && A.charAt(i) =='0'){
i++;
}
StringBuilder s1 = new StringBuilder();
while(i<l1 && A.charAt(i)!='.'){
s1.append(A.charAt(i));
i++;
}
while(j<l2 && B.charAt(j)=='0'){
j++;
}
StringBuilder s2 = new StringBuilder();
while(j<l2 && B.charAt(j)!='.'){
s2.append(B.charAt(j));
j++;
}
String t1 = s1.toString();
String t2 = s2.toString();
if(!t1.equals(t2)){
if(t1.length() == t2.length()){
return t1.compareTo(t2) > 0 ? 1:-1;
}
return t1.length() > t2.length() ? 1:-1;
}
i++;
j++;
}
while(i<l1 && A.charAt(i) =='0'){
i++;
}
while(j<l2 && B.charAt(j)=='0'){
j++;
}
if(i>=l1 && j>=l2){
return 0;
}
return i>j ? 1:-1;
}
public static void main(String[] args) {
System.out.println(new CompareVersions().compareVersion("01", "1"));
System.out.println(2147483647);
}
}