Python刷题:哈希表(二)

6.Word Pattern

    Given a pattern and a string str, find if str follows the same pattern.

    Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.

    给定一个模式pattern和一个字符串str,如果该字符串str遵守这种模式,找出它。

    这里的遵守的含义是全匹配,就是模式pattern和字符串str中的非空字具有双映射。

示例:

1. pattern =’abba’, str = ‘dog cat cat dog’,返回True;

2. pattern =’abba’, str = ‘dog cat cat fish’,返回False;

3. pattern = ‘aaaa’, str = ‘dog cat cat dog’,返回False;

4. pattern = ‘abba’, str = ‘dog dog dog dog’,返回False。

程序:

def wordPattern(pattern, string):
	if len(pattern)==len(string.split(' ')) and \
	len(set(zip(pattern, string.split(' '))))\
	== len(set(pattern)) == len(set(string.split(' '))):
		return True
	else:
		return False

    7.Intersection of Two Arrays

    Given two arrays, write a function to compute their intersection. Note: Each element in the result must be unique. The result can be in any order.

    给定两个数组,构造一个函数统计它们的交集。注意:结果中的所有元素都是独一无二的,并且结果的顺序不做要求。

示例:

给定nums1= [3,2,2,1],nums2 = [2,2],返回[2]。

程序:

def interesction(nums1,nums2):
	return[x for x in set(nums2) if x in set(nums1)]

8.Intersection of Two Arrays Ⅱ

    Given two arrays, write a function to compute their intersection. Note: Each element in the result should appear as many times as it shows in both arrays. The result can be in any order.

    给定两个数组,构造一个函数统计它们的交集。注意:结果中的所有元素出现的次数应当和它在两个数组中共同出现的次数一致,结果的顺序不做要求。

示例:

给定nums = [1,2,2,1], nums2 = [2,2],返回[2,2]。

程序:

import collections
def interesction(nums1, nums2):
	a = collections.Counter(nums1)
	b = collections.Counter(nums2)
	return [i for i in a.keys() for j in range(min(a[i], b[i]))]

9.Find the Difference

    Given two strings s and t which consist of only lowercase letters. String t is generated by random shuffling string s and then add one more letter at a random position. Find the letter that was added in f.

    给定两个字符串s和t,他们两个中包含了一个唯一的字符。字符串t是由随机混洗的字符串s在任意位置添加一个字符而生成的。找出f中被添加的字符。

示例:

输入s = “abcd”, t = “abcde”;输出e。

程序:

def differ(s,t):
	a = list(s)
	b = list(t)
	for i in range(min(len(a),len(t))):
		if a[i]!=b[i]:
			if len(a)>len(b):
				return a[i]
			else:
				return b[i]
	else:
		if len(a)>len(b):
			return a[-1]
		else:
			return b[-1]

10. Longest Palindrome

    Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. This is case sensitive, for example “Aa” is not considered a palindrome here. Note: Assume the length of given string will not exceed 1,010.

    给定一个字符串,其中包含大写和小写字母,找出这些字符可以构造的最长的回文。这是区分大小写的,例如“Aa”在这里就不认为是回文。注意:假定给出的字符串长度不超过1010。

示例:

输入“abccccdd”,输出7。这是因为可构造的最长回文为“dccacca。

程序:

import collections
def pailndrome(s):
	a = collections.Counter(s)
	length = 0
	res = 0
	for i in a.values():
		if i%2 == 0:
			length += i
		else:
			res = max(res,i)
	length += res
	return length

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值