# What's the point of this question?

• Don't take me wrong, but I mean what's the point of this question? Are we suppose to use regular expression? If not, are we really supposed to write such a loooog code in an interview? If we are supposed to do basic string processing, I thought the interviewer want to see if the candidate knows how to use languages like Python or Perl etc.

``````class Solution {
public:
string validIPAddress(string IP) {
int i = 0, n = IP.size();
while(i < n && IP[i] != '.' && IP[i] != ':') i++;
if(i < n && IP[i] == '.' && handleIpv4(IP)) return "IPv4";
else if(i < n && IP[i] == ':' && handleIpv6(IP)) return "IPv6";
else return "Neither";
}

private:
bool handleIpv4(string& ip) {
istringstream is(ip);
string str;
int count = 0;
while(getline(is, str, '.')) {
count++;
if(str.empty() || str.size() > 3 || (str.size() > 1 && str[0] == '0')) return false;
int num = 0;
for(int i = 0; i < str.size(); i++) {
if(isdigit(str[i])) num = num*10 + (int)(str[i] - '0');
else return false;
}
if(num > 255) return false;
}
if(count != 4 || ip.back() == '.') return false;
else return true;
}

bool handleIpv6(string& ip) {
istringstream is(ip);
string str;
int count = 0;
while(getline(is, str, ':')) {
count++;
if(str.empty() || str.size() > 4) return false;
for(int i = 0; i < str.size(); i++) {
if(isdigit(str[i])) continue;
else if(isalpha(str[i]) && tolower(str[i]) <= 'f') continue;
else return false;
}
}
if(count != 8 || ip.back() == ':') return false;
else return true;
}
};
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.