Skip to content

Instantly share code, notes, and snippets.

View dluciano's full-sized avatar
🎯
Focusing

Dawlin dluciano

🎯
Focusing
  • UK
View GitHub Profile
@dluciano
dluciano / MakeGood.cs
Created November 8, 2022 10:11
1544. Make The String Great
public class Solution {
public string MakeGood(string s) {
if(s.Length <= 1)
return s;
var sb = new StringBuilder(s);
var i = 1;
while(i < sb.Length){
if(Math.Abs(sb[i] - sb[i - 1]) == ('a' - 'A')){
sb.Remove(i, 1);
sb.Remove(i - 1, 1);
@dluciano
dluciano / Maximum69Number.cs
Created November 7, 2022 10:31
1323. Maximum 69 Number
public class Solution {
public int Maximum69Number (int num) {
var numBase = (int)Math.Log(num, 10);
var div = (int)Math.Pow(10, numBase);
var maxNumber = 0;
var flipped = false;
while(num > 0){
var digit = num / div;
num = num - (digit * div);
if(digit == 6 && !flipped){
@dluciano
dluciano / AddBlodTag.cs
Created November 5, 2022 21:33
616. Add Bold Tag in String
public class Solution {
public string AddBoldTag(string s, string[] words) {
BitArray FindMatches(){
var i = 0;
var bits = new BitArray(s.Length);
while(i < s.Length){
foreach(var word in words){
if(i + word.Length > s.Length)
continue;
var k = i;
@dluciano
dluciano / FindWords.cs
Created November 5, 2022 12:44
212. Word Search II
public class Solution {
private readonly List<string> _result = new();
public IList<string> FindWords(char[][] board, string[] words) {
var root = CreateTrie(words);
FindWords(board, root);
return _result;
}
static readonly (int row, int col)[] directions = new (int row, int col)[]{
@dluciano
dluciano / LongestPalindrome.cs
Created November 3, 2022 15:36
2131. Longest Palindrome by Concatenating Two Letter Words
public class Solution {
public int LongestPalindrome(string[] words) {
var freq = new Dictionary<string, int>();
var palindromes = new HashSet<string>();
var counter = 0;
foreach(var word in words){
if(!freq.ContainsKey(word))
freq[word] = 0;
freq[word]++;
@dluciano
dluciano / MinMutation.cs
Created November 2, 2022 11:26
433. Minimum Genetic Mutation
public class Solution {
public int MinMutation(string start, string end, string[] bank) {
var q = new Queue<string>();
var visited = new HashSet<string>();
var minMutation = 0;
q.Enqueue(start);
while(q.Count > 0){
var sz = q.Count;
@dluciano
dluciano / SmallestCommonElement.cs
Created November 1, 2022 21:28
1198. Find Smallest Common Element in All Rows
public class Solution {
public int SmallestCommonElement(int[][] mat) {
var cursors = new int[mat.Length];
var ROWS = mat.Length;
var COLS = mat[0].Length;
while(true){
var row = 0;
var allFound = true;
var min = -1;
while(row + 1 < ROWS){
@dluciano
dluciano / FindBall.cs
Last active November 1, 2022 20:30
1706. Where Will the Ball Fall
public class Solution {
public int[] FindBall(int[][] grid) {
var ROWS = grid.Length;
var COLS = grid[0].Length;
var result = new int[COLS];
// [ball_id, ball_pos]
var balls = new Queue<int[]>();
for(var col = 0; col < COLS; ++col)
balls.Enqueue(new int[]{ col, col });
@dluciano
dluciano / IsToeplitzMatrix.cs
Created October 31, 2022 12:37
766. Toeplitz Matrix
public class Solution {
public bool IsToeplitzMatrix(int[][] matrix) {
var ROWS = matrix.Length;
var COLS = matrix[0].Length;
for(var col = 0; col < COLS; ++col)
if(!IsEqualDiagonal(0, col, matrix))
return false;
for(var row = 0; row < ROWS; ++row)
if(!IsEqualDiagonal(row, 0, matrix))
@dluciano
dluciano / GroupAnagrams.cs
Created October 28, 2022 09:08
49. Group Anagrams
public class Solution {
public IList<IList<string>> GroupAnagrams(string[] strs) {
var groups = new List<IList<string>>();
var dict = new Dictionary<string, List<string>>();
string KeyForWord(in string word){
var freq = Freq(word);
var sb = new StringBuilder();
for(var c = 0; c < freq.Length; ++c)
sb.Append($"{c}-{freq[c]},");