Bracket the Dots in an IPv4 Address

Quick Overview

Bracket the Dots in an IPv4 Address. Given a valid IPv4 address string, return the same address with each dot replaced by [.].

Bracket the Dots in an IPv4 Address

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Given a valid IPv4 address string, return the same address with each dot replaced by `[.]`. Preserve all other characters and their order. ```cpp class Solution { public: string defangIPaddr(string address); }; ``` ### Examples **Example 1:** ```text Input: address = "10.20.30.40" Output: "10[.]20[.]30[.]40" Explanation: Each of the three dots is bracketed. ``` **Example 2:** ```text Input: address = "0.0.0.0" Output: "0[.]0[.]0[.]0" Explanation: Digits remain unchanged. ``` ### Constraints - The given address is a valid IPv4 address. ### Contract considerations The address is already valid. Only dots change; every digit and its order must be preserved. ```hint Read the boundary condition Which characters are required to change, and which must remain exactly as they appeared? ``` ### Boundary reminders The address is already valid. Only dots change; every digit and its order must be preserved.

Overview: Bracket the Dots in an IPv4 Address. Given a valid IPv4 address string, return the same address with each dot replaced by [.].

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Given a valid IPv4 address string, return the same address with each dot replaced by [.]. Preserve all other characters and their order.

class Solution {
public:
    string defangIPaddr(string address);
};

Examples

Example 1:

Input: address = "10.20.30.40"
Output: "10[.]20[.]30[.]40"
Explanation: Each of the three dots is bracketed.

Example 2:

Input: address = "0.0.0.0"
Output: "0[.]0[.]0[.]0"
Explanation: Digits remain unchanged.

Constraints

  • The given address is a valid IPv4 address.

Contract considerations

The address is already valid. Only dots change; every digit and its order must be preserved.

Boundary reminders

The address is already valid. Only dots change; every digit and its order must be preserved.