567. Permutation in String

网友投稿 602 2022-10-09

567. Permutation in String

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小时内删除侵权内容。

上一篇:We重邮 - 微信小程序(we重邮下载)
下一篇:微信小程序,集成redux(微信小程序开发华网天下开发)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~