• 567. Permutation in String判断某字符串中是否存在另一个字符串的Permutation


    [抄题]:

    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:True
    Explanation: s2 contains one permutation of s1 ("ba").
    

     

    Example 2:

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

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩corner case]:

    [思维问题]:

    子串覆盖的题其实经常用sliding window,忘了

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    [一句话思路]:

    为了减掉s1的字符,别的串是先加后减,这里是先减后加

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    [一刷]:

    s1的字符串头必须处理完才返回true,稍微理解下

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    [总结]:

    为了减掉s1的字符,别的串是先加后减,这里是先减后加。

    [复杂度]:Time complexity: O(n) Space complexity: O(n)

    [算法思想:迭代/递归/分治/贪心]:

    [关键模板化代码]:

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

     [代码风格] :

     [是否头一次写此类driver funcion的代码] :

     [潜台词] :

    // package whatever; // don't place package name!
    
    import java.io.*;
    import java.util.*;
    import java.lang.*;
    
    /*class TreeNode 
    {
      int val;
      TreeNode left, right;
      
      //parameter is another item
      TreeNode(int item) {
        val = item;
        left = right = null;
      }
    }*/
    
    /*
    Input:s1 = "ab" s2 = "eidbaooo"
    Output:True
    Explanation: s2 contains one permutation of s1 ("ba").
    */
    
    
    class Solution {
        public boolean checkInclusion(String s1, String s2) {
          //initialization counts
          int len1 = s1.length(); int len2 = s2.length();
          int[] counts = new int[26];
          
            //corner case
          if (len1 > len2) return false;
          
          
          
          //go through s1
          for (int i = 0; i < len1; i++) {
            counts[s1.charAt(i) - 'a']++;
            counts[s2.charAt(i) - 'a']--;
            if(allZeros(counts)) return true;
          }
          
           /*
         a++ = 1,e-- = -1 b++ = 1 i-- = -1
      
          */
          
          //go through s1 - s2
          for (int i = len1; i < len2; i++) {
            counts[s2.charAt(i) - 'a']--;
            counts[s2.charAt(i - len1) - 'a']++;
            if(allZeros(counts)) return true;
          }
          /*
         d-- = -1 e++ = 0,
      
          */
          
          return false;
        }
      
     
      
      public boolean allZeros(int[] counts) {
        //false if
        for(int i = 0; i < 26; i++)
          if (counts[i] != 0) return false;
        return true;
      }
    }
        
    
    class MyCode {
      public static void main (String[] args) {
        Solution answer = new Solution();
        String s1 = "ab";
        String s2 = "eidbaooo";
      
        System.out.println("result = " + answer.checkInclusion(s1, s2));
      }
    }
    View Code
  • 相关阅读:
    专题一 Java基础语法
    IOC属于那种设计模式?
    java桌面应用开发
    Gitblit
    基于JSoup库的java爬虫开发学习——小步快跑
    tomcat和solr的整合——小步快跑
    SSM mapper文件SQL语句里面的 小于号 Tag name expected 无法识别解读
    Java从FTP服务器中获取PDF文件数据
    idea 控制台log日志中文乱码解决方案
    Java开发文档Swagger的使用详细教程
  • 原文地址:https://www.cnblogs.com/immiao0319/p/9458562.html
Copyright © 2020-2023  润新知