# 检查两个字符串数组是否等效为Leetcode解决方案

## 例子

```word1[] = {"ab", "c"}
word2[] = {"a", "bc"}```
`true`

## 代码

### 检查两个字符串数组是否等效的Leetcode解决方案的C ++代码

```#include <bits/stdc++.h>
using namespace std;

bool arrayStringsAreEqual(vector<string>& word1, vector<string>& word2) {
int i1 = 0, j1 = 0, i2 = 0, j2 = 0;
while(true){
if(word1[i1][j1] != word2[i2][j2]) return false;
if(j1 == word1[i1].size()-1)i1++, j1 = 0;
else j1++;
if(j2 == word2[i2].size()-1)i2++, j2 = 0;
else j2++;
if(i1 == word1.size() && i2 == word2.size())
return true;
else if(i1 == word1.size() || i2 == word2.size())
return false;
}
}

int main() {
vector<string> word1 = {"ab", "c"};
vector<string> word2 = {"a", "bc"};
cout<<(arrayStringsAreEqual(word1, word2) ? "true" : "false");
return 0;
}```
`true`

### 检查两个字符串数组是否等效的Java代码Leetcode解决方案

```import java.util.*;
import java.lang.*;
import java.io.*;

class Main
{
public static boolean arrayStringsAreEqual(String[] word1, String[] word2) {
int i1 = 0, j1 = 0, i2 = 0, j2 = 0;
while(true){
if(word1[i1].charAt(j1) != word2[i2].charAt(j2)) return false;
if(j1 == word1[i1].length()-1){i1++; j1 = 0;}
else j1++;
if(j2 == word2[i2].length()-1){i2++; j2 = 0;}
else j2++;
if(i1 == word1.length && i2 == word2.length)
return true;
else if(i1 == word1.length || i2 == word2.length)
return false;
}
}

public static void main (String[] args) throws java.lang.Exception
{
String[] word1 = {"ab", "c"};
String[] word2 = {"a", "bc"};
System.out.print((arrayStringsAreEqual(word1, word2) ? "true" : "false"));
return 0;
}
}```
`true`

## 复杂度分析

### 时间复杂度

O（min（N，M）），因为我们遍历了较小字符串的每个字符。 在这里，N和M分别代表第一数组和第二数组中的字符数。

### 空间复杂度

O（1），因为我们使用了恒定数量的变量。