public class CompareString { public static void main(String[] args) { StringBuilder s1 = new StringBuilder("axx#bb#c"); StringBuilder s2 = new StringBuilder("axbd#c#c"); if (compareStrings(s1, s2)) System.out.print("True"); else System.out.print("False"); } public static boolean compareStrings(StringBuilder s1, StringBuilder s2) { int countS1 = -1; int countS2 = -1; for (int i = 0; i < s1.length(); i++) { if (s1.charAt(i) == '#' && countS1 != -1) countS1 -= 1; else if (s1.charAt(i) != '#') { s1.setCharAt(countS1 + 1, s1.charAt(i)); countS1 += 1; } } for (int i = 0; i < s2.length(); i++) { if (s2.charAt(i) == '#' && countS2 != -1) countS2 -= 1; else if (s2.charAt(i) != '#') { s2.setCharAt(countS2 + 1, s2.charAt(i)); countS2 += 1; } } if (countS2 != countS1) return false; else if (countS1 == -1 && countS2 == -1) return true; else { for (int i = 0; i <= countS2; i++) { if (s1.charAt(i) != s2.charAt(i)) return false; } return true; } } }
The code works, but the teacher insisted that instead of the Boolean type there should be an int type, and that it would return not true or false, but 1 or 0
public static int compareStrings(String s1, String s2)
pls help )