Skip to content

Commit 290fed9

Browse files
committedOct 25, 2022
leetcode
1 parent 65ebad4 commit 290fed9

4 files changed

+204
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
3+
4+
-* Check If Two String Arrays are Equivalent *-
5+
6+
7+
Given two string arrays word1 and word2, return true if the two arrays represent the same string, and false otherwise.
8+
9+
A string is represented by an array if the array elements concatenated in order forms the string.
10+
11+
12+
13+
Example 1:
14+
15+
Input: word1 = ["ab", "c"], word2 = ["a", "bc"]
16+
Output: true
17+
Explanation:
18+
word1 represents string "ab" + "c" -> "abc"
19+
word2 represents string "a" + "bc" -> "abc"
20+
The strings are the same, so return true.
21+
Example 2:
22+
23+
Input: word1 = ["a", "cb"], word2 = ["ab", "c"]
24+
Output: false
25+
Example 3:
26+
27+
Input: word1 = ["abc", "d", "defg"], word2 = ["abcddefg"]
28+
Output: true
29+
30+
31+
Constraints:
32+
33+
1 <= word1.length, word2.length <= 103
34+
1 <= word1[i].length, word2[i].length <= 103
35+
1 <= sum(word1[i].length), sum(word2[i].length) <= 103
36+
word1[i] and word2[i] consist of lowercase letters.
37+
38+
39+
*/
40+
41+
class A {
42+
// Runtime: 422 ms, faster than 83.33% of Dart online submissions for Check If Two String Arrays are Equivalent.
43+
// Memory Usage: 141.7 MB, less than 33.33% of Dart online submissions for Check If Two String Arrays are Equivalent.
44+
bool arrayStringsAreEqual(List<String> word1, List<String> word2) {
45+
return word1.join("") == word2.join("");
46+
}
47+
}
48+
49+
class B {
50+
// Runtime: 505 ms, faster than 33.33% of Dart online submissions for Check If Two String Arrays are Equivalent.
51+
// Memory Usage: 141.4 MB, less than 66.67% of Dart online submissions for Check If Two String Arrays are Equivalent.
52+
bool arrayStringsAreEqual(List<String> word1, List<String> word2) {
53+
int wordIdx1 = 0, wordIdx2 = 0, chIdx1 = 0, chIdx2 = 0;
54+
while (true) {
55+
int ch1 = word1[wordIdx1][chIdx1].codeUnitAt(0);
56+
int ch2 = word2[wordIdx2][chIdx2].codeUnitAt(0);
57+
if (ch1 != ch2) return false;
58+
59+
chIdx1++;
60+
chIdx2++;
61+
if (chIdx1 == word1[wordIdx1].length) {
62+
wordIdx1++;
63+
chIdx1 = 0;
64+
}
65+
if (chIdx2 == word2[wordIdx2].length) {
66+
wordIdx2++;
67+
chIdx2 = 0;
68+
}
69+
70+
if (wordIdx1 == word1.length && wordIdx2 == word2.length) break;
71+
if (wordIdx1 == word1.length || wordIdx2 == word2.length) return false;
72+
}
73+
return true;
74+
}
75+
}
76+
77+
class C {
78+
bool arrayStringsAreEqual(List<String> word1, List<String> word2) {
79+
int firstPointer = 0, secondPointer = 0; // inner pointer
80+
int firstWord = 0, secondWord = 0; // outer pointer
81+
82+
while (firstWord < word1.length && secondWord < word2.length) {
83+
String firstString = word1[firstWord], secondString = word2[secondWord];
84+
85+
if (firstString.codeUnitAt(firstPointer) !=
86+
secondString.codeUnitAt(secondPointer)) return false;
87+
88+
if (firstPointer < firstString.length - 1) {
89+
firstPointer++;
90+
} else {
91+
firstPointer = 0;
92+
firstWord++;
93+
}
94+
95+
if (secondPointer < secondString.length - 1) {
96+
secondPointer++;
97+
} else {
98+
secondPointer = 0;
99+
secondWord++;
100+
}
101+
}
102+
103+
return firstWord == word1.length && secondWord == word2.length;
104+
}
105+
}
106+
107+
class D {
108+
bool arrayStringsAreEqual(List<String> word1, List<String> word2) {
109+
StringBuffer sb1 = StringBuffer();
110+
StringBuffer sb2 = StringBuffer();
111+
for (String s in word1) {
112+
sb1.write(s);
113+
}
114+
for (String s in word2) {
115+
sb2.write(s);
116+
}
117+
return sb1.toString() == (sb2.toString());
118+
}
119+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
import "strings"
4+
5+
func arrayStringsAreEqual(word1 []string, word2 []string) bool {
6+
return strings.Join(word1, "") == strings.Join(word2, "")
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# 🔥 Check If Two String Arrays are Equivalent 🔥 || 3 Solutions || Simple Fast and Easy || with Explanation
2+
3+
## Approach Used: Arrays traversal, String comparison
4+
5+
### Explanation
6+
7+
This a very basic question. It is a beginner-friendly question which requires not much logic. We simply need to combine all the substrings in the array into one combines string for both the arrays. The order of combining should not change. After we get both the combined substring, we’ll simply compare them and return true is all the characters match, otherwise false.
8+
9+
### Complexity
10+
11+
#### Space Complexity: O(m+n) where m and n will be total characters present in array1 and array2 respectively
12+
13+
#### Time Complexity: O(a+b) where a and b are the length of both arrays
14+
15+
## Solution - 1
16+
17+
```dart
18+
class Solution {
19+
// Runtime: 422 ms, faster than 100.00% of Dart online submissions for Check If Two String Arrays are Equivalent.
20+
// Memory Usage: 141.7 MB, less than 33.33% of Dart online submissions for Check If Two String Arrays are Equivalent.
21+
bool arrayStringsAreEqual(List<String> word1, List<String> word2) {
22+
return word1.join("") == word2.join("");
23+
}
24+
}
25+
```
26+
27+
## Solution - 2
28+
29+
```dart
30+
class Solution {
31+
bool arrayStringsAreEqual(List<String> word1, List<String> word2) {
32+
int firstPointer = 0, secondPointer = 0; // inner pointer
33+
int firstWord = 0, secondWord = 0; // outer pointer
34+
35+
while (firstWord < word1.length && secondWord < word2.length) {
36+
String firstString = word1[firstWord], secondString = word2[secondWord];
37+
38+
if (firstString.codeUnitAt(firstPointer) !=
39+
secondString.codeUnitAt(secondPointer)) return false;
40+
41+
if (firstPointer < firstString.length - 1) {
42+
firstPointer++;
43+
} else {
44+
firstPointer = 0;
45+
firstWord++;
46+
}
47+
48+
if (secondPointer < secondString.length - 1) {
49+
secondPointer++;
50+
} else {
51+
secondPointer = 0;
52+
secondWord++;
53+
}
54+
}
55+
56+
return firstWord == word1.length && secondWord == word2.length;
57+
}
58+
}
59+
```
60+
61+
## Solution - 3
62+
63+
```dart
64+
class Solution {
65+
bool arrayStringsAreEqual(List<String> word1, List<String> word2) {
66+
StringBuffer sb1 = StringBuffer();
67+
StringBuffer sb2 = StringBuffer();
68+
for (String s in word1) {
69+
sb1.write(s);
70+
}
71+
for (String s in word2) {
72+
sb2.write(s);
73+
}
74+
return sb1.toString() == (sb2.toString());
75+
}
76+
}
77+
```

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
107107
- [Minimum Window Substring](MinimumWindowSubstring/minimum_window_substring.dart)
108108
- [Set Mismatch](SetMismatch/set_mismatch.dart)
109109
- [Maximum Length of a Concatenated String with Unique Characters](MaximumLengthOfAConcatenatedStringWithUniqueCharacters/maximum_length_of_a_concatenated_string_with_unique_characters.dart)
110+
- [Check If Two String Arrays are Equivalent](CheckIfTwoStringArraysAreEquivalent/check_if_two_string_arrays_are_equivalent.dart)
110111

111112
## Reach me via
112113

0 commit comments

Comments
 (0)
Please sign in to comment.