sam.gvp
BAN USERpublic class FindAstrix {
public static void main(String[] args) {
String A = "ABC*EFG", B = "ABCDE*G";
int indexOfA = A.indexOf('*'), indexOfB = B.indexOf('*');
if (A.length() != B.length() || indexOfA == -1 || indexOfB == -1 || indexOfA == indexOfB) {
System.out.println("NOT-POSSIBLE");
} else if (indexOfA > indexOfB) {
System.out.println(_findMissingChar(A, B));
} else if (indexOfA < indexOfB) {
System.out.println(_findMissingChar(B, A));
}
}
private static String _findMissingChar(String str1, String str2) {
String[] parts = str1.split("\\*");
int index = str2.indexOf(parts[0]);
if (index >= 0) {
return parts[0] + str2.charAt(index + 1) + parts[1];
} else {
index = str2.indexOf(parts[1]);
return parts[0] + str2.charAt(index - 1) + parts[1];
}
}
}
Sure the complexity is O(nlogn). To my knowledge, Number of steps is no way related to the Complexity (space/time). Complexity refers to relative performance of the program. You can write 10 lines of code that still has the complexity of O(n).
- sam.gvp August 03, 2014If the question is about the Complexity, then O(n) is the best. To be precise, O(6) (which makes no sense) since you will be comparing utmost 6 elements. Also, to my understanding people don't talk about complexity in terms of a specific integer like O(2). It has to be generic like O(n).