Monday, February 15, 2021

Leetcode Everyday: 1678. Goal Parser Interpretation. Easy

public class Solution {
    public string Interpret(string command) {
        var replaceWith = new Dictionary<string, string>() 
        {
            ["()"] = "o",
            ["(al)"] = "al"
        };
        foreach (var (key, value) in replaceWith) {
            command = command.Replace(key, value);
        }
        return command;
    }
}
Source: https://leetcode.com/problems/goal-parser-interpretation/submissions/

Leetcode Everyday: 1720. Decode XORed Array. Easy

public class Solution {
    public int[] Decode(int[] encoded, int first) {
        var list = new List<int>();
        var hidden = first;
        list.Add(hidden);
        foreach (var enc in encoded) {
            hidden ^= enc;
            list.Add(hidden);
        }
        return list.ToArray();
    }
}
Source: https://leetcode.com/problems/decode-xored-array/

Leetcode Everyday: 1528. Shuffle String. Easy

public class Solution {
    public string RestoreString(string s, int[] indices) {
        var sb = new StringBuilder();
        for (var i = 0; i < indices.Length; ++i) {
            var charPosition = Array.IndexOf(indices, i);
            sb.Append(s[charPosition]);
        }
        return sb.ToString();
    }
}
Source: https://leetcode.com/problems/shuffle-string/

Sunday, February 14, 2021

Leetcode Everyday: 1342. Number of Steps to Reduce a Number to Zero. Easy

public class Solution {
    public int NumberOfSteps (int num) {
        int steps = 0;
        while (num > 0) {
            ++steps;
            num = num % 2 == 0 ? num / 2 : num - 1;
        }
        return steps;
    }
}
Source: https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/

Leetcode Everyday: 1365. How Many Numbers Are Smaller Than the Current Number. Easy

public class Solution {
    public int[] SmallerNumbersThanCurrent(int[] nums) {
        var sorted = nums.OrderBy(n => n).ToArray();
        var smallCounts = new List();
        for (var i = 0; i < nums.Length; ++i) {
            var count = Array.IndexOf(sorted, nums[i]);
            smallCounts.Add(count);
        }
        return smallCounts.ToArray();
    }
}
Source: https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/