zoukankan      html  css  js  c++  java
  • two-strings-are-anagrams&&compare-strings&&longest-words

    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 };
  • 相关阅读:
    SpinLock 实现
    支持正则或通配符的hashmap
    HTTP报文
    Protostuff序列化
    【转】轻量级分布式 RPC 框架
    Servlet 3特性:异步Servlet
    Web服务器的工作原理
    《恰如其分的软件架构:风险驱动的设计方法》——读书笔记
    Java socket中关闭IO流后,发生什么事?(以关闭输出流为例)
    Java Socket常见异常处理 和 网络编程需要注意的问题
  • 原文地址:https://www.cnblogs.com/wangnanabuaa/p/4987167.html
Copyright © 2011-2022 走看看