判断短字符串S中的所有字符是否在长字符串T中全部出现。
请注意本题有多组样例输入。
数据范围:
进阶:时间复杂度:,空间复杂度:
输入两个字符串。第一个为短字符串,第二个为长字符串。两个字符串均由小写字母组成。
如果短字符串的所有字符均在长字符串中出现过,则输出字符串"true"。否则输出字符串"false"。
bc abc
true
其中abc含有bc,输出"true"
from collections import Counter short_s = raw_input() l_s = raw_input() c1 = Counter(short_s) c2 = Counter(l_s) k1 = c1.keys() k2 = c2.keys() flag = set(c1).issubset(set(c2)) print('true' if flag else 'false')
# coding: utf-8 def func(s1,s2): lens = len(s1) count = 0 for i in s1: if i in s2: count += 1 if count == lens: print 'true' else: print 'false' if __name__ == "__main__": import sys line1 = sys.stdin.readline().strip() line2 = sys.stdin.readline().strip() func(line1,line2)
while True: try: str1 = input() str2 = input() str1 = list(str1) str1 = set(str1)#去除短项重复项 str1 = list(str1) value = 'true'#预设为true for i in range (len(str1)):#逐个查找 if str1[i] not in str2:#一旦有一个不存在则返回false value = 'false' break print(value) except: break
# 方法一: # 思路:笨方法,拿短字符串中的每个字符去长字符串中看是否含有 while True: try: str1 = input().strip() str2 = input().strip() all_in = True for ch in str1: if ch not in str2: all_in = False break if all_in: print("true") else: print("false") except: break # 方法二: # 思路:巧方法,利用集合去重,集合的包含关系可以用>,<来表达 while True: try: set1, set2 = set(input().strip()), set(input().strip()) print('true' if set1 <= set2 else 'false') except: break
while True: try: strShort = input() strLong = input() AllIn = 1; for s in strShort: if s not in strLong: AllIn = 0 if AllIn: print('true') else: print('false') except: break
用set就可以。。。
while True:
try:
s1 = set(input())
s2 = set(input())
x = 'true' if s1 & s2 == s1 else 'false'
print(x)
except:
break
为什么换成string就不行? case通过率只有20%
while True:
try:
s1 = str(input())
s2 = str(input())
x = 'true' if s1 in s2 else 'false'
print(x)
except:
break
# -*- coding: utf-8 -*- # !/usr/bin/python3 def is_all_char_exist(lst1, lst2): m = len(st1) for i in range(m): if lst1[i] not in st2: return False return True while True: try: st1 = input() st2 = input() if is_all_char_exist(st1, st2): print('true') else: print('false') except: break