Skip to content

2160. Minimum Sum of Four Digit Number After Splitting Digits 👍

  • Time: $O(1)$
  • Space: $O(1)$
1
2
3
4
5
6
7
8
9
class Solution {
 public:
  int minimumSum(int num) {
    string s = to_string(num);
    ranges::sort(s);
    return stoi(s.substr(0, 1) + s.substr(2, 1)) +
           stoi(s.substr(1, 1) + s.substr(3, 1));
  }
};
1
2
3
4
5
6
7
class Solution {
  public int minimumSum(int num) {
    char[] chars = String.valueOf(num).toCharArray();
    Arrays.sort(chars);
    return (chars[0] - '0') * 10 + (chars[2] - '0') + (chars[1] - '0') * 10 + (chars[3] - '0');
  }
}
1
2
3
4
class Solution:
  def minimumSum(self, num: int) -> int:
    s = sorted(str(num))
    return int(s[0] + s[2]) + int(s[1] + s[3])