diff --git a/solution/0300-0399/0332.Reconstruct Itinerary/README.md b/solution/0300-0399/0332.Reconstruct Itinerary/README.md index 10906a75fd998..a84651a75af5e 100644 --- a/solution/0300-0399/0332.Reconstruct Itinerary/README.md +++ b/solution/0300-0399/0332.Reconstruct Itinerary/README.md @@ -57,6 +57,23 @@ ```python +class Solution: + def findItinerary(self, tickets: List[List[str]]) -> List[str]: + graph = defaultdict(list) + + for src, dst in sorted(tickets, reverse=True): + graph[src].append(dst) + + itinerary = [] + + def dfs(airport): + while graph[airport]: + dfs(graph[airport].pop()) + itinerary.append(airport) + + dfs("JFK") + + return itinerary[::-1] ``` diff --git a/solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md b/solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md index 99c61037d1859..2728db5195bf3 100644 --- a/solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md +++ b/solution/0300-0399/0332.Reconstruct Itinerary/README_EN.md @@ -49,6 +49,23 @@ ### **Python3** ```python +class Solution: + def findItinerary(self, tickets: List[List[str]]) -> List[str]: + graph = defaultdict(list) + + for src, dst in sorted(tickets, reverse=True): + graph[src].append(dst) + + itinerary = [] + + def dfs(airport): + while graph[airport]: + dfs(graph[airport].pop()) + itinerary.append(airport) + + dfs("JFK") + + return itinerary[::-1] ``` diff --git a/solution/0300-0399/0332.Reconstruct Itinerary/Solution.py b/solution/0300-0399/0332.Reconstruct Itinerary/Solution.py new file mode 100644 index 0000000000000..8b6c452a44ca2 --- /dev/null +++ b/solution/0300-0399/0332.Reconstruct Itinerary/Solution.py @@ -0,0 +1,17 @@ +class Solution: + def findItinerary(self, tickets: List[List[str]]) -> List[str]: + graph = defaultdict(list) + + for src, dst in sorted(tickets, reverse=True): + graph[src].append(dst) + + itinerary = [] + + def dfs(airport): + while graph[airport]: + dfs(graph[airport].pop()) + itinerary.append(airport) + + dfs("JFK") + + return itinerary[::-1] diff --git a/solution/0400-0499/0468.Validate IP Address/README.md b/solution/0400-0499/0468.Validate IP Address/README.md index bdbc0a18b6817..b7ac66790bc7d 100644 --- a/solution/0400-0499/0468.Validate IP Address/README.md +++ b/solution/0400-0499/0468.Validate IP Address/README.md @@ -65,6 +65,33 @@ ```python +class Solution: + def validIPAddress(self, IP: str) -> str: + if "." in IP: + segments = IP.split(".") + if len(segments) != 4: + return "Neither" + for segment in segments: + if ( + not segment.isdigit() + or not 0 <= int(segment) <= 255 + or (segment[0] == "0" and len(segment) > 1) + ): + return "Neither" + return "IPv4" + elif ":" in IP: + segments = IP.split(":") + if len(segments) != 8: + return "Neither" + for segment in segments: + if ( + not segment + or len(segment) > 4 + or not all(c in string.hexdigits for c in segment) + ): + return "Neither" + return "IPv6" + return "Neither" ``` diff --git a/solution/0400-0499/0468.Validate IP Address/README_EN.md b/solution/0400-0499/0468.Validate IP Address/README_EN.md index ff669c0532c13..d3e199ec15f95 100644 --- a/solution/0400-0499/0468.Validate IP Address/README_EN.md +++ b/solution/0400-0499/0468.Validate IP Address/README_EN.md @@ -57,6 +57,33 @@ ### **Python3** ```python +class Solution: + def validIPAddress(self, IP: str) -> str: + if "." in IP: + segments = IP.split(".") + if len(segments) != 4: + return "Neither" + for segment in segments: + if ( + not segment.isdigit() + or not 0 <= int(segment) <= 255 + or (segment[0] == "0" and len(segment) > 1) + ): + return "Neither" + return "IPv4" + elif ":" in IP: + segments = IP.split(":") + if len(segments) != 8: + return "Neither" + for segment in segments: + if ( + not segment + or len(segment) > 4 + or not all(c in string.hexdigits for c in segment) + ): + return "Neither" + return "IPv6" + return "Neither" ``` diff --git a/solution/0400-0499/0468.Validate IP Address/Solution.py b/solution/0400-0499/0468.Validate IP Address/Solution.py new file mode 100644 index 0000000000000..a23147565687f --- /dev/null +++ b/solution/0400-0499/0468.Validate IP Address/Solution.py @@ -0,0 +1,27 @@ +class Solution: + def validIPAddress(self, IP: str) -> str: + if "." in IP: + segments = IP.split(".") + if len(segments) != 4: + return "Neither" + for segment in segments: + if ( + not segment.isdigit() + or not 0 <= int(segment) <= 255 + or (segment[0] == "0" and len(segment) > 1) + ): + return "Neither" + return "IPv4" + elif ":" in IP: + segments = IP.split(":") + if len(segments) != 8: + return "Neither" + for segment in segments: + if ( + not segment + or len(segment) > 4 + or not all(c in string.hexdigits for c in segment) + ): + return "Neither" + return "IPv6" + return "Neither"