carloscn / structstudy

Leetcode daily trainning by using C/C++/RUST programming.
4 stars 1 forks source link

leetcode1662:检查两个字符串数组是否相等(check-if-two-string-arrays-are-equivalent) #262

Open carloscn opened 1 year ago

carloscn commented 1 year ago

问题描述

给你两个字符串数组 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  

提示:

1 <= word1.length, word2.length <= 103 1 <= word1[i].length, word2[i].length <= 103 1 <= sum(word1[i].length), sum(word2[i].length) <= 103 word1[i] 和 word2[i] 由小写字母组成

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

carloscn commented 1 year ago

问题分析

pub fn array_strings_are_equal(word1: Vec<&str>, word2: Vec<&str>) -> bool
{
    if word1.len() < 1 || word2.len() < 1 {
        return false;
    }

    let (mut i_1, mut i_2, mut j_1, mut j_2) = (0, 0, 0, 0);
    let mut e_arr_1:Vec<char> = word1[i_1].chars().collect();
    let mut e_arr_2:Vec<char> = word2[i_2].chars().collect();

    while i_1 < word1.len() || i_2 < word2.len() {

        if e_arr_1[j_1] != e_arr_2[j_2] {
            return false;
        }

        j_1 += 1;
        if j_1 == e_arr_1.len() {
            j_1 = 0;
            i_1 += 1;
            if i_1 < word1.len() {
                e_arr_1 = word1[i_1].chars().collect();
            }
        }

        j_2 += 1;
        if j_2 == e_arr_2.len() {
            j_2 = 0;
            i_2 += 1;
            if i_2 < word2.len() {
                e_arr_2 = word2[i_2].chars().collect();
            }
        }
    }

    return true;
}
carloscn commented 1 year ago

code

https://review.gerrithub.io/c/carloscn/structstudy/+/556255 https://github.com/carloscn/structstudy/commit/dfe5b43e52526140cd62665bc4d7d5af20f60404