Thursday, February 18, 2021

Leetcode Everyday: 1688. Count of Matches in Tournament. Easy

public class Solution {
    public int NumberOfMatches(int teams) {          
        int matchesSum = 0;
        while (teams > 1) {
            int matches = teams / 2;
            matchesSum += matches;                        
            teams -= matches;   
        }
        
        return matchesSum;
    }
}
Source: https://leetcode.com/problems/count-of-matches-in-tournament/

Leetcode Everyday: 1614. Maximum Nesting Depth of the Parentheses. Easy

public class Solution {
    public int MaxDepth(string s) {
        int depth = 0;
        int maxDepth = 0;
        foreach (var c in s) {
            if (c == '(') {
                ++depth;
            } else if (c == ')') {
                --depth;
            }
            if (depth > maxDepth) {
                maxDepth = depth;
            }
        }
        return maxDepth;
    }
}
Source: https://leetcode.com/problems/maximum-nesting-depth-of-the-parentheses/

Leetcode Everyday: 938. Range Sum of BST. Easy

public class Solution {
    public int RangeSumBST(TreeNode root, int low, int high) {
        int sum = low <= root.val && root.val <= high ? root.val : 0;
        
        if (root.left != null) {
            sum += RangeSumBST(root.left, low, high);
        }
        if (root.right != null) {
            sum += RangeSumBST(root.right, low, high);
        }
        
        return sum;
    }
}
Source: https://leetcode.com/problems/range-sum-of-bst/

Wednesday, February 17, 2021

Leetcode Everyday: 1684. Count the Number of Consistent Strings. Easy

public class Solution {
     public int CountConsistentStrings(string allowed, string[] words) =>
         words.Count(word => !word.Any(wc => allowed.IndexOf(wc) == -1));
}
Solution 2, faster and still readable:
public class Solution {
    public int CountConsistentStrings(string allowed, string[] words) {
        int count = 0;
        foreach (string word in  words) {     
            foreach (char c in word) {          
              if (allowed.IndexOf(c) == -1) {
                 goto nextWord;
              }
            }
            ++count;
        nextWord:;
        }
        return count;
    }
}
Solution 2b, goto-less:
public class Solution {
    public int CountConsistentStrings(string allowed, string[] words) {
        int count = words.Length;
        foreach (string word in  words) {     
            foreach (char c in word) {          
              if (allowed.IndexOf(c) == -1) {
                 --count;
                  break;
              }
            }
        }
        return count;
    }
}
Solution 3, goto when used judiciously makes for a faster code. This is the fastest C# code on leetcode. Optimized other's code by converting boolean+break combo to a goto:
public class Solution {
    public int CountConsistentStrings(string allowed, string[] words) {
        bool[] valid = new bool[26];

        foreach (char c in allowed) valid[c - 'a'] = true;

        int count = 0;
        foreach (var word in words) {
            foreach (char c in word) if (!valid[c - 'a']) goto nextWord;
            
            ++count;            
        nextWord:;
        }
        
        return count;
    }
}
Solution 4, almost same as above, fastet code too. Just reversed the logic, something is valid until it is not. So the count starts with words.Length. made it goto-less
public class Solution {
    public int CountConsistentStrings(string allowed, string[] words) {
        bool[] valid = new bool[26];

        foreach (char c in allowed) valid[c - 'a'] = true;

        int count = words.Length;
        foreach (var word in words) {
            foreach (char c in word) {
                if (!valid[c - 'a']) {
                    --count;
                    break;
                }
            }
        }
        
        return count;
    }
}
Source: https://leetcode.com/problems/count-the-number-of-consistent-strings/

Leetcode Everyday: 1662. Check If Two String Arrays are Equivalent. Easy

public class Solution {
    public bool ArrayStringsAreEqual(string[] word1, string[] word2) {
        var isEqual = word1.Aggregate((left, right) => left + right) == word2.Aggregate((left, right) => left + right);
        
        return isEqual;
    }
}
Source: https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/