Skip to content

Commit

Permalink
add string shift checker problem
Browse files Browse the repository at this point in the history
  • Loading branch information
gitgik committed Feb 4, 2022
1 parent 724692b commit bcd52d5
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions strings/string_is_shifted.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "9d04d27f",
"metadata": {},
"source": [
"## Problem\n",
"Given two strings A and B, return whether or not A can be shifted a number of times to get B.\n",
"\n",
"For example, if `A = cdeab` and `B = abcde` return true. If `A = xyz and B = xzy` return false. "
]
},
{
"cell_type": "markdown",
"id": "097684f2",
"metadata": {},
"source": [
"### Solution\n",
"First, we should return false if the first and second string differ in length.\n",
"\n",
"We can then concatenate one of the strings to itself, like (A + A) and check if B is in the concatenated string.\n",
"If the string is shifted, we will find it in the new string."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "3a619761",
"metadata": {},
"outputs": [],
"source": [
"def is_shifted(a, b):\n",
" if len(a) != len(b):\n",
" return False\n",
" return b in a + a"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "08ceb2ca",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_shifted(\"cdeab\", \"abcde\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1117e70c",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit bcd52d5

Please sign in to comment.