-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotateString.php
More file actions
42 lines (40 loc) · 1019 Bytes
/
Copy pathRotateString.php
File metadata and controls
42 lines (40 loc) · 1019 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
namespace App;
/**
* Rotate String
*
* Given two strings s and goal, return true if and only if s can become goal after some number of shifts on s.
* A shift on s consists of moving the leftmost character of s to the rightmost position.
* For example, if s = "abcde", then it will be "bcdea" after one shift.
*
* Example 1:
* Input: s = "abcde", goal = "cdeab"
* Output: true
*
* Example 2:
* Input: s = "abcde", goal = "abced"
* Output: false
*
* https://leetcode.com/problems/rotate-string
*/
class RotateString
{
/**
* @param string $str
* @param string $goal
* @return bool
*/
public function rotateString(string $str, string $goal): bool
{
if ($str === $goal) {
return true;
}
for ($i = 0, $iMax = strlen($str); $i < $iMax; $i++) {
$newStr = substr($str, $i + 1) . substr($str, 0, $i + 1);
if ($newStr === $goal) {
return true;
}
}
return false;
}
}