zoukankan      html  css  js  c++  java
  • Interleaving String

     1 public class Solution {
     2     public boolean isInterleave(String s1, String s2, String s3) {
     3         // IMPORTANT: Please reset any member data you declared, as
     4         // the same Solution instance will be reused for each test case.
     5        int len1 = s1.length(), len2 = s2.length(), len3 = s3.length();
     6         if(len3 != len1 + len2){
     7             return false;
     8         }
     9         
    10         boolean[][] match = new boolean[len1 + 1][len2 + 1];
    11         match[0][0] = true;
    12         
    13         // j == 0
    14         int i = 1;
    15         while(i <= len1 && s1.charAt(i-1) == s3.charAt(i-1)){
    16             match[i][0] = true;
    17             i++;
    18         }
    19         
    20         // i == 0
    21         int j = 1;
    22         while(j <= len2 && s2.charAt(j-1) == s3.charAt(j-1)){
    23             match[0][j] = true;
    24             j++;
    25         }
    26         
    27         for(int m = 1; m <= len1; m++){
    28             for(int n = 1; n <= len2; n++){
    29                 char c = s3.charAt(m + n - 1);
    30                 if(c == s1.charAt(m - 1)){
    31                     match[m][n] |= match[m-1][n];
    32                 }
    33                 if(c == s2.charAt(n - 1)){
    34                     match[m][n] |= match[m][n-1];
    35                 }
    36             }
    37         }
    38         return match[len1][len2];
    39     }
    40 }
     1 public class Solution {
     2     public boolean isInterleave(String s1, String s2, String s3) {
     3         // IMPORTANT: Please reset any member data you declared, as
     4         // the same Solution instance will be reused for each test case.
     5        int len1 = s1.length(), len2 = s2.length(), len3 = s3.length();
     6         if(len3 != len1 + len2){
     7             return false;
     8         }
     9         
    10         boolean[][] match = new boolean[len1 + 1][len2 + 1];
    11         match[0][0] = true;
    12         
    13         // j == 0
    14         int i = 1;
    15         while(i <= len1 && s1.charAt(i-1) == s3.charAt(i-1)){
    16             match[i][0] = true;
    17             i++;
    18         }
    19         
    20         // i == 0
    21         int j = 1;
    22         while(j <= len2 && s2.charAt(j-1) == s3.charAt(j-1)){
    23             match[0][j] = true;
    24             j++;
    25         }
    26         
    27         for(int m = 1; m <= len1; m++){
    28             for(int n = 1; n <= len2; n++){
    29                 char c = s3.charAt(m + n - 1);
    30                 if(c == s1.charAt(m - 1)){
    31                     match[m][n] |= match[m-1][n];
    32                 }
    33                 if(c == s2.charAt(n - 1)){
    34                     match[m][n] |= match[m][n-1];
    35                 }
    36             }
    37         }
    38         return match[len1][len2];
    39     }
    40 }
  • 相关阅读:
    Update SSM agent to each EC2 via Bat and bash script
    Shell脚本循环读取文件中的每一行
    通过psexec实现远程安装软件包
    Getting Private/Public IP address of EC2 instance using AWS-cli [closed]
    Python to list users in AWS
    BAT script to set local account password never expire
    Difference between Netbios and Host name
    PowerShell官方文档
    powershell for rename server name
    Create a conditional DNS forwarder on our domain.com to Amazon default DNS provider
  • 原文地址:https://www.cnblogs.com/jasonC/p/3433319.html
Copyright © 2011-2022 走看看