題目

Given two string arrays word1 and word2, return true if the two arrays represent the same string, and false otherwise.

A string is represented by an array if the array elements concatenated in order forms the string.

Example

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.
  • 題意: 比對concat後的字串是否一樣

解法

func arrayStringsAreEqual(word1 []string, word2 []string) bool {
	return strings.EqualFold(strings.Join(word1, ""), strings.Join(word2, ""))
}
  • 加個測試,看看結果是不是如預期
func Test_arrayStringsAreEqual(t *testing.T) {
	assert.Equal(t, true, arrayStringsAreEqual([]string{
		"ab", "c",
	}, []string{
		"a", "bc",
	}))
}

參考連結