Skip to content
38 changes: 38 additions & 0 deletions 0006-zigzag-conversion/0006-zigzag-conversion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class Solution {
public String convert(String s, int numRows) {
//Define StringBuilders
StringBuilder[] sbs = new StringBuilder[numRows];
for(int i = 0; i < numRows; i++)
{
sbs[i] = new StringBuilder();
}

//Define Variables
char[] arr = s.toCharArray();
int n = arr.length;
int index = 0;

//Tranverse zig-zag
while(index < n)
{
//Go down
for(int j = 0; j < numRows && index < n; j++)
{
sbs[j].append(arr[index++]);
}
//Go up Before Start
for(int j = numRows - 2; j > 0 && index < n; j--)
{
sbs[j].append(arr[index++]);
}
}

//Combine all stringbuilders into one
StringBuilder res = sbs[0];
for(int i = 1; i < numRows; i++)
{
res.append(sbs[i].toString());
}
return res.toString();
}
}
20 changes: 20 additions & 0 deletions 0006-zigzag-conversion/0006-zigzag-conversion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function convert(s, numRows) {
if (numRows === 1 || numRows >= s.length) {
return s;
}

let rows = Array.from({ length: numRows }, () => '');
let idx = 0, step = 1;

for (let c of s) {
rows[idx] += c;
if (idx === 0) {
step = 1;
} else if (idx === numRows - 1) {
step = -1;
}
idx += step;
}

return rows.join('');
}
32 changes: 32 additions & 0 deletions 0006-zigzag-conversion/0006-zigzag-conversion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

class Solution {

/**
* @param String $s
* @param Integer $numRows
* @return String
*/
function convert($s, $numRows) {
if ($numRows == 1 || $numRows >= strlen($s)) {
return $s;
}

$rows = array_fill(0, $numRows, '');
$idx = 0;
$step = 1;

for ($i = 0; $i < strlen($s); $i++) {
$rows[$idx] .= $s[$i];
if ($idx == 0) {
$step = 1;
} else if ($idx == $numRows - 1) {
$step = -1;
}
$idx += $step;
}

return implode('', $rows);
}

}
15 changes: 15 additions & 0 deletions 0006-zigzag-conversion/0006-zigzag-conversion.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def convert(s, numRows)
return s if numRows == 1 || numRows >= s.length
rows = Array.new(numRows) { "" }
idx, step = 0, 1
s.each_char do |c|
rows[idx] += c
if idx == 0
step = 1
elsif idx == numRows - 1
step = -1
end
idx += step
end
rows.join("")
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class Solution {
public int LengthOfLongestSubstring(string s) {
int n = s.Length;
HashSet<char> set = new HashSet<char>();
int maxLength = 0, i = 0, j = 0;
while (i < n && j < n) {
if (!set.Contains(s[j])) {
set.Add(s[j++]);
maxLength = Math.Max(maxLength, j - i);
} else {
set.Remove(s[i++]);
}
}
return maxLength;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public int lengthOfLongestSubstring(String s) {
int n = s.length();
Set<Character> set = new HashSet<>();
int maxLength = 0, i = 0, j = 0;
while (i < n && j < n) {
if (!set.contains(s.charAt(j))) {
set.add(s.charAt(j++));
maxLength = Math.max(maxLength, j - i);
} else {
set.remove(s.charAt(i++));
}
}
return maxLength;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var lengthOfLongestSubstring = function(s) {
let n = s.length;
let set = new Set();
let maxLength = 0, i = 0, j = 0;
while (i < n && j < n) {
if (!set.has(s.charAt(j))) {
set.add(s.charAt(j++));
maxLength = Math.max(maxLength, j - i);
} else {
set.delete(s.charAt(i++));
}
}
return maxLength;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

class Solution {

/**
* @param String $s
* @return Integer
*/
function lengthOfLongestSubstring($s) {
$n = strlen($s);
$set = array();
$maxLength = 0;
$i = 0;
$j = 0;
while ($i < $n && $j < $n) {
if (!in_array($s[$j], $set)) {
$set[] = $s[$j++];
$maxLength = max($maxLength, $j - $i);
} else {
unset($set[array_search($s[$i++], $set)]);
}
}
return $maxLength;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def length_of_longest_substring(s)
n = s.length
set = Set.new
max_length, i, j = 0, 0, 0
while i < n && j < n
if !set.include?(s[j])
set.add(s[j])
j += 1
max_length = [max_length, j - i].max
else
set.delete(s[i])
i += 1
end
end
return max_length
end
28 changes: 28 additions & 0 deletions 5-longest-palindromic-substring/5-longest-palindromic-substring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution:
def longestDupSubstring(self, S: str) -> str:
def rabin_karp(mid):
h = 0
for i in range(mid):
h = (h * 26 + nums[i]) % mod
s = {h}
p = pow(26, mid, mod)
for i in range(mid, len(S)):
h = (h * 26 + nums[i] - nums[i - mid] * p) % mod
if h in s:
return i - mid + 1
s.add(h)
return -1

mod = 2**63 - 1
nums = [ord(c) - ord('a') for c in S]
left, right = 0, len(S)
index = -1
while left <= right:
mid = (left + right) // 2
pos = rabin_karp(mid)
if pos != -1:
left = mid + 1
index = pos
else:
right = mid - 1
return S[index:index + left - 1] if index != -1 else ""