LeetCode Entry
3754. Concatenate Non-Zero Digits and Multiply by Sum I
Sum digits concat digits
3754. Concatenate Non-Zero Digits and Multiply by Sum I easy substack youtube
https://dmitrysamoylenko.com/leetcode/

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1413
Problem TLDR
Sum digits * concat digits
Intuition
Sum them and concat them with strings. Or go from tail with match.
Approach
- can be done in two phases for shorter code
Complexity
-
Time complexity: \(O(logn)\)
-
Space complexity: \(O(logn)\)
Code
fun sumAndMultiply(n: Int) =
("0"+"$n".filter{it>'0'}).toLong()*"$n".sumOf{it-'0'}
pub fn sum_and_multiply(mut n: i32) -> i64 {
let(mut v,mut s,mut m)=(0,0,1);
while n>0{let d=(n%10)as i64;s+=d;if d>0{v+=d*m;m*=10}n/=10}v*s
}
Comments