On an infinite plane, a robot initially stands at (0, 0) and faces north. The robot can receive one of three instructions:

The robot performs the instructions given in order, and repeats them forever.

Return true if and only if there exists a circle in the plane such that the robot never leaves the circle.

Example 1:

Input: instructions = "GGLLGG"
Output: true
Explanation: The robot moves from (0,0) to (0,2), turns 180 degrees, and then returns to (0,0). …

var prystring = function(char, num){
let string = '\n'
for (let i = 1 ; i <= num; i++){
for(let j = num; j > i; j--){
string += " "
}
for(let k = 0; k < i * 2 -1; k++){
string += `${char}`
}
for(let j = num; j > i; j--){
string += " "
}
string += "\n"
}
console.log(string)
}

prystring('*',5)
'
*
***
*****
*******
*********
'
var strings = function(char, num){
let string = '\n'
for (let i = 0 ; i < num; i++){
for(let j = 0; j < i; j++){
string += " "
}
for(let k = 0; k < 2 * (num-i) -1; k++){
string += `${char}`
}
string += "\n"
}
console.log(string)
}

strings('*',5)
'
*********
*******
*****
***
*
'

Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.

Example 1:

Input: nums = [-4,-1,0,3,10]
Output: [0,1,9,16,100]
Explanation: After squaring, the array becomes [16,1,0,9,100].
After sorting, it becomes [0,1,9,16,100].

Example 2:

Input: nums = [-7,-3,2,3,11]
Output: [4,9,9,49,121]

Constraints:

Solution:

var sortedSquares = function(nums) {
let arr=[]
let left = 0
let right = nums.length -1

for(let i = nums.length -1 ; i >= 0 ;i--){
let square
if(Math.abs(nums[left]) < Math.abs(nums[right])){ …

Given an array of unsorted numbers, find all unique triplets in it that add up to zero.

Example 1:

Input: [-3, 0, 1, 2, -1, 1, -2]
Output: [-3, 1, 2], [-2, 0, 2], [-2, 1, 1], [-1, 0, 1]
Explanation: There are four unique triplets whose sum is equal to zero.

Example 2:

Input: [-5, 2, -1, -2, 3]
Output: [[-5, 2, 3], [-2, -1, 3]]
Explanation: There are two unique triplets whose sum is equal to zero.

Solution

We will sort the array and then iterate through it taking one number at a time. Let’s say during our iteration…


function solution(S, K) {

let split = S.split(“ “)

console.log(split)

let lengths = split.map(word => word.length)

console.log(lengths)

let arr = S.split(“ “).map(word => word.length)

console.log(“arr”, arr)

let map = new Map()

let smsCount = 1

if ( Math.max(…arr) > K ) {

return -1

//if biggest item in array > K , exit with -1

}

arr.forEach((smsLength, index) => {

if (index === 0) {

map[smsCount] = smsLength

//first sms item added to map

}else {

if (map[smsCount] + smsLength + 1 <= K) {

//if first/prev item + current length + space is less than or equal to K


/*
Simple String Sort Function
Given two arguments, a string of characters (input) and a sort string (sortStr),
implement a simple sort function that returns the characters to sort in the order
specified by the sort string. If any characters do not appear in the sort string,
they go at the end, in any order.
@Example
sort(‘banana’, ‘nab’) -> ‘nnaaab’
sort(‘house’, ‘soup’) -> ‘souhe’ or ‘soueh’
*/

function sort(input, sortStr) {
let sorted =[]
let remainder = input
for(let i =0; i<=sortStr.length -1; i++){
for(let j =0; j<=input.length; j++){
if (sortStr[i] == input[j]){
sorted.push(sortStr[i])
console.log(sorted)
}
}
}
remainder = remainder.split(“”)
let end =remainder.filter( x => !sorted.includes(x))
sorted.push(end.join(“”))
return sorted.join(“”)
}

sort(‘house’, ‘soup’)
sort(‘banana’, ‘nab’)


Problem Statement

Given a string with lowercase letters only, if you are allowed to replace no more than ‘k’ letters with any letter, find the length of the longest substring having the same letters after replacement.

Example 1:

Input: String="aabccbb", k=2
Output: 5
Explanation: Replace the two 'c' with 'b' to have a longest repeating substring "bbbbb".

Example 2:

Input: String="abbcb", k=1
Output: 4
Explanation: Replace the 'c' with 'b' to have a longest repeating substring "bbbb".

Example 3:

Input: String="abccde", k=1
Output: 3
Explanation: Replace the 'b' or 'd' with 'c' to have the longest repeating substring "ccc".
function length_of_longest_substring(str, k)…

Write a function, linkedListValues, that takes in the head of a linked list as an argument. The function should return an array containing all values of the nodes in the linked list.

test_00:

const a = new Node("a");
const b = new Node("b");
const c = new Node("c");
const d = new Node("d");
a.next = b;
b.next = c;
c.next = d;
// a -> b -> c -> dlinkedListValues(a); // -> [ 'a', 'b', 'c', 'd' ]

test_01:

const x = new Node("x");
const y = new Node("y");
x.next = y;// x -> ylinkedListValues(x); // -> [ 'x'…

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

Example 1:

Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.

Example 2:

Input: nums = [1]
Output: 1

Example 3:

Input: nums = [5,4,-1,7,8]
Output: 23

Solution:

var maxSubArray = function(nums) {
let sum = 0;
let maxSum = -Infinity;

if(nums.length === 0) return 0;
if(nums.length === 1) return nums[0]

for(let i = 0;i<nums.length;i++){
sum+=nums[i];
maxSum = Math.max(maxSum,sum);
if(sum < 0) sum = 0;
}
return maxSum;
};

Write a function, intersection, that takes in two arrays, a,b, as arguments. The function should return a new array containing elements that are in both of the two arrays.

You may assume that each input array does not contain duplicate elements.

const intersection = (a, b) => {
// todo
let set = new Set()
let arr = []
for( let i = 0; i < a.length; i++){
set.add(a[i])
}
for(let j = 0; j < b.length; j ++){
if (set.has(b[j])){
arr.push(b[j])
}
}
return arr
}

SunJet Liu

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store