码迷,mamicode.com
首页 > 其他好文 > 详细

two-strings-are-anagrams&&compare-strings&&longest-words

时间:2015-11-23 00:45:03      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

1、两个字符串是变位词

写出一个函数 anagram(s, t) 去判断两个字符串是否是颠倒字母顺序构成的

样例:
给出 s="abcd",t="dcab",返回 true;

解题思路:这道题首先要明白变位词并不是指的两个单词字母顺序颠倒,指的是构成他们的字母是一样的,但是字母的位置不一样;第一种方法很容易想到通过ASC码构建哈希表,这里需要注意的是如果以‘a‘来当做基点,程序会卡在68%的数据部分,因为我们需要考虑空格的存在,所以第一种方法的程序如下:

 1 public class Solution {
 2     /**
 3      * @param s: The first string
 4      * @param b: The second string
 5      * @return true or false
 6      */
 7     public boolean anagram(String s, String t) {
 8         // write your code here
 9         if( s==null||t==null )  return false;
10         char [] A=s.toCharArray();
11         char [] T=t.toCharArray();
12         int [] nums = new int[200];
13         if( A.length != T.length )  return false;
14         int l = A.length;
15         for(int i=0; i<l ;i++){
16            int index = A[i]-‘ ‘;
17            nums[index]++;
18         }
19         for(int i=0; i<l ;i++){
20            int index = T[i]-‘ ‘;
21            nums[index]--;
22         }
23         for(int i=0;i<200;i++){
24             if(nums[i]>0)  return false;
25         }
26         return true;
27     }
28 };

第二种方法就是C++函数中的sort函数自动对单词中的字母进行排序,排序后相等就是true;通过这两种方法,只想感慨C++在很多时候真的灰常666;

 1 class Solution {
 2 public:
 3     /**
 4      * @param s: The first string
 5      * @param b: The second string
 6      * @return true or false
 7      */
 8     bool anagram(string s, string t) {
 9         // write your code here
10         sort(s.begin(), s.end());
11         sort(t.begin(), t.end());
12 
13         return s == t;
14     }
15 };

2、比较字符串

比较两个字符串A和B,确定A中是否包含B中所有的字符。字符串A和B中的字符都是 大写字母

 

样例

给出 A = "ABCD" B = "ACD",返回 true

给出 A = "ABCD" B = "AABC", 返回 false

注意

在 A 中出现的 B 字符串里的字符不需要连续或者有序。

解题思路:思路如同上题,不过这次由于题目中限制了所有的字符均为大写字母,并且是包含关系,所以相对而言java的哈希表更加方便;

 1 public class Solution {
 2     /**
 3      * @param A : A string includes Upper Case letters
 4      * @param B : A string includes Upper Case letter
 5      * @return :  if string A contains all of the characters in B return true else return false
 6      */
 7     public boolean compareStrings(String A, String B) {
 8         // write your code here
 9         char [] a = A.toCharArray();
10         char [] b = B.toCharArray();
11         if(a.length<b.length) return false;
12         int[] nums= new int[26];
13         for(int i=0; i< a.length ; i++){
14             int index = a[i]-‘A‘;
15             nums[index]++;
16         }
17         for(int i=0; i< b.length ; i++){
18             int index = b[i]-‘A‘;
19             nums[index]--;
20         }
21         for(int i=0 ;i<26;i++){
22             if(nums[i]<0) return false;
23         }
24         return true;
25     }
26 }

3、最长单词

给一个词典,找出其中所有最长的单词;

 

在词典

{
  "dog",
  "google",
  "facebook",
  "internationalization",
  "blabla"
}

中, 最长的单词集合为 ["internationalization"]

在词典

{
  "like",
  "love",
  "hate",
  "yes"
}

中,最长的单词集合为 ["like", "love", "hate"]

挑战

遍历两次的办法很容易想到,如果只遍历一次你有没有什么好办法?

解题思路:这道题走了一些弯路,当然遍历两遍的算法不难,并且也比较好写。此处有一个我没有掌握透的知识点是动态数组ArrayList的用法,具体用法见链接http://www.cnblogs.com/rickie/articles/67978.html;

遍历一遍使用动态数组的思路就是假设第一个就是最长的,开始遍历,遇到长度相同的,将这个单词放入数组中,遇到长度更大的则清空当前数组,将新的单词放进去,继续,直到结束。

 1 class Solution {
 2     /**
 3      * @param dictionary: an array of strings
 4      * @return: an arraylist of strings
 5      */
 6     ArrayList<String> longestWords(String[] dictionary) {
 7         // write your code here
 8         int maxlen = dictionary[0].length();
 9         int nums = dictionary.length;
10         ArrayList<String> max = new ArrayList<String>();
11         for(int i=0;i<nums;i++){
12             int l = dictionary[i].length();
13             if(l>maxlen){
14                 max.clear();
15                 max.add(dictionary[i]);
16                 maxlen = l;
17             }else if(l==maxlen){
18                 max.add(dictionary[i]);
19             }else {
20                 
21             }
22         }
23         return max;
24     }
25 };

 

two-strings-are-anagrams&&compare-strings&&longest-words

标签:

原文地址:http://www.cnblogs.com/wangnanabuaa/p/4987167.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!