From a6c55dd4a0b01c70bc92ef547fe7c798b65e9a28 Mon Sep 17 00:00:00 2001 From: Cal Date: Thu, 4 Sep 2025 17:58:01 -0700 Subject: [PATCH 1/4] First pass at this. I don't like my solution, it's memory inefficient and doesn't pass all cases... but it does work for the first two cases. Keep debugging. --- problem_003/solution.py | 62 ++++++++++++++++++++++++++++++++++++ problem_003/test_inputs.json | 17 ++++++++++ 2 files changed, 79 insertions(+) create mode 100644 problem_003/solution.py create mode 100644 problem_003/test_inputs.json diff --git a/problem_003/solution.py b/problem_003/solution.py new file mode 100644 index 0000000..9c0b1ae --- /dev/null +++ b/problem_003/solution.py @@ -0,0 +1,62 @@ +# 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 = "" + last_string = "" + init = True + + # 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: + if current_string[0] == str: + # We are finding a new character string, so we should store the old character string length off somewhere. + current_string = current_string[1:] + 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 + + # 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..74f1f13 --- /dev/null +++ b/problem_003/test_inputs.json @@ -0,0 +1,17 @@ +{ + "method": "lengthOfLongestSubstring", + "tests": [ + { + "Input": "abcabcbb", + "Output": "3" + }, + { + "Input": "bbbbb", + "Output": "1" + }, + { + "Input": "pwwkew", + "Output": "3" + } + ] +} \ No newline at end of file From b893c8fdc3fbed72ee54c4f990a7a2b90601c199 Mon Sep 17 00:00:00 2001 From: Cal Date: Thu, 4 Sep 2025 18:52:46 -0700 Subject: [PATCH 2/4] Small Update, Needs Work This fixed the issue for the 3rd example string, but still needs a lot of work. It failed when using whitespace as part of the string which was a case I didn't consider. I added that as a test case, as well as a single character string, and will work on this again later when I have more time. --- problem_003/solution.py | 10 +++++----- problem_003/test_inputs.json | 8 ++++++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/problem_003/solution.py b/problem_003/solution.py index 9c0b1ae..2c8701b 100644 --- a/problem_003/solution.py +++ b/problem_003/solution.py @@ -26,9 +26,11 @@ def lengthOfLongestSubstring(self, s: str) -> int: if not current_string: current_string = str else: - if current_string[0] == str: + #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 # We are finding a new character string, so we should store the old character string length off somewhere. - current_string = current_string[1:] + str + current_string = current_string[repeat_index:] + str measuredLengths[str] = len(hashMap[str]) else: current_string = current_string + str @@ -42,14 +44,12 @@ def lengthOfLongestSubstring(self, s: str) -> int: # 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} ") + # 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 diff --git a/problem_003/test_inputs.json b/problem_003/test_inputs.json index 74f1f13..a1b3607 100644 --- a/problem_003/test_inputs.json +++ b/problem_003/test_inputs.json @@ -12,6 +12,14 @@ { "Input": "pwwkew", "Output": "3" + }, + { + "Input": "a", + "Output": "1" + }, + { + "Input": " ", + "Output": "1" } ] } \ No newline at end of file From f9bde77efd2ad284d20984da28940bd74face824 Mon Sep 17 00:00:00 2001 From: Cal Date: Thu, 4 Sep 2025 19:00:48 -0700 Subject: [PATCH 3/4] More Correct, Still Needs Work This passed a few more test cases, but still needs to work on more simple cases. Added another case that fails. The main key here was checking to see if the string has no repeats and then the longest length is just the full string length --- problem_003/solution.py | 60 +++++++++++++++++++----------------- problem_003/test_inputs.json | 8 +++++ 2 files changed, 40 insertions(+), 28 deletions(-) diff --git a/problem_003/solution.py b/problem_003/solution.py index 2c8701b..0ab84ee 100644 --- a/problem_003/solution.py +++ b/problem_003/solution.py @@ -20,35 +20,39 @@ def lengthOfLongestSubstring(self, s: str) -> int: last_string = "" init = True - # 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 - # 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]) + # 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: - 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 - - # 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 + #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 + # 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 + + # 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__": diff --git a/problem_003/test_inputs.json b/problem_003/test_inputs.json index a1b3607..55ad64d 100644 --- a/problem_003/test_inputs.json +++ b/problem_003/test_inputs.json @@ -20,6 +20,14 @@ { "Input": " ", "Output": "1" + }, + { + "Input": "au", + "Output": "2" + }, + { + "Input": "aab", + "Output": "2" } ] } \ No newline at end of file From b101a63143c5e0429009fe7683b4c88edaa86704 Mon Sep 17 00:00:00 2001 From: Cal Date: Thu, 4 Sep 2025 19:14:39 -0700 Subject: [PATCH 4/4] Closer Still, Still Needs Work I got many more cases handled, but this failed in a much later case where the string is much longer and complex. --- problem_003/solution.py | 13 ++++++++++--- problem_003/test_inputs.json | 6 +++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/problem_003/solution.py b/problem_003/solution.py index 0ab84ee..f9a4aa0 100644 --- a/problem_003/solution.py +++ b/problem_003/solution.py @@ -15,10 +15,7 @@ def lengthOfLongestSubstring(self, s: str) -> int: hashMap = {} measuredLengths = {} longest_string = 0 - current_string = "" - last_string = "" - init = True # Cover edge case where the string has no repeats if len(s) == len(set(s)): @@ -33,6 +30,7 @@ def lengthOfLongestSubstring(self, s: str) -> int: #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]) @@ -44,6 +42,15 @@ def lengthOfLongestSubstring(self, s: str) -> int: 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: diff --git a/problem_003/test_inputs.json b/problem_003/test_inputs.json index 55ad64d..0f05783 100644 --- a/problem_003/test_inputs.json +++ b/problem_003/test_inputs.json @@ -28,6 +28,10 @@ { "Input": "aab", "Output": "2" - } + }, + { + "Input": "odgqjxwoupzjiudpadcrozuujz", + "Output": "10" + } ] } \ No newline at end of file