Super Easy and intuitive Java solution( No need Of BigInteger or inbuilt split function)


#1

public class Solution {
public int compareVersion(String A, String B) {
ArrayList a=new ArrayList();
ArrayList b=new ArrayList();
int l=A.length();
int i=0;
int start=0;
//Implementing split funtion and also to remove starting zeroes;
while(i<l){
if(A.charAt(i)==’.’){
while(start<i-1 && A.charAt(start)==‘0’) start++;//removing starting zeroes;
String local=A.substring(start,i);
a.add(local);
start=i+1;
}
i++;
}
while(start<l-1 && A.charAt(start)==‘0’) start++;
a.add(A.substring(start,l));
i=0;
l=B.length();
start=0;
while(i<l){
if(B.charAt(i)==’.’){
while(start<i-1 && B.charAt(start)==‘0’) start++;
String local=B.substring(start,i);
b.add(local);
start=i+1;
}
i++;
}
while(start<l-1 && B.charAt(start)==‘0’) start++;
b.add(B.substring(start,l));
while(a.size()<b.size()) a.add(“0”);
while(b.size()<a.size()) b.add(“0”);
i=0;
l=a.size();
while(i<l){
int val=compare(a.get(i),b.get(i));
if(val>0){ return 1;}
else if(val<0){ return -1;}
i++;
}
return 0;
}
public static int compare(String a,String b){
if(a.length()>b.length()) return 1;
else if(a.length()<b.length()) return -1;
else return a.compareTo(b);
}
}