Determine String Rotation in Computer Science

AlgorithmAlgorithmBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In computer science, string rotation is a process of moving the characters in a string to the left or right. A string rotation of another string is a process of taking a string and moving its characters in a circular fashion. For example, the string "abcde" can be rotated to form the string "cdeab". In this challenge, we are going to determine if a string s1 is a rotation of another string s2.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL algorithm(("`Algorithm`")) -.-> algorithm/BasicAlgorithmsGroup(["`Basic Algorithms`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) algorithm/BasicAlgorithmsGroup -.-> algorithm/recursion_dynamic("`Recursion Dynamic`") algorithm/BasicAlgorithmsGroup -.-> algorithm/arrays_strings("`Arrays Strings`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills algorithm/recursion_dynamic -.-> lab-268807{{"`Determine String Rotation in Computer Science`"}} algorithm/arrays_strings -.-> lab-268807{{"`Determine String Rotation in Computer Science`"}} python/booleans -.-> lab-268807{{"`Determine String Rotation in Computer Science`"}} python/conditional_statements -.-> lab-268807{{"`Determine String Rotation in Computer Science`"}} python/tuples -.-> lab-268807{{"`Determine String Rotation in Computer Science`"}} python/function_definition -.-> lab-268807{{"`Determine String Rotation in Computer Science`"}} python/classes_objects -.-> lab-268807{{"`Determine String Rotation in Computer Science`"}} python/encapsulation -.-> lab-268807{{"`Determine String Rotation in Computer Science`"}} python/build_in_functions -.-> lab-268807{{"`Determine String Rotation in Computer Science`"}} end

Rotation

Problem

Given two strings s1 and s2, determine if s1 is a rotation of s2 by calling (only once) a function is_substring. The function is_substring takes two strings as input and returns True if the first string is a substring of the second string, and False otherwise.

Requirements

To solve this problem, we need to meet the following requirements:

  • The string is ASCII.
  • The comparison is case sensitive.
  • We can use additional data structures.
  • The strings can fit in memory.

Example Usage

Here are some examples of how the function should behave:

  • If the strings have different sizes, the function should return False.
  • If any of the strings is None, the function should return False.
  • If one of the strings is a space and the other is not, the function should return False.
  • If both strings are spaces, the function should return True.
  • If s1 is a rotation of s2, the function should return True.

Summary

In this challenge, we learned how to determine if a string is a rotation of another string by calling a function that checks if a string is a substring of another string. We also established the requirements for solving this problem and provided examples of how the function should behave.

Other Algorithm Tutorials you may like