# Programming Languages war Python, PHP, Javascript (Nodejs) Length of Longest Substring

## English

·

Hey my friends, a new post about this war. I don't know which is better, but I'll try to figure it out. I hope y'all enjoy this series. Here we go!

This is a challenge of leetcode.com I made this challenge with Python3, and next I made PHP and Javascript the same way.

Problem:

Given a string s, find the length of the longest substring without repeating characters.

Example 1:

Input: s = "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3.

### Python

I know that I can receive any string, so I need to manage that in Python, I know that I can read a string letter by letter with a For loop, I create two variables "longest" and "newstr" as list, I create a try-except method to validate and to manage the method "index" if exist in the list the same letter, I receive the position the letter if exist in the new list and remove each letter before that with the method "remove", next I add the new letter to the new list with the method "append", and last I validate if the length to the new list is grater than the "longest" variable, I save in this variable.

``````class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
longest = 0
newstr = []
for letter in s:
try:
a = newstr.index(letter, 0)
while a >= 0:
newstr.remove(newstr[0])
a -= 1
except:
pass
newstr.append(letter)
if len(newstr) > longest:
longest = len(newstr)

return longest

sol = Solution()
result = sol.lengthOfLongestSubstring("abcabcbb")
print(result)
``````

### PHP

I know that I can receive any string, so I need to manage that in PHP, first, I need to validate if the string is different to void, I know that I can't read a string letter by letter with a For loop so I need to turn to array whit the method "str_split", I create two variables "longest" and "newstr" as array, I use the method "array_keys" to validate if exist in the array the same letter, I receive the position the letter in an array if exist in the new array and remove each letter before that, from the first with the method "array_shift", next I add the new letter to the new array, and last I validate if the length to the new array is grater than the "longest" variable, I save in this variable.

``````<?php
class Solution
{
public function lengthOfLongestSubstring(\$s)
{
\$longest = 0;
\$newstr  = [];
if (\$s !== "") {
\$s = str_split(\$s);
foreach (\$s as \$letter) {
\$a = array_keys(\$newstr, \$letter);
if (count(\$a) >= 1) {
while (\$a[0] >= 0) {
array_shift(\$newstr);
\$a[0] -= 1;
}
}
\$newstr[] = \$letter;
if (count(\$newstr) > \$longest) {
\$longest = count(\$newstr);
}
}
}
return \$longest;
}
}

\$sol      = new Solution();
\$result = \$sol->lengthOfLongestSubstring("");

print_r(\$result);
?>
``````

### Javascript

I know that I can receive any string, so I need to manage that in Javascript,, I know that I can read a string letter by letter with a For loop, I create two variables "longest" and "newstr" as array, I use the method "indexOf" to validate if exist in the array the same letter, I receive the position the letter if exist in the new array and remove each letter before that, from the first with the method "shift", next I add the new letter to the new array with the method "push", and last I validate if the length to the new array is grater than the "longest" variable, I save in this variable.

``````class Solution {
lengthOfLongestSubstring(s) {
let longest = 0;
let newstr = [];
let a;
for (const letter of s) {
a = newstr.indexOf(letter, 0);
while (a >= 0) {
newstr.shift();
a -= 1;
}
newstr.push(letter);
if (newstr.length > longest) {
longest = newstr.length;
}
}
return longest;
}
}

const sol = new Solution();
const result = sol.lengthOfLongestSubstring("abcabcbb")

console.log(result);
``````

Conclusion

Structure: This time the three codes are very similar. Lines: Python: 21, PHP: 32, Javascript: 24 Time to execution: Python: 64 ms, PHP: 48 ms, Javascript: 92 ms Memory Usage: Python: 14.5 MB, PHP: 17.3 MB, Javascript: 40.9 MB

Would you like that I write about more challenges?

I hope you enjoy my post and remember that I am just a Dev like you!