Skip to content

Commit df7c80c

Browse files
committed
leetcode
1 parent fc37155 commit df7c80c

File tree

4 files changed

+318
-0
lines changed

4 files changed

+318
-0
lines changed
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
3+
-* 1657. Determine if Two Strings Are Close *-
4+
5+
Two strings are considered close if you can attain one from the other using the following operations:
6+
7+
Operation 1: Swap any two existing characters.
8+
For example, abcde -> aecdb
9+
Operation 2: Transform every occurrence of one existing character into another existing character, and do the same with the other character.
10+
For example, aacabb -> bbcbaa (all a's turn into b's, and all b's turn into a's)
11+
You can use the operations on either string as many times as necessary.
12+
13+
Given two strings, word1 and word2, return true if word1 and word2 are close, and false otherwise.
14+
15+
16+
17+
Example 1:
18+
19+
Input: word1 = "abc", word2 = "bca"
20+
Output: true
21+
Explanation: You can attain word2 from word1 in 2 operations.
22+
Apply Operation 1: "abc" -> "acb"
23+
Apply Operation 1: "acb" -> "bca"
24+
Example 2:
25+
26+
Input: word1 = "a", word2 = "aa"
27+
Output: false
28+
Explanation: It is impossible to attain word2 from word1, or vice versa, in any number of operations.
29+
Example 3:
30+
31+
Input: word1 = "cabbba", word2 = "abbccc"
32+
Output: true
33+
Explanation: You can attain word2 from word1 in 3 operations.
34+
Apply Operation 1: "cabbba" -> "caabbb"
35+
Apply Operation 2: "caabbb" -> "baaccc"
36+
Apply Operation 2: "baaccc" -> "abbccc"
37+
38+
39+
Constraints:
40+
41+
1 <= word1.length, word2.length <= 105
42+
word1 and word2 contain only lowercase English letters.
43+
44+
45+
46+
*/
47+
48+
import 'dart:collection';
49+
import 'dart:math';
50+
51+
class A {
52+
bool closeStrings(String word1, String word2) {
53+
List<int> freq1 = List.filled(26, 0);
54+
List<int> freq2 = List.filled(26, 0);
55+
for (int i = 0; i < word1.length; i++) {
56+
freq1[word1.codeUnitAt(i) - 'a'.codeUnitAt(0)]++;
57+
}
58+
for (int i = 0; i < word2.length; i++) {
59+
freq2[word2.codeUnitAt(i) - 'a'.codeUnitAt(0)]++;
60+
}
61+
for (int i = 0; i < 26; i++) {
62+
if ((freq1[i] == 0 && freq2[i] != 0) || (freq1[i] != 0 && freq2[i] == 0))
63+
return false;
64+
}
65+
freq1.sort();
66+
freq2.sort();
67+
for (int i = 0; i < 26; i++) {
68+
if (freq1[i] != freq2[i]) return false;
69+
}
70+
return true;
71+
}
72+
}
73+
74+
class B {
75+
bool closeStrings(String word1, String word2) {
76+
HashSet<String> set1 = HashSet();
77+
for (String c in word1.split("")) {
78+
set1.add(c);
79+
}
80+
for (String c in word2.split("")) {
81+
if (set1.contains(c)) {
82+
set1.remove(c);
83+
}
84+
}
85+
if (set1.length != 0) {
86+
return false;
87+
}
88+
List<int> l1 = [];
89+
List<int> l2 = [];
90+
Map<String, int> map1 = HashMap();
91+
Map<String, int> map2 = HashMap();
92+
for (String c in word1.split("")) {
93+
if (!map1.containsKey(c)) {
94+
map1[c] = 1;
95+
} else {
96+
map1[c] = (map1[c] ?? 0) + 1;
97+
}
98+
}
99+
for (String c in word2.split("")) {
100+
if (!map2.containsKey(c)) {
101+
map2[c] = 1;
102+
} else {
103+
map2[c] = (map2[c] ?? 0) + 1;
104+
}
105+
}
106+
for (int p in map1.values) {
107+
l1.add(p);
108+
}
109+
for (int p in map2.values) {
110+
l2.add(p);
111+
}
112+
// Collections.sort(l1);
113+
l1.sort();
114+
// Collections.sort(l2);
115+
l2.sort();
116+
StringBuffer sb1 = StringBuffer();
117+
StringBuffer sb2 = StringBuffer();
118+
for (int p in l1) {
119+
sb1.write(String.fromCharCode(p));
120+
}
121+
for (int p in l2) {
122+
sb2.write(String.fromCharCode(p));
123+
}
124+
return sb1.toString() == sb2.toString();
125+
}
126+
}
127+
128+
class C {
129+
bool closeStrings(String word1, String word2) {
130+
if (word1.length > word2.length || word1.length < word2.length) {
131+
return false;
132+
}
133+
List<int> w1 = List.filled(26, 0);
134+
List<int> w2 = List.filled(26, 0);
135+
int maxi = 0;
136+
for (int i = 0; i < word1.length; i++) {
137+
w1[word1.codeUnitAt(i) - 'a'.codeUnitAt(0)] += 1;
138+
maxi = max(maxi, w1[word1.codeUnitAt(i) - 'a'.codeUnitAt(0)]);
139+
}
140+
for (int i = 0; i < word2.length; i++) {
141+
w2[word2.codeUnitAt(i) - 'a'.codeUnitAt(0)] += 1;
142+
maxi = max(maxi, w2[word2.codeUnitAt(i) - 'a'.codeUnitAt(0)]);
143+
}
144+
for (int i = 0; i < 26; i++) {
145+
if ((w1[i] != 0 && w2[i] == 0) || (w1[i] == 0 && w2[i] != 0)) {
146+
return false;
147+
}
148+
}
149+
List<int> f = List.filled(maxi + 1, 0);
150+
for (int i = 0; i < 26; i++) {
151+
f[w1[i]] += 1;
152+
}
153+
for (int i = 0; i < 26; i++) {
154+
f[w2[i]] -= 1;
155+
}
156+
for (int i = 1; i <= maxi; i++) {
157+
if (f[i] > 0) {
158+
return false;
159+
}
160+
}
161+
return true;
162+
}
163+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
func closeStrings(word1 string, word2 string) bool {
4+
var ctr1, ctr2 [26]int
5+
6+
if len(word1) != len(word2) {
7+
return false
8+
}
9+
for i := 0; i < len(word1); i++ {
10+
ctr1[word1[i]-byte('a')]++
11+
ctr2[word2[i]-byte('a')]++
12+
}
13+
for i := 0; i < len(ctr1); i++ {
14+
if (ctr1[i] != 0) != (ctr2[i] != 0) {
15+
return false
16+
}
17+
}
18+
cctr := make(map[int]int)
19+
for i := 0; i < len(ctr1); i++ {
20+
cctr[ctr1[i]]++
21+
cctr[ctr2[i]]--
22+
}
23+
for _, c := range cctr {
24+
if c != 0 {
25+
return false
26+
}
27+
}
28+
return true
29+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# 🔥 Determine if Two Strings Are Close 🔥 || 3 Approaches|| Simple Fast and Easy || with Explanation
2+
3+
## Solution - 1
4+
5+
```dart
6+
class Solution {
7+
bool closeStrings(String word1, String word2) {
8+
List<int> freq1 = List.filled(26, 0);
9+
List<int> freq2 = List.filled(26, 0);
10+
for (int i = 0; i < word1.length; i++) {
11+
freq1[word1.codeUnitAt(i) - 'a'.codeUnitAt(0)]++;
12+
}
13+
for (int i = 0; i < word2.length; i++) {
14+
freq2[word2.codeUnitAt(i) - 'a'.codeUnitAt(0)]++;
15+
}
16+
for (int i = 0; i < 26; i++) {
17+
if ((freq1[i] == 0 && freq2[i] != 0) || (freq1[i] != 0 && freq2[i] == 0))
18+
return false;
19+
}
20+
freq1.sort();
21+
freq2.sort();
22+
for (int i = 0; i < 26; i++) {
23+
if (freq1[i] != freq2[i]) return false;
24+
}
25+
return true;
26+
}
27+
}
28+
```
29+
30+
## Solution - 2
31+
32+
```dart
33+
class Solution {
34+
bool closeStrings(String word1, String word2) {
35+
HashSet<String> set1 = HashSet();
36+
for (String c in word1.split("")) {
37+
set1.add(c);
38+
}
39+
for (String c in word2.split("")) {
40+
if (set1.contains(c)) {
41+
set1.remove(c);
42+
}
43+
}
44+
if (set1.length != 0) {
45+
return false;
46+
}
47+
List<int> l1 = [];
48+
List<int> l2 = [];
49+
Map<String, int> map1 = HashMap();
50+
Map<String, int> map2 = HashMap();
51+
for (String c in word1.split("")) {
52+
if (!map1.containsKey(c)) {
53+
map1[c] = 1;
54+
} else {
55+
map1[c] = (map1[c] ?? 0) + 1;
56+
}
57+
}
58+
for (String c in word2.split("")) {
59+
if (!map2.containsKey(c)) {
60+
map2[c] = 1;
61+
} else {
62+
map2[c] = (map2[c] ?? 0) + 1;
63+
}
64+
}
65+
for (int p in map1.values) {
66+
l1.add(p);
67+
}
68+
for (int p in map2.values) {
69+
l2.add(p);
70+
}
71+
l1.sort();
72+
l2.sort();
73+
StringBuffer sb1 = StringBuffer();
74+
StringBuffer sb2 = StringBuffer();
75+
for (int p in l1) {
76+
sb1.write(String.fromCharCode(p));
77+
}
78+
for (int p in l2) {
79+
sb2.write(String.fromCharCode(p));
80+
}
81+
return sb1.toString() == sb2.toString();
82+
}
83+
}
84+
```
85+
86+
## Solution - 3
87+
88+
```dart
89+
class Solution {
90+
bool closeStrings(String word1, String word2) {
91+
if (word1.length > word2.length || word1.length < word2.length) {
92+
return false;
93+
}
94+
List<int> w1 = List.filled(26, 0);
95+
List<int> w2 = List.filled(26, 0);
96+
int maxi = 0;
97+
for (int i = 0; i < word1.length; i++) {
98+
w1[word1.codeUnitAt(i) - 'a'.codeUnitAt(0)] += 1;
99+
maxi = max(maxi, w1[word1.codeUnitAt(i) - 'a'.codeUnitAt(0)]);
100+
}
101+
for (int i = 0; i < word2.length; i++) {
102+
w2[word2.codeUnitAt(i) - 'a'.codeUnitAt(0)] += 1;
103+
maxi = max(maxi, w2[word2.codeUnitAt(i) - 'a'.codeUnitAt(0)]);
104+
}
105+
for (int i = 0; i < 26; i++) {
106+
if ((w1[i] != 0 && w2[i] == 0) || (w1[i] == 0 && w2[i] != 0)) {
107+
return false;
108+
}
109+
}
110+
List<int> f = List.filled(maxi + 1, 0);
111+
for (int i = 0; i < 26; i++) {
112+
f[w1[i]] += 1;
113+
}
114+
for (int i = 0; i < 26; i++) {
115+
f[w2[i]] -= 1;
116+
}
117+
for (int i = 1; i <= maxi; i++) {
118+
if (f[i] > 0) {
119+
return false;
120+
}
121+
}
122+
return true;
123+
}
124+
}
125+
```

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
154154
- [**1207.** Unique Number of Occurrences](UniqueNumberOfOccurrences/unique_number_of_occurrences.dart)
155155
- [**392.** Is Subsequence](IsSubsequence/is_subsequence.dart)
156156
- [**1704.** Determine if String Halves Are Alike](DetermineIfStringHalvesAreAlike/determine_if_string_halves_are_alike.dart)
157+
- [**1657.** Determine if Two Strings Are Close](DetermineIfTwoStringsAreClose/determine_if_two_strings_are_close.dart)
157158

158159
## Reach me via
159160

0 commit comments

Comments
 (0)