Answer: VAHAN Hiring | Defanging an IP Address | On-Campus OA (2025)
Answer · Posted Jun 2026
Approach Traverse the IP address. Replace every "." with "[.]". Return the modified string. Java provides the replace() method, making the solution straightforward. Strategy Scan each character of the string. Whenever a dot is encountered, replace it with "[.]". Otherwise, keep the character unchanged. Java Code class Solution { public String defangIPaddr(String address) { return address.replace(".", "[.]"); } } Time Complexity O(n) Space Complexity O(n)
The full answer & interview discussion are available to premium members.
Log in Create a free account