首页 > 试题广场 >

子串判断

[编程题]子串判断
  • 热度指数:6733 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

给定一个string数组p及其大小n,同时给定长字符串string s,请返回一个bool数组,元素为true或false对应p中的对应字符串是否为s的子串。要求p中的串长度小于等于8,且p中的串的个数小于等于500,同时要求s的长度小于等于1000。

测试样例:
["a","b","c","d"],4,"abc"
返回:[true,true,true,false]
public class Substr {
    public boolean[] chkSubStr(String[] p, int n, String s) {
        boolean[] f=new boolean[n];
        for(int i=0;i<n;i++){
            f[i]=s.indexOf(p[i])>=0?true:false;
        }
        return f;
    }
}


发表于 2021-11-27 13:04:54 回复(0)
import java.util.*;

public class Substr {
    public boolean[] chkSubStr(String[] p, int n, String s) {
        // write code here
        boolean[] result = new boolean[n];
        for (int i = 0; i < n; i++) {
            result[i] = s.contains(p[i]);
        }
        return result;
    }
}

发表于 2020-06-08 10:14:24 回复(0)
import java.util.Scanner;

public class Contains {
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        String []str=new String[n];
        for(int i=0;i<n;i++)
            str[i]=sc.next();
        String str1=sc.next();
        Contains c=new Contains();
        boolean []result=c.chkSubStr(str,n,str1);
        for(int i=0;i<n;i++)
            System.out.print(result[i]+" ");
    }

    public boolean[] chkSubStr(String[] p, int n, String s) {
        boolean []b=new boolean[n];
        for(int i=0;i<n;i++){
            if(s.contains(p[i]))
                b[i]=true;
            else
                b[i]=false;
                
        }

        return b;
    }
}

发表于 2018-03-21 19:04:55 回复(0)
import java.util.*;

public class Substr {
    public boolean[] chkSubStr(String[] p, int n, String s) {
        // write code here
        boolean [] result = new boolean[p.length];
        for(int i=0;i<p.length;i++){
            if(s.indexOf(p[i])>-1){
                result[i]=true;
            }
        }
        return result;
    }
}

发表于 2017-06-10 21:01:24 回复(0)
import java.util.*;

public class Substr {
    public boolean[] chkSubStr(String[] p, int n, String s) {
        // write code here
         boolean[]hasSub=new boolean[p.length];
		 for (int i = 0; i < p.length; i++) {
			hasSub[i]=s.contains(p[i]);
		}
		 return hasSub;
    }
}


编辑于 2016-08-13 15:02:21 回复(0)

问题信息

难度:
5条回答 21197浏览

热门推荐

通过挑战的用户

查看代码