Rotation Verification with String Concatenation
In software development interviews, candidates often face complex questions that test their problem-solving skills. One such challenge is determining if one string is a rotated version of another.
Consider the following interview question:
Question:
Given two strings, s1 and s2, how do you determine if s1 is a rotated version of s2?
Example:
Previously, an interviewee proposed a solution that involved finding the rotation point and concatenating split substrings. However, the interviewer requested a simpler approach.
Optimal Solution:
A more efficient solution utilizes string concatenation. It checks if s2 is a substring of s1 concatenated with itself. This ensures that all possible rotations are considered.
Pseudocode:
algorithm checkRotation(string s1, string s2) if( len(s1) != len(s2)) return false if( substring(s2,concat(s1,s1)) return true return false end
Java Implementation:
<code class="java">boolean isRotation(String s1,String s2) { return (s1.length() == s2.length()) && ((s1+s1).indexOf(s2) != -1); }</code>
This algorithm offers a straightforward and effective way to determine if a string is a rotation of another, meeting the interviewer's request for a simpler solution.
The above is the detailed content of Here are a few title options, incorporating the question-answer format and focusing on the key takeaways: **Option 1 (Direct & Concise):** * **Is String s1 a Rotation of String s2? A Simple Solu. For more information, please follow other related articles on the PHP Chinese website!