小程序三方平台开发: 解析小程序开发的未来趋势和机遇
602
2022-10-09
567. Permutation in String
Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. In other words, one of the first string’s permutations is the substring of the second string. Example 1:
Input:s1 = "ab" s2 = "eidbaooo"Output:TrueExplanation: s2 contains one permutation of s1 ("ba").
Example 2:
Input:s1= "ab" s2 = "eidboaoo"Output: False
Note: The input strings only contain lower case letters. The length of both given strings is in range [1, 10,000].
class Solution { public boolean checkInclusion(String s1, String s2) { int n1 = s1.length(), n2 = s2.length(); if (n1 > n2) return false; int[] map1 = new int[26]; int[] map2 = new int[26]; for (int i = 0; i < n1; i++) { map1[s1.charAt(i) - 'a']++; } for (int j = 0; j < n2; j++) { map2[s2.charAt(j) - 'a']++; if (j >= n1) { //定位到从窗口出去的那个数,减去一次occurence map2[s2.charAt(j - n1) - 'a']--; } if (Arrays.equals(map1, map2)) return true; } return false; }}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~