三. Anagram detection problem for string(字符串中回文词汇检测问题)

anagram 相同字母异序词。heart vs earth

1.Our first solution to the anagram problem will check to see that each character in the first string actually occurs in the second. If it is possible to “checkoff” each character, then the two strings must be anagrams. Checking off a character(字母) will be accomplished by replacing it with the special Python value None. However, since strings in Python are immutable, the first step in the process will be to convert the second string to a list. Each character from the first string can be checked against the characters in the list and if found, checked off by replacement. ActiveCode 1 shows this function.

检测anagram的字符串:第一种方法:主要查看在第一个字符串中每一个单词是否出现在第二个字符串中,相同的字母用None代替,必须将string转变成list,因为,因为字符串不可以修改(immutable)。

 1 def  anagramSolution1(s1,s2):
 2     alist=list(s2)
 3     "依次对s1中字母与s2进行比较,只要s1中出现了s2中没有的字母,break"
 4     pos1=0;stillok=True
 5
 6     while pos1<len(s1) and stillok:
 7         pos2=0
 8         found=False
 9         while pos2<len(alist) and not found:
10             if s1[pos1]==alist[pos2]:
11                 found=True
12             else:
13                 pos2=pos2+1
14
15         if found:
16             alist[pos2]=None
17         else:
18              stillok=False
19         pos1=pos1+1
20
21     return stillok
22         
 1 def  anagramSolution1(s1,s2):
 2     alist=list(s2)
 3     "依次对s1中字母与s2进行比较,只要s1中出现了s2中没有的字母,break"
 4     pos1=0;stillok=True
 5
 6     while pos1<len(s1) and stillok:
 7
 8         if  s1[pos1] in alist:
 9             print s1[pos1],"in alist"
10             pos1=pos1+1
11             continue
12         else:
13             stillok=False
14
15     return stillok    

在接下来的计算当中,使用不同的算法,第一种算法的时间复杂度是o(n^2),第二种:先进行排序在进行对比o(nlogn),第三种:brute 暴力破解:o(n!)

对于最后一种算法能够精确到o(n):

we will first count the number of times each character occurs. Since there are 26 possible characters, we can use a list of 26 counters, one for each possible character. Each time we see a particular character, we will increment the counter at that position. In the end, if the two lists of counters are identical, the strings must be anagrams. ActiveCode 3 shows this solution.

这种算法根据单词26个字母特性,建立一个list,对26个字母遍历string S1 ,string S2,最后比较相同变换趋势。

 1 def  anagramSolution2(s1,s2):
 2     alist1=[0]*26
 3     alist2=[0]*26
 4
 5     for i in range(len(s1)):
 6         "ord C中 int,将ascall强制转换成integer"
 7         posi=ord(s1[i])-ord(‘a‘)
 8         alist1[posi]+=1
 9
10     for i in range(len(s2)):
11         "ord C中 int,将ascall强制转换成integer"
12         posi=ord(s2[i])-ord(‘a‘)
13         alist2[posi]+=1
14     matched=True
15     i=0
16     while i< 26 and matched:
17         if alist1[i]==alist2[i]:
18             i+=1
19         else:
20             matched=False
21
22     return matched

在所有的复杂度中最好能够缩减到o(n)和log(n)的时间复杂度,超过o(n^2)时间复杂度的算法必须要对算法进行改近

时间: 2024-08-27 17:49:03

三. Anagram detection problem for string(字符串中回文词汇检测问题)的相关文章

UVA - 11584 划分字符串的回文串子串; 简单dp

/** 链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34398 UVA - 11584 划分字符串的回文串子串: 简单dp 题目大意: 给一个字符串, 要求把它分割成若干个子串,使得每个子串都是回文串.问最少可以分割成多少个. 定义:dp[i]表示前0~i内的字符串划分成的最小回文串个数: dp[i] = min(dp[j]+1 | j+1~i是回文串); 先预处理flag[i][j]表示以i~j内的字符串为回文串

字符串的回文子序列个数

题目描述 求一个长度不超过15的字符串的回文子序列个数(子序列长度>=1). 输入描述 输入一个长度不超过15的字符串,字符串均由小写字母表示 输出描述 输出其回文子序列个数 样例输入 abaa 样例输出 10 注释 本例中其所有回文子序列为: a,b,a,a,aba,aba,aa,aa,aa,aaa 一个字符串的子序列是指在原字符串上去除某些字符但不破坏余下元素的相对位置(在前或在后)而形成的新字符串. #include<iostream> #include<string>

UVa 401 Palindromes(字符串,回文)

 Palindromes  A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDEDCBA" is a palindrome because it is the same when the string is read from left to right as when the string i

【c语言】判断一个字符串是不是回文字符串

//判断一个字符串是不是回文字符串 #include <stdio.h> #include <assert.h> int panduan( char *p ) { char *q ; assert( *p != NULL ); q = p; while( *p != '\0') { p++; } p--; while(*q != '\0') { if( *p == *q) { p--; q++; } else return -1; } return 1; } int main()

字符串-判断回文

编程判断一个字符串是否是回文,当字符串是回文时,输出字符串:yes!,否则输出字符串:no!.所谓回文即正向与反的拼向写都一样,如adgda. 长度在100以内,且全为小写字母样例输入:adgda样例输出:yes! 代码如下: 1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 5 int main() 6 { 7 char s[100]; 8 cin >> s; 9 int n = s

[CareerCup] 1.1 Unique Characters of a String 字符串中不同的字符

1.1 Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structure? 这道题让我们判断一个字符串中是否有重复的字符,要求不用特殊的数据结构,这里应该是指哈希表之类的不让用.像普通的整型数组应该还是能用的,这道题的小技巧就是用整型数组来代替哈希表,在之前Bitwise AND of Numbers Range 数

(数组)字符串的回文构词法( anagrams)

题目:https://www.nowcoder.com/practice/e84e273b31e74427b2a977cbfe60eaf4?tpId=46&tqId=29130&tPage=3&rp=3&ru=/ta/leetcode&qru=/ta/leetcode/question-ranking 思路: 首先简单介绍一下Anagram(回文构词法).Anagrams是指由颠倒字母顺序组成的单词,比如"dormitory"颠倒字母顺序会变成&

hdu 5340 Three Palindromes(字符串处理+ 回文)

hdu 5340 Three Palindromes 问题描述 判断是否能将字符串S分成三段非空回文串. 输入描述 第一行一个整数T,表示数据组数.T \leq 20T≤20 对于每一个组,仅包含一个由小写字母组成的串.1 \leq |S| \leq 200001≤∣S∣≤20000 输出描述 对于每一组,单行输出"Yes" 或 "No". 输入样例 2 abc abaadada 输出样例 Yes No 题目大意:给出一个字符串,判断,是否能将其分成三个不为空的回文

POJ 1159 Palindrome(字符串变回文:LCS)

http://poj.org/problem?id=1159 题意: 给你一个字符串, 问你做少需要在该字符串中插入几个字符能是的它变成一个回文串. 分析: 首先把原字符串和它的逆串进行匹配, 找出最长公共子序列. 那么最长公共子序列的字符串肯定是一个回文串. 所以原串剩下的部分是不构成回文的. 我们只需要添加剩下部分的字符到对应位置, 原串自然就变成了一个回文. 所以本题的解为: n 减去 (原串与逆串的LCS长度). 令dp[i][j]==x表示串A的前i个字符与串B的前j个字符的子串的最长