博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LintCode] Permutation in String
阅读量:6242 次
发布时间:2019-06-22

本文共 1328 字,大约阅读时间需要 4 分钟。

Problem

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

Example 1:

Input:s1 = "ab" s2 = "eidbaooo"

Output:True
Explanation: s2 contains one permutation of s1 ("ba").
Example 2:

Input:s1= "ab" s2 = "eidboaoo"

Output: False

Solution

public class Solution {    /**     * @param s1: a string     * @param s2: a string     * @return: if s2 contains the permutation of s1     */    public boolean checkInclusion(String s1, String s2) {        int len = s1.length();        for (int i = 0; i <= s2.length()-len; i++) {            if (isPermutation(s2.substring(i, i+len), s1)) return true;        }        return false;    }        //use the method of #String Permutation    public boolean isPermutation(String s1, String s2) {        if (s1 == null) return s2 == null;        if (s2 == null) return s1 == null;        if (s1.length() != s2.length()) return false;                int[] dict = new int[256];        char[] c1 = s1.toCharArray();        char[] c2 = s2.toCharArray();        for (char ch: c1) {            dict[(int)ch]++;        }        for (char ch: c2) {            dict[(int)ch]--;            if (dict[(int)ch] < 0) return false;        }        return true;    }}

转载地址:http://qmdia.baihongyu.com/

你可能感兴趣的文章
Cache Fusion
查看>>
bzoj2502
查看>>
Xcode 控制台打印Unicode字符串转换为中文
查看>>
Codeforces 831C--Jury Marks (思维)
查看>>
oracle内存结构+系统全局区+程序全局区(pga)+排序区+大型池+java池
查看>>
成长7 - lambda,filter,map的运用
查看>>
New Concept English Two 18 46
查看>>
Qt 删除目录
查看>>
Git 移除某些文件
查看>>
poj2940
查看>>
django做form表单的数据验证
查看>>
【OpenFOAM】——OpenFOAM入门算例学习
查看>>
STL UVA 11991 Easy Problem from Rujia Liu?
查看>>
模拟 URAL 1149 Sinus Dances
查看>>
Oracle 11G 数据库迁移【expdp/impdp】
查看>>
17.EXTJs 中icon 与iconCls的区别及用法!
查看>>
3.mybatis实战教程(mybatis in action)之三:实现数据的增删改查
查看>>
Caused by: Unable to load bean: type: class:com.opensymphony.xwork2.ObjectFactory - bean - jar
查看>>
让你拥有超能力:程序员应该掌握的统计学公式
查看>>
互联网组织的未来:剖析 GitHub 员工的任性之源
查看>>