当前位置:网站首页>Leecode brushes questions and records interview questions 01.02 Determine whether it is character rearrangement for each other

Leecode brushes questions and records interview questions 01.02 Determine whether it is character rearrangement for each other

2022-07-07 00:12:00 Why is there a bug list

List of articles

topic

Given two strings s1 and s2, Please write a program , After determining the character rearrangement of one of the strings , Can it be another string .

Example 1:

Input : s1 = “abc”, s2 = “bca”
Output : true
Example 2:

Input : s1 = “abc”, s2 = “bad”
Output : false
explain :

0 <= len(s1) <= 100
0 <= len(s2) <= 100

answer

class Solution {
    
    public boolean CheckPermutation(String s1, String s2) {
    
         char[] c1=s1.toCharArray();
        char[] c2=s2.toCharArray();
        if(c1.length!=c2.length) return false;
        boolean a=false;
        boolean b=false;
        int j=0;
        for(int i=0;i<c1.length;i++)
        {
    
            for(j=0;j<c2.length;j++)
            {
    
                if(c1[i]==c2[j]) break;

            }
            if(j==c2.length) return false;
            j=0;
        }
        a=true;
        for(int i=0;i<c2.length;i++)
        {
    
            for(j=0;j<c1.length;j++)
            {
    
                if(c2[i]==c1[j]) break;

            }
            if(j==c2.length) return false;
            j=0;
        }
        b=true;
        return a&&b;
    }
}
原网站

版权声明
本文为[Why is there a bug list]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131017365043.html