mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
Compare commits
5 Commits
a4d986f169
...
35e5026163
Author | SHA1 | Date | |
---|---|---|---|
|
35e5026163 | ||
|
f3f32ae3ca | ||
|
e3bd7721c8 | ||
|
e3f3d668be | ||
|
db6a052a62 |
|
@ -16,7 +16,7 @@ repos:
|
||||||
- id: auto-walrus
|
- id: auto-walrus
|
||||||
|
|
||||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||||
rev: v0.7.2
|
rev: v0.7.4
|
||||||
hooks:
|
hooks:
|
||||||
- id: ruff
|
- id: ruff
|
||||||
- id: ruff-format
|
- id: ruff-format
|
||||||
|
@ -42,7 +42,7 @@ repos:
|
||||||
pass_filenames: false
|
pass_filenames: false
|
||||||
|
|
||||||
- repo: https://github.com/abravalheri/validate-pyproject
|
- repo: https://github.com/abravalheri/validate-pyproject
|
||||||
rev: v0.22
|
rev: v0.23
|
||||||
hooks:
|
hooks:
|
||||||
- id: validate-pyproject
|
- id: validate-pyproject
|
||||||
|
|
||||||
|
|
|
@ -172,7 +172,7 @@ def solved(values):
|
||||||
|
|
||||||
def from_file(filename, sep="\n"):
|
def from_file(filename, sep="\n"):
|
||||||
"Parse a file into a list of strings, separated by sep."
|
"Parse a file into a list of strings, separated by sep."
|
||||||
return open(filename).read().strip().split(sep) # noqa: SIM115
|
return open(filename).read().strip().split(sep)
|
||||||
|
|
||||||
|
|
||||||
def random_puzzle(assignments=17):
|
def random_puzzle(assignments=17):
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#!/usr/bin/env python3
|
#!python
|
||||||
import os
|
import os
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
66
strings/booths_algorithm.py
Normal file
66
strings/booths_algorithm.py
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
class BoothsAlgorithm:
|
||||||
|
"""
|
||||||
|
Booth's Algorithm finds the lexicographically minimal rotation of a string.
|
||||||
|
|
||||||
|
Time Complexity: O(n) - Linear time where n is the length of input string
|
||||||
|
Space Complexity: O(n) - Linear space for failure function array
|
||||||
|
|
||||||
|
For More Visit - https://en.wikipedia.org/wiki/Booth%27s_multiplication_algorithm
|
||||||
|
"""
|
||||||
|
|
||||||
|
def find_minimal_rotation(self, string: str) -> str:
|
||||||
|
"""
|
||||||
|
Find the lexicographically minimal rotation of the input string.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
string (str): Input string to find minimal rotation.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: Lexicographically minimal rotation of the input string.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ValueError: If the input is not a string or is empty.
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
>>> ba = BoothsAlgorithm()
|
||||||
|
>>> ba.find_minimal_rotation("baca")
|
||||||
|
'abac'
|
||||||
|
>>> ba.find_minimal_rotation("aaab")
|
||||||
|
'aaab'
|
||||||
|
>>> ba.find_minimal_rotation("abcd")
|
||||||
|
'abcd'
|
||||||
|
>>> ba.find_minimal_rotation("dcba")
|
||||||
|
'adcb'
|
||||||
|
>>> ba.find_minimal_rotation("aabaa")
|
||||||
|
'aaaab'
|
||||||
|
"""
|
||||||
|
if not isinstance(string, str) or not string:
|
||||||
|
raise ValueError("Input must be a non-empty string")
|
||||||
|
|
||||||
|
n = len(string)
|
||||||
|
s = string + string # Double the string to handle all rotations
|
||||||
|
f = [-1] * (2 * n) # Initialize failure function array with twice the length
|
||||||
|
k = 0 # Starting position of minimal rotation
|
||||||
|
|
||||||
|
for j in range(1, 2 * n):
|
||||||
|
sj = s[j]
|
||||||
|
i = f[j - k - 1]
|
||||||
|
|
||||||
|
while i != -1 and sj != s[k + i + 1]:
|
||||||
|
if sj < s[k + i + 1]:
|
||||||
|
k = j - i - 1
|
||||||
|
i = f[i]
|
||||||
|
|
||||||
|
if i == -1 and sj != s[k]:
|
||||||
|
if sj < s[k]:
|
||||||
|
k = j
|
||||||
|
f[j - k] = -1
|
||||||
|
else:
|
||||||
|
f[j - k] = i + 1
|
||||||
|
|
||||||
|
return s[k : k + n]
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
ba = BoothsAlgorithm()
|
||||||
|
print(ba.find_minimal_rotation("bca")) # output is 'abc'
|
Loading…
Reference in New Issue
Block a user