This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)[]{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]++; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]},"); |