Skip to content

Commit 604c880

Browse files
committed
Adding solution for Problem - 1342 - Efficient Solution
1 parent be440d8 commit 604c880

File tree

2 files changed

+61
-29
lines changed

2 files changed

+61
-29
lines changed

Defanging IP Address/EfficientSolution.py

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,68 @@
22
## Leetcode
33
## Student: Vandit Jyotindra Gajjar
44
## Year: 2020
5-
## Problem: 1108
6-
## Problem Name: Defanging an IP Address
5+
## Problem: 1342
6+
## Problem Name: Number of Steps to Reduce a Number to Zero
77
##===================================
8-
#Given a valid (IPv4) IP address, return a defanged version of that IP address.
9-
#
10-
#A defanged IP address replaces every period "." with "[.]".
8+
#Given a non-negative integer num, return the number of steps to reduce it to zero.
119
#
10+
#If the current number is even, you have to divide it by 2, otherwise, you have to subtract 1 from it.
1211
#Example 1:
1312
#
14-
#Input: address = "1.1.1.1"
15-
#Output: "1[.]1[.]1[.]1"
13+
#Input: num = 14
14+
#Output: 6
15+
#Explanation:
16+
#Step 1) 14 is even; divide by 2 and obtain 7.
17+
#Step 2) 7 is odd; subtract 1 and obtain 6.
18+
#Step 3) 6 is even; divide by 2 and obtain 3.
19+
#Step 4) 3 is odd; subtract 1 and obtain 2.
20+
#Step 5) 2 is even; divide by 2 and obtain 1.
21+
#Step 6) 1 is odd; subtract 1 and obtain 0.
22+
#
1623
#Example 2:
1724
#
18-
#Input: address = "255.100.50.0"
19-
#Output: "255[.]100[.]50[.]0"
25+
#Input: num = 8
26+
#Output: 4
27+
#Explanation:
28+
#Step 1) 8 is even; divide by 2 and obtain 4.
29+
#Step 2) 4 is even; divide by 2 and obtain 2.
30+
#Step 3) 2 is even; divide by 2 and obtain 1.
31+
#Step 4) 1 is odd; subtract 1 and obtain 0.
2032
#
21-
#Constraints:
33+
#Example 3:
2234
#
23-
#The given address is a valid IPv4 address.
35+
#Input: num = 123
36+
#Output: 12
37+
#
38+
#The solution for this problem is fairly starightforward by following the problem statement.
39+
2440
class Solution:
2541

26-
def defangIPaddr(self, address):
27-
28-
string = [] #[]
29-
30-
for character in address:
31-
32-
if character == '.': #1.1.1.1 if . is in address
33-
34-
string.append("[.]") #we'll append it by ["[.]"]
35-
36-
else:
37-
38-
string.append(character) #Or we'll append it with characters
42+
def numberOfSteps (self, number):
43+
44+
step = 0 #Initialize our step counter.
45+
46+
while number != 0: #While the number is not zero (Following the condition of non-negative integer)
47+
48+
if number % 2 == 0: #Condition Check - If number is divided by zero and gives no remainder
49+
50+
number = number // 2 #We'll divide the number by 2 again. Here (//) will give you integer as in 2.0, 3.0 etc. In python (/) will give you floating numbers.
51+
52+
else: #If condition fails then
53+
54+
number = number - 1 #We'll subtract the number by 1.
55+
56+
step = step + 1 #Corresponding, we'll update our step counter.
57+
58+
return step #We'll return the step number at the end.
3959

40-
return "".join(string) #Finally we'll join the string which will give "1[.]1[.]1[.]1"
41-
.
42-
#Initialize empty string
43-
#For every character in our IP Address, we will check with the condition that if "." is in the address, we'll simply replace with our given value which is "[.]",
44-
#otherwise, we'll append remaining address. Lastly, we'll simply join all the character in string.
60+
#For example: number = 3
61+
# 3 % 2 = 1 (If condition failed.)
62+
# 3 - 1 = 2 (Enters the else condition.) [step = 1]
63+
# Now number = 2.
64+
# 2 % 2 = 0 (If condition pass.)
65+
# 2 // 2 = 1.0 (Enters the if condition.) [step = 2]
66+
# number = 1.0
67+
# 1 % 2 = -1 (if condition failed.)
68+
# 1 - 1 = 0 (Enters the else condition.) (Goal state reached.) [step = 3]
69+
# Total number of step = 3.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
##==================================
2+
## Leetcode
3+
## Student: Vandit Jyotindra Gajjar
4+
## Year: 2020
5+
## Problem: 1108
6+
## Problem Name: Defanging an IP Address
7+
##===================================

0 commit comments

Comments
 (0)