Created
July 9, 2024 17:04
-
-
Save subrotoice/120b798e15538785811dbfce3e30d602 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Task 1: | |
Write a function that does the following task. | |
You are given a string s and an integer array indices of the same length. The string s will be shuffled such that the character at the ith position moves to indices[i] in the shuffled string. Return the shuffled string. | |
Ans: | |
console.log(stringOrganize("mamacode", [4, 5, 6, 7, 0, 1, 2, 3])); | |
function stringOrganize(string, intArray) { | |
let newString = ""; | |
const length = intArray.length; | |
for (let index = 0; index < length; index++) { | |
newString += string[intArray[index]]; | |
} | |
return newString; | |
} | |
// Time Complixity: O(n) | |
// Space Complexity: O(n) | |
Task 2: | |
Write a function that does the following task. | |
Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. | |
Ans: | |
console.log(stringOrganize("sadbutsad", "sad")); | |
function stringOrganize(haystack, needle) { | |
let hLength = haystack.length; | |
let nLength = needle.length; | |
console.log(hLength, nLength); | |
for (var i = 0; i <= hLength - nLength; i++) { | |
for (var j = 0; j < nLength; j++) { | |
if (haystack[i + j] != needle[j]) break; | |
} | |
if (j == nLength) return i; | |
} | |
return -1; | |
} | |
// Time Complixity: O(n) | |
// Space Complexity: O(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment