VMWare Inc Interview Question
InternsCountry: United States
Interview Type: Phone Interview
If the two strings are equal, then the method must return true, else it must return false.
Yes, you would also need to check if the references of the strings are same!
For example String str1 = new "Hello";
String str2 = str1;
In this case too, the strings are equal.
I was not able to figure out how to do the locale part, any idea how to tackle that?
if(str1==null&&str2==null) return true;
if(str1==null||str2==null) return false;
if(str1.length()!=str2.length()) return false;
int index1=0;
int index2=0;
while(index1<str1.length() && index2<str2.length()) {
if(str1.charAt(index1)!=str2.charAt(index2)) return false;
index1++;
index2++;
}
return true;
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "Hello";
int found = 1;
if (str1.length() == str2.length()) {
for (int i = 0; i < str1.length(); i++) {
if (str1.charAt(i) == str2.charAt(i)) {
continue;
} else {
found = 0;
}
}
if (found == 0) {
System.out.println("Not Equal Strings");
}else{
System.out.println("Hurray the strings are equal");
}
} else {
System.out.println("Not Equal Strings");
}
}
1.read each char from first string put it in map(key will be index and value will be that char)
2.read each char from second string and read char from map as well for that index
3. if both char are not same return false;
4 and at the end if map size and second string size are not same return false;
5 return true
Can you please explain what is expected when two strings are given, I mean what kind of comparison is expected?
- Victor March 28, 2014