Skip to content

Commit 0dcca8e

Browse files
committed
1662. Check If Two String Arrays are Equivalent: AC
1 parent 73d775b commit 0dcca8e

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,3 +1252,4 @@ mod s1656_design_an_ordered_stream;
12521252
mod s1657_determine_if_two_strings_are_close;
12531253
mod s1658_minimum_operations_to_reduce_x_to_zero;
12541254
mod s1659_maximize_grid_happiness;
1255+
mod s1662_check_if_two_string_arrays_are_equivalent;
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* [1662] Check If Two String Arrays are Equivalent
3+
*
4+
* Given two string arrays word1 and word2, return true if the two arrays represent the same string, and false otherwise.
5+
* A string is represented by an array if the array elements concatenated in order forms the string.
6+
*
7+
* Example 1:
8+
*
9+
* Input: word1 = ["ab", "c"], word2 = ["a", "bc"]
10+
* Output: true
11+
* Explanation:
12+
* word1 represents string "ab" + "c" -> "abc"
13+
* word2 represents string "a" + "bc" -> "abc"
14+
* The strings are the same, so return true.
15+
* Example 2:
16+
*
17+
* Input: word1 = ["a", "cb"], word2 = ["ab", "c"]
18+
* Output: false
19+
*
20+
* Example 3:
21+
*
22+
* Input: word1 = ["abc", "d", "defg"], word2 = ["abcddefg"]
23+
* Output: true
24+
*
25+
*
26+
* Constraints:
27+
*
28+
* 1 <= word1.length, word2.length <= 10^3
29+
* 1 <= word1[i].length, word2[i].length <= 10^3
30+
* 1 <= sum(word1[i].length), sum(word2[i].length) <= 10^3
31+
* word1[i] and word2[i] consist of lowercase letters.
32+
*
33+
*/
34+
pub struct Solution {}
35+
36+
// problem: https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/
37+
// discuss: https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/?currentPage=1&orderBy=most_votes&query=
38+
39+
// submission codes start here
40+
41+
impl Solution {
42+
pub fn array_strings_are_equal(word1: Vec<String>, word2: Vec<String>) -> bool {
43+
word1.concat() == word2.concat()
44+
}
45+
}
46+
47+
// submission codes end
48+
49+
#[cfg(test)]
50+
mod tests {
51+
use super::*;
52+
53+
#[test]
54+
fn test_1662_example_1() {
55+
let word1 = vec_string!["ab", "c"];
56+
let word2 = vec_string!["a", "bc"];
57+
58+
let result = true;
59+
60+
assert_eq!(Solution::array_strings_are_equal(word1, word2), result);
61+
}
62+
63+
#[test]
64+
fn test_1662_example_2() {
65+
let word1 = vec_string!["a", "cb"];
66+
let word2 = vec_string!["ab", "c"];
67+
68+
let result = false;
69+
70+
assert_eq!(Solution::array_strings_are_equal(word1, word2), result);
71+
}
72+
73+
#[test]
74+
fn test_1662_example_3() {
75+
let word1 = vec_string!["abc", "d", "defg"];
76+
let word2 = vec_string!["abcddefg"];
77+
78+
let result = true;
79+
80+
assert_eq!(Solution::array_strings_are_equal(word1, word2), result);
81+
}
82+
}

0 commit comments

Comments
 (0)