↓Skip to main content
  1. LeetCode/

LeetCode 1108: Defanging an IP Address

·1 min· ·
LeetCode Easy String
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-10-18

  • Submissions: Accepted (first try)

Approach
#

Replace every . with [.], either with one replace or by building the result character by character in a list (String Template 1).

My Solution
#

class Solution:
    def defangIPaddr(self, address: str) -> str:
        return address.replace(".", "[.]")

Complexity
#

Time O(n), space O(n).

Mistakes to Avoid
#

  • replace returns a new string; the original is unchanged.

Interview Explanation
#

I replace each dot with a bracketed dot in one replace call, in O(n).

Related

LeetCode 1160: Find Words That Can Be Formed by Characters
·1 min
LeetCode Easy Array Hash-Table String Counting
LeetCode 1160 note: a word is formable when none of its character counts exceeds chars’; sum the lengths of such words
LeetCode 942: DI String Match
·1 min
LeetCode Easy Array Two-Pointers String Greedy
LeetCode 942 note: two pointers hold the smallest and largest unused values; place the smallest on I and the largest on D
LeetCode 824: Goat Latin
·1 min
LeetCode Easy String
LeetCode 824 note: split into words and convert each: move a leading consonant to the end, then append ma and i + 1 copies of a
LeetCode 804: Unique Morse Code Words
·1 min
LeetCode Easy Array Hash-Table String
LeetCode 804 note: translate each word into a Morse string and count the distinct results with a set
LeetCode 657: Robot Return to Origin
·1 min
LeetCode Easy String Simulation
LeetCode 657 note: returning to the origin means as many U as D and as many L as R
LeetCode 557: Reverse Words in a String III
·1 min
LeetCode Easy Two-Pointers String
LeetCode 557 note: split on spaces, reverse each word, and join with spaces