Thousand Separator

easy string

Problem

Given an integer n, add a dot '.' as the thousands separator and return it as a string. The dot goes between every group of three digits counted from the right.

Inputn = 1234567
Output"1.234.567"
Groups from the right: 567, 234, 1.

def thousand_separator(n):
    s = str(n)
    out = []
    count = 0
    for ch in reversed(s):
        if count and count % 3 == 0:
            out.append(".")
        out.append(ch)
        count += 1
    return "".join(reversed(out))
function thousandSeparator(n) {
  const s = String(n);
  let out = "";
  let count = 0;
  for (let i = s.length - 1; i >= 0; i--) {
    if (count && count % 3 === 0) out = "." + out;
    out = s[i] + out;
    count++;
  }
  return out;
}
class Solution {
    public String thousandSeparator(int n) {
        String s = Integer.toString(n);
        StringBuilder out = new StringBuilder();
        int count = 0;
        for (int i = s.length() - 1; i >= 0; i--) {
            if (count > 0 && count % 3 == 0) out.append('.');
            out.append(s.charAt(i));
            count++;
        }
        return out.reverse().toString();
    }
}
string thousandSeparator(int n) {
    string s = to_string(n);
    string out = "";
    int count = 0;
    for (int i = s.size() - 1; i >= 0; i--) {
        if (count && count % 3 == 0) out = '.' + out;
        out = s[i] + out;
        count++;
    }
    return out;
}
Time: O(d) Space: O(d)