I have been solving this problem that states given two string arrays word1and word2, return true if the two arrays represent the same string, and false otherwise.
Input: word1 = ["ab", "c"], word2 = ["a", "bc"]
Output: true
Explanation:
word1 represents string "ab" + "c" -> "abc"
word2 represents string "a" + "bc" -> "abc"
The strings are the same, so return true.
In order to solve the above problem statement I have made the below java class but it is not working properly, please advise how to overcome from this
public class ArraysEqual {
public static void main(String[] args) {
List<String> l1 = new ArrayList<>(); // "a", "cb"
List<String> l2 = new ArrayList<>(); // ["ab", "c"]
l1.add("ab");
l1.add("c");
l2.add("a");
l2.add("bc");
final boolean b = arrayStringsAreEqual(l1, l2);
System.out.println(b);
}
static boolean arrayStringsAreEqual(List<String> word1, List<String> word2) {
String s = null, t = null;
int n = word1.size(), m = word2.size();
for (int i = 0; i < n; ++i)
s += word1.get(i);
for (int i = 0; i < m; ++i) t += word2.get(i);
return s == t;
}
}