Leetcode brush questions [22-day summary] Interview questions 01.09. String rotation

CSDN Topic Challenge Phase 2
Participation Topic: Algorithm Solution


insert image description here

Topic link and description

https://leetcode.cn/problems/string-rotation-lcci/

String rotation. Given two strings s1 and s2, write code to check whether s2 is the rotation of s1 (for example, waterbottle is the rotation of erbottlewat).

Example 1:

Input: s1 = "waterbottle", s2 = "erbottlewat"
Output: True
Example 2:

Input: s1 = "aa", s2 = "aba"
Output: False
Prompt:

The string length is in the range [0, 100000].
illustrate:

Can you call the method that checks the substring only once?

Keywords: splicing and matching kmp algorithm

method one:

run screenshot

insert image description here

the code


    public boolean isFlipedString(String s1, String s2) {
    
    
        if(s1.length()!=s2.length()){
    
    
            return false;
        }
        s1 += s1;
        return s1.contains(s2);
    }

Conclusion

It takes 21 days to form a habit. It has been 23 days since the 7th.

insert image description here
Here is a brief review summary:

Welcome to communicate in the comment area, check in every day, and rush! ! !

Guess you like

Origin blog.csdn.net/qq_35530042/article/details/127113773