Anagrams问题

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <map>
 5 #include <string>
 6 using namespace std;
 7 map<char,int> s0,s1;
 8 char str0[90],str1[90];
 9
10 void zh(char str0[],int len0,char str1[],int len1)
11 {
12     for(int i=0;i<len0;i++)
13         if(str0[i]>=‘A‘&&str0[i]<=‘Z‘)
14             str0[i]+=32;
15     for(int i=0;i<len1;i++)
16         if(str1[i]>=‘A‘&&str1[i]<=‘Z‘)
17             str1[i]+=32;
18 }
19 int main()
20 {
21     int i,j,len0,len1;
22     scanf("%s%s",str0,str1);
23     len0=strlen(str0);    len1=strlen(str1);
24     zh(str0,len0,str1,len1);
25
26     for(i=0;i<len0;i++)
27         s0[str0[i]]++;
28     for(i=0;i<len1;i++)
29         s1[str1[i]]++;
30     char c=‘a‘;
31     for(c;c<=‘z‘;c++)
32     {
33         if(s0[c]!=s1[c])
34         {
35             printf("N\n");
36             return 0;
37
38         }
39     }
40     printf("Y\n");
41     return 0;
42 }

这题如果用map容器的话可能更好些吧,现在还没能清楚,有时间在系统做下吧

时间: 2025-01-11 15:40:10

Anagrams问题的相关文章

【LeetCode】49. Group Anagrams

Given an array of strings, group anagrams together. For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return: [ ["ate", "eat","tea"], ["nat",

Substring Anagrams

Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 40,000. The order of output does not matter.

LeetCode49 Group Anagrams

Given an array of strings, group anagrams together. For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return: [ ["ate", "eat","tea"], ["nat",

Group Anagrams

Given an array of strings, group anagrams together. For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return: [ ["ate", "eat","tea"], ["nat",

Two Strings Are Anagrams

Write a method anagram(s,t) to decide if two strings are anagrams or not. Example Given s="abcd", t="dcab", return true. 根据定义可知,两个字符串为anagrams,则每个字符出现的次数相同. 因此有两种解法: 1. 将两个字符串排序,如果排序后两字符串相等则返回true.  2. 统计量字符串的中字符出现的次数是否相等. 1 public cla

[LeetCode][Java] Anagrams

题目: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 题意: 给定一个字符串数组,返回所有的易位构词组合. 注意:所有的输入都是小写. 算法分析: 易位构词其实也很好理解,就是两个单词所包含的字符和数量都是一样的,只是顺序不同 对字符串中各字母进行排序,那么互为重排列的字符串就会相等. 按照上述思路,用一个map纪

leetcode_49题——Anagrams(string,hashtable,还用到了算法sort,迭代器)

Anagrams Total Accepted: 33531 Total Submissions: 137666My Submissions Question Solution Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. Hide Tags Hash Table String Have you met this

LeetCode[Hash Table]: Anagrams

Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 思路:对每一个单词的所有字母按照字典顺序排序,排序结果作为key,所有具有相同key的单词组合在一起成为一个Anagram group.最后返回所有的Anagram group. class Solution { public: vector<string> anag

[Leetcode] Anagrams 颠倒字母构成词

Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 题意:anagrams的意思是回文构词法.回文构词法有一个特点:单词里的字母的种类和数目没有改变,只是改变了字母的排列顺序.如: Input: ["tea","and","ate","eat",&qu

12、Anagrams by Stack

How can anagrams result from sequences of stack operations? There are two sequences of stack operators which can convert TROT to TORT: [ i i i i o o o o i o i i o o i o ] where i stands for Push and o stands for Pop. Your program should, given pairs