Last active
December 12, 2018 05:50
-
-
Save lienista/5b0171018cb9107551662067742d8f9a to your computer and use it in GitHub Desktop.
Algorithms in Javascript: CTCI 1.9 - String rotation: Assume you have a method isSubstring which checks if one word is a substring of another. Given two strings, write the code to check if one string is a rotation of the other using only one call to isSubstring (e.g., waterbottle is a rotation of erbottlewat)
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
| const isSubstring = (s1, s2) => { | |
| if (!s1 || !s2) { | |
| return false; | |
| } | |
| if (s1.length !== s2.length) { | |
| return false; | |
| } | |
| return (s1 + s1).includes(s2); | |
| } | |
| var a = 'waterbottle'; | |
| var b = 'bottlewater'; | |
| var c = 'buttlewater'; | |
| console.log(isSubstring(a,b)); | |
| console.log(isSubstring(a,c)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment