Skip to content

Instantly share code, notes, and snippets.

@lienista
Last active December 12, 2018 05:50
Show Gist options
  • Select an option

  • Save lienista/5b0171018cb9107551662067742d8f9a to your computer and use it in GitHub Desktop.

Select an option

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)
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