Skip to content

Commit

Permalink
solved
Browse files Browse the repository at this point in the history
  • Loading branch information
a-abdellatif98 committed Jan 31, 2022
1 parent 7bdcf9a commit 2c979f0
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Longest_Substring_Without_Repeating_Characters.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# @param {String} s
# @return {Integer}
def length_of_longest_substring(s)
stack = []
highest_count = 0
s.each_char do |c|
unless index = stack.index(c)
stack.push(c)
else
stack.push(c)
(index + 1).times {stack.shift}
end
highest_count = stack.length if stack.length > highest_count
end
highest_count
end
23 changes: 23 additions & 0 deletions Max_Area_of_Island.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
def DFS(grid, i, j)
if i < 0 || j < 0 || i > grid.length - 1 || j > grid[0].length - 1 || grid[i][j] == 0
return 0
end

grid[i][j] = 0

return 1 + DFS(grid, i - 1, j) + DFS(grid, i + 1, j) + DFS(grid, i, j - 1) + DFS(grid, i, j + 1)
end

# @param {Integer[][]} grid
# @return {Integer}
def max_area_of_island(grid)
maxArea = 0

for i in 0..grid.length - 1
for j in 0..grid[0].length - 1
maxArea = [maxArea, DFS(grid, i, j)].max
end
end

return maxArea
end
17 changes: 17 additions & 0 deletions MiddleoftheLinkedList.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def middle_node(head)
org_head = head
count = 0
while !head.nil?
count+=1
head = head.next
end

i = 0
head = org_head
while i < count/2
i+=1
head = head.next
end

return head
end
23 changes: 23 additions & 0 deletions Permutation_in_String.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# @param {String} s1
# @param {String} s2
# @return {Boolean}
def check_inclusion(s1, s2)
return false if s2.size < s1.size
window = Array.new(26, 0)

s1.each_char do |ch|
window[ch.ord - 'a'.ord] += 1
end

s2.each_char.with_index do |ch, i|
window[ch.ord - 'a'.ord] -= 1
if i >= s1.size
window[s2[i - s1.size].ord - 'a'.ord] += 1
end
if window.all?(&:zero?)
return true
end
end

false
end
17 changes: 17 additions & 0 deletions Remove_Nth_Node_From_End_of_List.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def remove_nth_from_end(head, n)
node = head
prev = nil
arr_node = []
while !node.nil? do
arr_node << node
node = node.next
end
if arr_node.length - n == 0
head = head.next
else
node = arr_node[-n-1]
node.next = node.next.next
end

return head
end
3 changes: 3 additions & 0 deletions ReverseWordsinaStringIII.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def reverse_words(s)
s.split.map(&:reverse).join(" ")
end
47 changes: 47 additions & 0 deletions merge_two_lists.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Definition for singly-linked list.
# class ListNode
# attr_accessor :val, :next
# def initialize(val)
# @val = val
# @next = nil
# end
# end

# @param {ListNode} l1
# @param {ListNode} l2
# @return {ListNode}
def merge_two_lists(l1, l2)
solution = [] # head = nil
ho = l1
ht = l2

while !ho.nil? && !ht.nil? do
if ho.val < ht.val
solution << ho.val
ho = ho.next
else
solution << ht.val
ht = ht.next
end
end

if ho.nil?
while !ht.nil? do
solution << ht.val
ht = ht.next
end
elsif ht.nil?
while !ho.nil? do
solution << ho.val
ho = ho.next
end
end

solution
end

list1 = [1,2,4], list2 = [1,3,4]

re = merge_two_lists(list1, list2)

puts re
19 changes: 19 additions & 0 deletions remove_element.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# @param {Integer[]} nums
# @param {Integer} val
# @return {Integer}
def remove_element(nums, val)
return 0 if nums.length == 0
nums.each_with_index do |num, index|
if num == val
nums[index] = nil
end
end
nums.compact!
nums.length

end


nums = [3,2,2,3], val = 3

puts remove_element(nums,val)

0 comments on commit 2c979f0

Please sign in to comment.