Skip to content

Commit 4125613

Browse files
author
Gabriel Albuquerque da Rosa
committed
[feat] 271. encode and decode strings
1 parent 4a3acdc commit 4125613

4 files changed

Lines changed: 64 additions & 1 deletion

File tree

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This repository contains solutions to various LeetCode problems implemented in C
1616
| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) | Easy | [Code](https://github.com/gabaoalb/LeetCodeCSharp/blob/main/Solutions/Problems/ContainsDuplicate.cs) |
1717
| 226 | [Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/) | Easy | [Code](https://github.com/gabaoalb/LeetCodeCSharp/blob/main/Solutions/Problems/InvertBinaryTree.cs) |
1818
| 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram/) | Easy | [Code](https://github.com/gabaoalb/LeetCodeCSharp/blob/main/Solutions/Problems/ValidAnagram.cs) |
19+
| 271 | [Encode and Decode Strings](https://leetcode.com/problems/encode-and-decode-strings/) | Medium | [Code](https://github.com/gabaoalb/LeetCodeCSharp/blob/main/Solutions/Problems/EncodeAndDecodeStrings.cs) |
1920
| 347 | [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | Medium | [Code](https://github.com/gabaoalb/LeetCodeCSharp/blob/main/Solutions/Problems/TopKFrequentElements.cs) |
2021
| 543 | [Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/) | Easy | [Code](https://github.com/gabaoalb/LeetCodeCSharp/blob/main/Solutions/Problems/DiameterOfBinaryTree.cs) |
2122
| 853 | [Car Fleet](https://leetcode.com/problems/car-fleet/) | Medium | [Code](https://github.com/gabaoalb/LeetCodeCSharp/blob/main/Solutions/Problems/CarFleet.cs) |
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using Solutions.Problems;
2+
3+
namespace Solutions.Tests.Problems;
4+
5+
public class EncodeAndDecodeStringsTest
6+
{
7+
public EncodeAndDecodeStringsSolution Solution { get; set; } = new();
8+
9+
[Fact]
10+
public void Case1()
11+
{
12+
// Arrange
13+
IList<string> strs = ["Hello", "World"];
14+
15+
// Act
16+
string encoded = Solution.Encode(strs);
17+
IList<string> decoded = Solution.Decode(encoded);
18+
19+
// Assert
20+
Assert.Equal(strs, decoded);
21+
}
22+
23+
[Fact]
24+
public void Case2()
25+
{
26+
// Arrange
27+
IList<string> strs = ["Foo", "Bar", "Baz"];
28+
29+
// Act
30+
string encoded = Solution.Encode(strs);
31+
IList<string> decoded = Solution.Decode(encoded);
32+
33+
// Assert
34+
Assert.Equal(strs, decoded);
35+
}
36+
37+
[Fact]
38+
public void Case3()
39+
{
40+
// Arrange
41+
IList<string> strs = ["", " ", " "];
42+
43+
// Act
44+
string encoded = Solution.Encode(strs);
45+
IList<string> decoded = Solution.Decode(encoded);
46+
47+
// Assert
48+
Assert.Equal(strs, decoded);
49+
}
50+
}

‎Solutions.Tests/Problems/TopKFrequentElementsTest.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace Solutions.Tests.Problems;
44

55
public class TopKFrequentElementsTest
66
{
7-
public TopKFrequentElementsSolution Solution = new();
7+
public TopKFrequentElementsSolution Solution { get; set; } = new();
88

99
[Fact]
1010
public void Case1()
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace Solutions.Problems;
2+
3+
public class EncodeAndDecodeStringsSolution
4+
{
5+
public string Encode(IList<string> strs) =>
6+
string.Join('/', strs.Select(s => $"{s.Length}/{s}"));
7+
8+
public IList<string> Decode(string s) =>
9+
[.. s.Split('/')
10+
.Chunk(2)
11+
.Select(c => c[1])];
12+
}

0 commit comments

Comments
 (0)