0241-0250

241. Different Ways to Add Parentheses $\star\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution {
 public:
  vector<int> diffWaysToCompute(string input) {
    vector<int> ans;

    for (int i = 0; i < input.size(); ++i)
      if (ispunct(input[i])) {
        vector<int> left = diffWaysToCompute(input.substr(0, i));
        vector<int> right = diffWaysToCompute(input.substr(i + 1));
        for (int a : left)
          for (int b : right)
            ans.push_back(input[i] == '+' ? a + b
                                          : input[i] == '-' ? a - b : a * b);
      }

    return ans.empty() ? vector<int>{stoi(input)} : ans;
  }
};

242. Valid Anagram $\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
 public:
  bool isAnagram(string s, string t) {
    if (s.length() != t.length()) return false;

    unordered_map<char, int> map;
    for (char c : s) ++map[c];

    for (char c : t)
      if (--map[c] < 0) return false;

    return true;
  }
};

243. Shortest Word Distance $\star$

244. Shortest Word Distance II $\star\star$

245. Shortest Word Distance III $\star\star$

246. Strobogrammatic Number $\star$

247. Strobogrammatic Number II $\star\star$

248. Strobogrammatic Number III $\star\star\star$

249. Group Shifted Strings $\star\star$

250. Count Univalue Subtrees $\star\star$