Last active
May 18, 2020 03:01
-
-
Save coreycoburn/4fda7037e76301f1671dfe359084a8b8 to your computer and use it in GitHub Desktop.
Coding Challenges Data Structures and Algorithms
This file contains 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
// Reverse a string | |
// O(n) linear time | |
const reversePhrase = phrase => { | |
phrase = phrase.split('') | |
const halfPhrase = Math.floor(phrase.length / 2) | |
const length = phrase.length - 1 | |
for (i=0; i < halfPhrase; i++) { | |
let front = phrase[i] | |
let back = phrase[length-i] | |
phrase[i] = back | |
phrase[length-i] = front | |
} | |
return phrase.join('') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment