1662. 检查两个字符串数组是否相等-while单层循环解决
给你两个字符串数组 word1 和 word2 。如果两个数组表示的字符串相同,返回 true ;否则,返回 false 。
数组表示的字符串 是由数组中的所有元素 按顺序 连接形成的字符串。
示例 1:
输入:word1 = [“ab”, “c”], word2 = [“a”, “bc”]
输出:true
解释:
word1 表示的字符串为 “ab” + “c” -> “abc”
word2 表示的字符串为 “a” + “bc” -> “abc”
两个字符串相同,返回 true
示例 2:
输入:word1 = [“a”, “cb”], word2 = [“ab”, “c”]
输出:false
示例 3:
输入:word1 = [“abc”, “d”, “defg”], word2 = [“abcddefg”]
输出:true
对于这一题,博主写了一个很棒的代码,非常简洁易懂,代码如下,感兴趣的可以学习学习:
bool arrayStringsAreEqual(char ** word1, int word1Size, char ** word2, int word2Size){
int word1_index=0,word2_index=0;
int w1po=0,w2po=0;
while(true){
if(word1[word1_index][w1po]=='\0'){
word1_index++;
w1po=0;
}
if(word2[word2_index][w2po]=='\0'){
word2_index++;
w2po=0;
}
if(word1_index==word1Size||word2_index==word2Size){
break;
}
if(word1[word1_index][w1po]==word2[word2_index][w2po]){
w1po++;
w2po++;
}
else{
return false;
}
}
if(word1_index==word1Size&&word2_index==word2Size){
return true;
}
return false;
}