Practice/Meta/Leetcode 1108. Defanging an IP Address
CodingOptional
You are building a security tool that needs to sanitize URLs before displaying them in logs or documentation. Your task is to create a function that takes a URL string and returns a "sanitized" version where the colon in the protocol (like http: or https:) is replaced with [:].
This sanitization helps prevent URLs from being automatically converted into clickable links in certain contexts, making them safer to display in plain text environments.
: to [:]http://, https://, ftp://)Example 1:
Input: "https://google.com" Output: "https[:]//google.com" Explanation: The colon after "https" is replaced with "[:]"
Example 2:
Input: "http://example.com:3000/api/users" Output: "http[:]//example.com:3000/api/users" Explanation: Only the protocol colon is replaced. The port number colon remains unchanged.
Example 3:
Input: "ftp://files.company.net/downloads" Output: "ftp[:]//files.company.net/downloads" Explanation: Works with any protocol, not just HTTP/HTTPS