diff --git a/problem_003/solution.py b/problem_003/solution.py new file mode 100644 index 0000000..f9a4aa0 --- /dev/null +++ b/problem_003/solution.py @@ -0,0 +1,73 @@ +# Extra header to ensure that tests can run individually and as a suite +# ------------------------------------------------------------------- +import sys +from pathlib import Path +# Add the project root folder to the Python path +sys.path.append(str(Path(__file__).resolve().parents[1])) +# ------------------------------------------------------------------- + +# Now you can import your wrapper +from test_runner.wrapper import run_tests + +class Solution: + def lengthOfLongestSubstring(self, s: str) -> int: + # str_start -> str + hashMap = {} + measuredLengths = {} + longest_string = 0 + current_string = "" + + # Cover edge case where the string has no repeats + if len(s) == len(set(s)): + return len(s) + else: + # Iterate over whole string, one character at a time + for str in s: + # Initial condition, current_string is empty so put the string into it. + if not current_string: + current_string = str + else: + #TODO: I feel like I can do a much better job than this one... it's messy and slow even if it works. + if str in current_string: + repeat_index = current_string.index(str) + 1 + # TODO: I am pretty sure this problem is -- RIGHT HERE -- so I should work on this part harder. If I remove it I miss cases in old tests, but I am assuming this part is the case where I am seeing failures. + # We are finding a new character string, so we should store the old character string length off somewhere. + current_string = current_string[repeat_index:] + str + measuredLengths[str] = len(hashMap[str]) + else: + current_string = current_string + str + + count = 0 + # This is really bad, I know. Fix it. + for str2 in current_string: + hashMap[str2] = current_string[count:] + count += 1 + + # See if the current value is greater than the last string value found with this length. If so, update the entry. + + # Attempts to look up the value, and if it isn't found just return 0 + lastMeasuredLength = measuredLengths.get(str2, 0) + + newMeasuredLength = len(hashMap[str2]) + if(newMeasuredLength > lastMeasuredLength): + measuredLengths[str2] = len(hashMap[str2]) + + # Soo.... now I should be able to iterate through meaturedLengths see the lengths of each string based on it's index. Then I just need to find the max? + for character in measuredLengths: + length = measuredLengths[character] + # print(f"character: {character}, length: {length} ") + if length > longest_string: + longest_string = length + + return longest_string + + +if __name__ == "__main__": + # Get the directory where this solution.py script lives + script_dir = Path(__file__).parent + + # Join the script's directory with the JSON filename to create a full path + # Make sure your file is actually named "test_inputs.json"! + test_file_path = script_dir / "test_inputs.json" + + run_tests(Solution, test_file_path) \ No newline at end of file diff --git a/problem_003/test_inputs.json b/problem_003/test_inputs.json new file mode 100644 index 0000000..0f05783 --- /dev/null +++ b/problem_003/test_inputs.json @@ -0,0 +1,37 @@ +{ + "method": "lengthOfLongestSubstring", + "tests": [ + { + "Input": "abcabcbb", + "Output": "3" + }, + { + "Input": "bbbbb", + "Output": "1" + }, + { + "Input": "pwwkew", + "Output": "3" + }, + { + "Input": "a", + "Output": "1" + }, + { + "Input": " ", + "Output": "1" + }, + { + "Input": "au", + "Output": "2" + }, + { + "Input": "aab", + "Output": "2" + }, + { + "Input": "odgqjxwoupzjiudpadcrozuujz", + "Output": "10" + } + ] +} \ No newline at end of file