华为机试-字符串匹配

题目描述

题目标题:

判断短字符串中的所有字符是否在长字符串中全部出现

详细描述:

接口说明

原型:

boolIsAllCharExist(char* pShortString,char* pLongString);

输入参数:

    char* pShortString:短字符串

    char* pLongString:长字符串

输入描述:

输入两个字符串。第一个为短字符,第二个为长字符。

输出描述:

返回值:

示例1

输入

bc
abc

输出

true

Java程序实现:

  1. import java.util.HashSet;  
  2. import java.util.Scanner;  
  3.   
  4. public class Main {  
  5.   
  6.     public static void main(String[] args) {  
  7.   
  8.         Scanner scanner = new Scanner(System.in);  
  9.         while (scanner.hasNext()) {  
  10.             String string1 = scanner.nextLine();  
  11.             String string2 = scanner.nextLine();  
  12.             boolean result = allIn(string1, string2);  
  13.             System.out.println(result);  
  14.         }  
  15.   
  16.     }  
  17.   
  18.     private static boolean allIn(String string1, String string2) {  
  19.         HashSet<Character> set = new HashSet<>();  
  20.         for (int i = 0; i < string2.length(); i++) {  
  21.             set.add(string2.charAt(i));  
  22.         }  
  23.         for (int i = 0; i < string1.length(); i++) {  
  24.             if (set.add(string1.charAt(i))) {  
  25.                 return false;  
  26.             }  
  27.         }  
  28.         return true;  
  29.     }  
  30. }  
原文地址:https://www.cnblogs.com/wwjldm/p/7102324.html