
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Is the second string a rotated version of the first string JavaScript
We are required to write a JavaScript function that takes in two strings, say str1 and str2. We are required to determine whether or not the second string is a rotated version of the first string.
For example− If the input strings are −
const str1 = 'abcde'; const str2 = 'cdeab';
Then the output should be true because str2 is indeed made by shifting 'ab' to the end of string in str1.
Example
const str1 = 'abcde'; const str2 = 'cdeab'; const isRotated = (str1, str2) => { if(str1.length !== str2.length){ return false }; if( (str1.length || str2.length) === 0){ return true }; for(let i = 0; i < str1.length; i++){ const reversed = str1.slice(i).concat(str1.slice(0, i)); if(reversed === str2){ return true }; } return false; }; console.log(isRotated(str1, str2));
Output
And the output in the console will be −
true
- Related Articles
- Dynamic Programming: Is second string subsequence of first JavaScript
- Return TRUE if the first string starts with a specific second string JavaScript
- Remove all characters of first string from second JavaScript
- Delete elements in first string which are not in second string in JavaScript
- How to concatenate two strings so that the second string must concatenate at the end of first string in JavaScript?
- How to concatenate two strings so that the second string must concatenate at the end of the first string in JavaScript?
- Find the character in first string that is present at minimum index in second string in Python
- Sorting one string by the order of second in JavaScript
- Check whether second string can be formed from characters of first string in Python
- Second most frequent character in a string - JavaScript
- Finding second smallest word in a string - JavaScript
- Finding the first non-repeating character of a string in JavaScript
- Function to find the length of the second smallest word in a string in JavaScript
- Returning the second most frequent character from a string (including spaces) - JavaScript
- How to return a string value version of the current number?

Advertisements