LeetCode:检查两个字符串数组是否相等


题目

给你两个字符串数组 word1word2 。如果两个数组表示的字符串相同,返回 true ;否则,返回 false

数组表示的字符串 是由数组中的所有元素 按顺序 连接形成的字符串。

示例 1:

1
2
3
4
5
6
输入:word1 = ["ab", "c"], word2 = ["a", "bc"]
输出:true
解释:
word1 表示的字符串为 "ab" + "c" -> "abc"
word2 表示的字符串为 "a" + "bc" -> "abc"
两个字符串相同,返回 true

示例 2:

1
2
输入:word1 = ["a", "cb"], word2 = ["ab", "c"]
输出:false

示例 3:

1
2
输入:word1  = ["abc", "d", "defg"], word2 = ["abcddefg"]
输出:true

提示:

  • 1 <= word1.length, word2.length <= 10^3
  • 1 <= word1[i].length, word2[i].length <= 10^3
  • 1 <= sum(word1[i].length), sum(word2[i].length) <= 10^3
  • word1[i]word2[i] 由小写字母组成

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/check-if-two-string-arrays-are-equivalent

代码

Go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package main

import (
"fmt"
)

func arrayStringsAreEqual(word1 []string, word2 []string) bool {
return wordToString(word1) == wordToString(word2)
}

func wordToString(word []string) string {
str := ""

for _, v := range word {
str += v
}

return str
}

func main() {
fmt.Println(arrayStringsAreEqual([]string{"a", "bc"}, []string{"ab", "c"}))
}

评论