6 kyu

Calculate String Rotation

355 of 7,499obnounce

Description:

Write a function that receives two strings and returns n, where n is equal to the number of characters we should shift the first string forward to match the second. The check should be case sensitive.

For instance, take the strings "fatigue" and "tiguefa". In this case, the first string has been rotated 5 characters forward to produce the second string, so 5 would be returned.

If the second string isn't a valid rotation of the first string, the method returns -1.

Examples:

"coffee", "eecoff" => 2
"eecoff", "coffee" => 4
"moose", "Moose" => -1
"isn't", "'tisn" => 2
"Esham", "Esham" => 0
"dog", "god" => -1
Strings
Arrays
Algorithms

Stats:

CreatedJul 3, 2015
PublishedJul 4, 2015
Warriors Trained14810
Total Skips2132
Total Code Submissions27996
Total Times Completed7499
Ruby Completions355
Java Completions1923
JavaScript Completions2128
Clojure Completions145
Python Completions1906
Haskell Completions149
C# Completions567
CoffeeScript Completions16
F# Completions79
Swift Completions283
Rust Completions118
COBOL Completions4
C Completions92
Total Stars228
% of votes with a positive feedback rating92% of 1019
Total "Very Satisfied" Votes877
Total "Somewhat Satisfied" Votes129
Total "Not Satisfied" Votes13
Ad
Contributors
  • obnounce Avatar
  • jhoffner Avatar
  • ZozoFouchtra Avatar
  • bkaes Avatar
  • Unnamed Avatar
  • fcbsd Avatar
  • datagram Avatar
  • imjasonmiller Avatar
  • user5036852 Avatar
  • drpicox Avatar
  • Mazur213 Avatar
  • Blind4Basics Avatar
  • wneumann Avatar
  • hobovsky Avatar
  • solitude Avatar
  • trashy_incel Avatar
  • WestwardLand968 Avatar
  • ejini战神 Avatar
  • akar-0 Avatar
  • saudiGuy Avatar
Ad