Showing posts with label LeetCode. Show all posts
Showing posts with label LeetCode. Show all posts

Wednesday, 11 December 2019

1108. Defanging an IP Address


/*
Tanzila Islam
Website: https://sites.google.com/site/tanzilamohita
Email: tanzilamohita@gmail.com
*/

#include <bits/stdc++.h>
using namespace std;
 string defangIPaddr(string address) {
    string res;
    for (int i=0; i<address.size(); i++)
        {
            if (address[i] == '.')
                res += "[.]";
            else
                res.push_back(address[i]);
        }
        return res;
}
int main(){
string address;
cout << "Input: ";
cin >> address;

cout << "Output: " << defangIPaddr(address) <<endl;

}

1281. Subtract the Product and Sum of Digits of an Integer


/*
Tanzila Islam
Website: https://sites.google.com/site/tanzilamohita
Email: tanzilamohita@gmail.com
*/

#include <bits/stdc++.h>
using namespace std;

int subtractProductAndSum(int n) {
    int sum = 0;
    int product = 1;

	while(n>0){
    	sum += n % 10;
    	product *= n % 10;
    	n /= 10;
	}
	return product - sum;
}

int main() {
	int n;
	cout << "Input: ";
	cin >> n;
	cout << "Output " << subtractProductAndSum(n) << endl;
	return 0;
}