{"id":3677,"date":"2024-04-15T05:26:28","date_gmt":"2024-04-15T05:26:28","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=3677"},"modified":"2025-04-03T12:55:31","modified_gmt":"2025-04-03T12:55:31","slug":"validate-ip-address-problem","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/validate-ip-address-problem\/","title":{"rendered":"Validate IP Address Problem ( C++, JAVA, Python)"},"content":{"rendered":"\n<p>An IP address is used to transfer the information to computers connected through the internet. Each computer connected via the internet has a unique IP address. Every computer connected via the same Internet has a unique IP address. In this article, we will discuss the problem \u201cValidate IP Address\u201d. It is based on the IP address. This question was recently asked in the \u201cDeutsche Bank\u201d coding round.\u00a0<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is the \u201cValidate IP Address\u201d Problem?<\/strong><\/h2>\n\n\n\n<p>IP addresses are of two types. IPV4 and IPV6. In this problem, you are given a string. You have to determine whether it is IPV4 or IPV6.\u00a0<\/p>\n\n\n\n<p><strong>An IPV4 address is in the form of a1.a2.a3.a4. Where (0&lt;=ai&lt;=255), here i goes from 1 to 4.<\/strong> This means each a1, a2, a3, and a4 lies between 0 to 255. <\/p>\n\n\n\n<p>Also, ai should not have leading zeroes. For example, <em>23.2.10.244<\/em> is a valid IPV4 address, while <em>23.2.10<\/em>, <em>23.2.10.<\/em>, <em>265.10.2.5<\/em>, and <em>23.02.10.244<\/em> are not valid IPV4 addresses.<\/p>\n\n\n\n<p><strong>An IPV6 address is in the form of a1:a2:a3:a4:a5:a6:a7:a8. Where size of 1&lt;=ai&lt;=4 here i goes from 1 to 8.<\/strong> Each character of ai can be \u20180\u2019 to \u20189\u2019, \u2018a\u2019 to \u2018f\u2019, or \u2018A\u2019 to \u2018F\u2019. <\/p>\n\n\n\n<p>In an IPV6 address, ai can have leading zeroes. For example, <em>a234:F:23:b2:08:00:aC:ee<\/em> is a valid IPV6 address, but <em>a2671:1F:23:b2:08:00:aC:ee<\/em>, <em>g2:1F:23:b2:0988:00:aC:ee<\/em>, <em>a2:1F:23:b2:08:00:aC<\/em>, and <em>a2::23:b2:08:00:aC:ee<\/em> is not a valid IPV6 address. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution to IP Address Problem<\/strong><\/h2>\n\n\n\n<p>This is a basic <a href=\"https:\/\/favtutor.com\/blogs\/char-to-string-cpp\" data-type=\"link\" data-id=\"https:\/\/favtutor.com\/blogs\/char-to-string-cpp\">string <\/a>question in which we just need to check all the conditions given in the question. Once we had checked all the conditions, we could easily determine if it was an IPV4, IPV6, or none of them.<\/p>\n\n\n\n<p>Let\u2019s examine this approach step by step.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>IPV4 check<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, create an empty string temp that stores ai, and an integer variable count that stores a count of ai.<\/li>\n\n\n\n<li>Now iterate over the string and check if s[i] is \u2018.\u2019 or not.&nbsp;<\/li>\n\n\n\n<li>If it is not \u2018.\u2019 then check if s[i] lies between \u20180\u2019 to \u20189\u2019 or not. If it does not lie between them, return false. Otherwise, check if the size of the temp exceeds 3 or not. If it exceeds 3, return false.<\/li>\n\n\n\n<li>If s[i] is equal to \u2018.\u2019 then increment the count by 1. Now check if the size of temp is zero and return false. Now convert this temp into an integer and check if the integer lies between 0 to 255 if not, return false. Also, check that if the size of temp is greater than 1 and temp[0] is 0, return false. At last, check if it is greater than or equal to 4 and return false. After checking all the conditions make temp = \u201c\u201d.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>IPV6 check<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, create an empty string temp that stores ai, and an integer variable count that stores a count of ai.<\/li>\n\n\n\n<li>Now iterate over the string and check if s[i] is \u2018:\u2019 or not.&nbsp;<\/li>\n\n\n\n<li>If it is not \u2018.\u2019 then check if s[i] lies between \u20180\u2019 to \u20189\u2019, \u2018a\u2019 to \u2018f\u2019, or \u2018A\u2019 to \u2018F\u2019 or not. If it does not lie between them, return false. Otherwise, check if the size of the temp exceeds 4 or not. If it exceeds 4, return false.<\/li>\n\n\n\n<li>If s[i] is equal to \u2018:\u2019 then increment the count by 1. Now check if the size of the temp is zero, or the temp size is greater than 4, or the count is greater than 8, and return false. Once you have checked all the conditions, assign temp = \u201c\u201d.<\/li>\n\n\n\n<li>After the loop is over, check for the temperature as we had checked in point 4.<\/li>\n\n\n\n<li>At last return true.<\/li>\n<\/ul>\n\n\n\n<p>In the main function, check if IPV4 returns true, then return \u201cIPV4\u201d. If not, check if IPV6 returns true, then return \u201cIPV6\u201d. If not, then return \u201cNeither\u201d.<\/p>\n\n\n\n<p>Let\u2019s examine the code for the above approach.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>C++ Code<\/strong><\/h3>\n\n\n\n<p>Here is the C++ solution:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;C++&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">#include&lt;bits\/stdc++.h&gt;\n\nusing namespace std;\n\n\u00a0\u00a0\u00a0\u00a0bool check_IPv4(string s){\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0vector&lt;int&gt;v;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0string temp = &quot;&quot;;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0int ct = 0;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for(int i=0;i&lt; s.size(); i++){\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if(s[i] == '.'){\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ct++;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if(temp.size() == 0) return false;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0int a = stoi(temp);\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if(a&gt;255 || ct&gt;=4) return false;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if(temp.size()&gt;1 &amp;&amp; temp[0]=='0')return false;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0temp = &quot;&quot;;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}else{\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0int ch = s[i] - '0';\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ cout&lt;&lt;temp.size();\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if(ch&gt;=0 &amp;&amp; ch&lt;= 9) temp += s[i];\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0else return false;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if(temp.size() &gt; 3) return false;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ct++;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if(temp.size() == 0) return false;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0int a = stoi(temp);\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if(a&gt;255) return false;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if((temp.size()&gt;1 &amp;&amp; temp[0]=='0') || (ct!=4))return false;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return true;\n\n\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0bool check_Ipv6(string&amp; s){\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0string t = &quot;&quot;;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0int ct = 0;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for(int i=0;i&lt;s.size();i++){\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if((s[i]&gt;='0' &amp;&amp; s[i]&lt;='9') || (s[i]&gt;='a' &amp;&amp; s[i]&lt;='f') || (s[i]&gt;='A' &amp;&amp; s[i]&lt;='F')){\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if(t.size()&gt;4) return false;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0t+=s[i];\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}else if(s[i]==':'){\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ct++;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if(t.size()==0 || t.size()&gt;4 || ct&gt;=8) return false;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0t=&quot;&quot;;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}else return false;\u00a0\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ct++;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if(t.size()==0 || t.size()&gt;4 || ct!=8) return false;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return true;\n\n\u00a0\u00a0\u00a0\u00a0}\n\nint main(){\n\n\u00a0\u00a0\u00a0\u00a0string s = &quot;Fa:ab:0132:21:a1:01:100C:1234&quot;;\n\n\u00a0\u00a0\u00a0\u00a0if(check_IPv4(s)) {\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0cout&lt;&lt;&quot;IPv4&quot;&lt;&lt;endl;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return 0;\n\n\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0if(check_Ipv6(s)) {\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0cout&lt;&lt;&quot;IPv6&quot;&lt;&lt;endl;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return 0;\n\n\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0cout&lt;&lt;&quot;Neither&quot;;\n\n\u00a0\u00a0\u00a0\u00a0return 0;\n\n}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Java Code<\/strong><\/h3>\n\n\n\n<p>Here is the same solution in Java:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Java&quot;,&quot;modeName&quot;:&quot;java&quot;}\">import java.util.*;\n\nclass IPAddressChecker {\n\n\u00a0\u00a0\u00a0\u00a0public static boolean checkIPv4(String s) {\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0List&lt;Integer&gt; v = new ArrayList&lt;&gt;();\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0StringBuilder temp = new StringBuilder();\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0int ct = 0;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for (int i = 0; i &lt; s.length(); i++) {\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (s.charAt(i) == '.') {\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ct++;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (temp.length() == 0) return false;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0int a = Integer.parseInt(temp.toString());\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (a &gt; 255 || ct &gt;= 4) return false;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (temp.length() &gt; 1 &amp;&amp; temp.charAt(0) == '0') return false;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0temp.setLength(0);\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0} else {\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0int ch = s.charAt(i) - '0';\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (ch &gt;= 0 &amp;&amp; ch &lt;= 9) temp.append(s.charAt(i));\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0else return false;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (temp.length() &gt; 3) return false;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ct++;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (temp.length() == 0) return false;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0int a = Integer.parseInt(temp.toString());\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (a &gt; 255) return false;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if ((temp.length() &gt; 1 &amp;&amp; temp.charAt(0) == '0') || (ct != 4)) return false;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return true;\n\n\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0public static boolean checkIPv6(String s) {\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0StringBuilder t = new StringBuilder();\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0int ct = 0;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for (int i = 0; i &lt; s.length(); i++) {\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if ((s.charAt(i) &gt;= '0' &amp;&amp; s.charAt(i) &lt;= '9') ||\u00a0\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0(s.charAt(i) &gt;= 'a' &amp;&amp; s.charAt(i) &lt;= 'f') ||\u00a0\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0(s.charAt(i) &gt;= 'A' &amp;&amp; s.charAt(i) &lt;= 'F')) {\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (t.length() &gt; 4) return false;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0t.append(s.charAt(i));\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0} else if (s.charAt(i) == ':') {\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ct++;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (t.length() == 0 || t.length() &gt; 4 || ct &gt;= 8) return false;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0t.setLength(0);\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0} else return false;\u00a0\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ct++;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (t.length() == 0 || t.length() &gt; 4 || ct != 8) return false;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return true;\n\n\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0public static void main(String[] args) {\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0String s = &quot;Fa:ab:0132:21:a1:01:100C:1234&quot;;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (checkIPv4(s)) {\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println(&quot;IPv4&quot;);\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (checkIPv6(s)) {\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println(&quot;IPv6&quot;);\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println(&quot;Neither&quot;);\n\n\u00a0\u00a0\u00a0\u00a0}\n\n}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Python Code<\/strong><\/h3>\n\n\n\n<p>You can implement it in Python too:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">def check_ipv4(s):\n\n\u00a0\u00a0\u00a0\u00a0v = []\n\n\u00a0\u00a0\u00a0\u00a0temp = &quot;&quot;\n\n\u00a0\u00a0\u00a0\u00a0ct = 0\n\n\u00a0\u00a0\u00a0\u00a0for i in range(len(s)):\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if s[i] == '.':\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ct += 1\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if len(temp) == 0:\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return False\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0a = int(temp)\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if a &gt; 255 or ct &gt;= 4:\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return False\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if len(temp) &gt; 1 and temp[0] == '0':\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return False\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0temp = &quot;&quot;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0else:\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ch = ord(s[i]) - ord('0')\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if 0 &lt;= ch &lt;= 9:\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0temp += s[i]\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0else:\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return False\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if len(temp) &gt; 3:\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return False\n\n\u00a0\u00a0\u00a0\u00a0ct += 1\n\n\u00a0\u00a0\u00a0\u00a0if len(temp) == 0:\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return False\n\n\u00a0\u00a0\u00a0\u00a0a = int(temp)\n\n\u00a0\u00a0\u00a0\u00a0if a &gt; 255:\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return False\n\n\u00a0\u00a0\u00a0\u00a0if (len(temp) &gt; 1 and temp[0] == '0') or (ct != 4):\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return False\n\n\u00a0\u00a0\u00a0\u00a0return True\n\ndef check_ipv6(s):\n\n\u00a0\u00a0\u00a0\u00a0t = &quot;&quot;\n\n\u00a0\u00a0\u00a0\u00a0ct = 0\n\n\u00a0\u00a0\u00a0\u00a0for i in range(len(s)):\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if '0' &lt;= s[i] &lt;= '9' or 'a' &lt;= s[i] &lt;= 'f' or 'A' &lt;= s[i] &lt;= 'F':\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if len(t) &gt; 4:\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return False\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0t += s[i]\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0elif s[i] == ':':\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ct += 1\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if len(t) == 0 or len(t) &gt; 4 or ct &gt;= 8:\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return False\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0t = &quot;&quot;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0else:\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return False\n\n\u00a0\u00a0\u00a0\u00a0ct += 1\n\n\u00a0\u00a0\u00a0\u00a0if len(t) == 0 or len(t) &gt; 4 or ct != 8:\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return False\n\n\u00a0\u00a0\u00a0\u00a0return True\n\ns = &quot;Fa:ab:0132:21:a1:01:100C:1234&quot;\n\nif check_ipv4(s):\n\n\u00a0\u00a0\u00a0\u00a0print(&quot;IPv4&quot;)\n\nelif check_ipv6(s):\n\n\u00a0\u00a0\u00a0\u00a0print(&quot;IPv6&quot;)\n\nelse:\n\n\u00a0\u00a0\u00a0\u00a0print(&quot;Neither&quot;)<\/pre><\/div>\n\n\n\n<p><strong>Output:&nbsp;<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background has-link-color wp-elements-e0282b7bf155587da9209b5a62af27c7\" style=\"background-color:#fedcba\"><code>IPv6<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Complexity Analysis<\/strong><\/h3>\n\n\n\n<p><strong>The time complexity for the above approach is O(n),<\/strong> where n is the size of the string. We are just iterating the string twice, whose time complexity is O(2*n) ~ O(n). The space complexity of the above code is O(1,) as we are not using any extra space.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>So, we have discussed how to solve the problem \u201cValidate IP Address\u201d. We have seen that it&#8217;s an easy string question; all we have to check are all the conditions that are given in the question. As it has many conditions to check one can easily miss out. Try to solve some more questions like \u201cIP to CIDR\u201d and \u201c<a href=\"https:\/\/leetcode.com\/problems\/strong-password-checker-ii\/\" target=\"_blank\" rel=\"noopener\">Strong Password Checker II<\/a>\u201d. Try to solve these questions on your own.\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>An IP address is used to transfer the information to computers connected through the internet. Each computer connected via the internet has a unique IP address. Every computer connected via the same Internet has a unique IP address. In this article, we will discuss the problem \u201cValidate IP Address\u201d. It is based on the IP [&hellip;]<\/p>\n","protected":false},"author":14,"featured_media":3681,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jnews-multi-image_gallery":[],"jnews_single_post":null,"jnews_primary_category":{"id":"","hide":""},"footnotes":""},"categories":[7],"tags":[14,15],"class_list":["post-3677","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-structures-algorithms","tag-dsa","tag-leetcode"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/3677","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/comments?post=3677"}],"version-history":[{"count":3,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/3677\/revisions"}],"predecessor-version":[{"id":7366,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/3677\/revisions\/7366"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/3681"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=3677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=3677"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=3677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}