Skip to content

Commit

Permalink
solution 242
Browse files Browse the repository at this point in the history
  • Loading branch information
ianoflynnautomation committed Apr 3, 2024
1 parent 5a77e5d commit 5c4b200
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 7 deletions.
49 changes: 42 additions & 7 deletions src/LeetCode.Solutions/1_TwoSum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,54 @@ namespace LeetCode.Solutions;
[Level(Level.Easy)]
public class _1_TwoSum
{
[TimeComplexity("O(n)")]
[SpaceComplexity("O(n)")]
public int[] TwoSum(int[] nums, int target)
{
var dic = new Dictionary<int, int>();
var dictionary = new Dictionary<int, int>();

for (int i = 0; i < nums.Length; i++)
{
if (dic.ContainsKey(target - nums[i]))
return new int[] { i, dic[target - nums[i]] };
else if (!dic.ContainsKey(nums[i]))
dic.Add(nums[i], i);
}
int result = target - nums[i];

return new int[2];
if (dictionary.ContainsKey(result))
return new int[] { dictionary[result], i };
else
dictionary[nums[i]] = i;
}
// no solution found.
return null;
}

[TimeComplexity("O(n log n)")]
[SpaceComplexity("O(1)")]
public int[] TwoSum2(int[] nums, int target)
{
// Sort the array
Array.Sort(nums);

// Initialize two pointers
int left = 0;
int right = nums.Length - 1;

while (left < right)
{
int sum = nums[left] + nums[right];
if (sum == target)
{
return new int[] { left, right };
}
else if (sum < target)
{
left++;
}
else
{
right--;
}
}

// No solution found
return null;
}
}
57 changes: 57 additions & 0 deletions src/LeetCode.Solutions/242_ValidAnagram.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
namespace LeetCode.Solutions;

/// <summary>
/// 242. Valid Anagram
/// https://leetcode.com/problems/valid-anagram/
/// Given two strings s and t, return true if t is an anagram of s, and false otherwise.
/// An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
/// Example 1:
/// Input: s = "anagram", t = "nagaram"
/// Output: true
/// Example 2:
/// Input: s = "rat", t = "car"
/// Output: false
/// Constraints:
/// 1 <= s.length, t.length <= 5 * 104
/// s and t consist of lowercase English letters.
/// </summary>

[Level(Level.Easy)]
public class _242_ValidAnagram
{
[TimeComplexity("O(n)")]
[SpaceComplexity("O(1)")]
public bool IsAnagram(string s, string t)
{
var dictionary = new Dictionary<char, int>();

// count the number of each character in s
foreach (char c in s)
{
if (dictionary.ContainsKey(c))
dictionary[c]++;
else
dictionary[c] = 1;
}

// decrement the count of each character in t
foreach (char c in t)
{
if (dictionary.ContainsKey(c))
{
dictionary[c]--;
if (dictionary[c] == 0)
dictionary.Remove(c);
}
else
{
// if the character is not in the dictionary, it means it is not an anagram
return false;
}
}

// if the dictionary is empty, it means all characters in s and t are the same
return dictionary.Count() == 0;
}

}
9 changes: 9 additions & 0 deletions src/LeetCode.Tests/1_TwoSum_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,13 @@ public void Case3()
result.Should().BeEquivalentTo(new int[] { 1, 0 });
}

[Test]
public void Case4()
{
var solution = new _1_TwoSum();
var result = solution.TwoSum(new int[] {1,1,1,1,1,4,1,1,1,1,1,7,1,1,1,1,1}, 11);

result.Should().BeEquivalentTo(new int[] { 5,11});
}

}
25 changes: 25 additions & 0 deletions src/LeetCode.Tests/242_ValidAnagram_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace LeetCode.Tests;

public class _242_ValidAnagram_Tests
{
[Test]
public void Case1()
{
var solution = new _242_ValidAnagram();

var result = solution.IsAnagram("anagram", "nagaram");

result.Should().BeTrue();
}

[Test]
public void Case2()
{
var solution = new _242_ValidAnagram();

var result = solution.IsAnagram("rat", "car");

result.Should().BeFalse();
}

}

0 comments on commit 5c4b200

Please sign in to comment.