IT企业面试题(java描述)-字符串包含(2)-查找字典里面的兄弟字符串

转载自:http://blog.csdn.net/raylee2007/article/details/50679195

题目:

查找字典里面的兄弟字符串

兄弟字符串概念:长度相同、字符完全相同、顺序不一定一样的字符串,例如“abc”和“cba”就是兄弟字符串,“aab”和“aba”也是

在这里的字典我们为了方便,选用简单的字符串数组来替代,一般大的字典都是大数据的字符串文件


1.思路

(1)暴力轮询

通过双重for循环来轮询两个字符串的每一个字符,相同的变成某个数字,然后再轮询一遍其中一个,如果有不同的字符,则不是兄弟字符串

此方法的时间复杂度是O(m*n+n)

(2)排序法

由于两字符串长度相同、字符完全相同,只是顺序不一样,那么我们先排序,我使用的是快速排序法,然后同时对比两个字符串在同一个位置上取出来的字符,如果相同,则true,不同,则false

此方法的时间复杂度平均是O(mlog(m)+nlog(n)+n)

(3)HashMap方法

在此利用HashMap的key的唯一性

我们把字符当成key放到HashMap去,然后再记录每一个字符的出现次数,最后通过对比map的长度、字符出现的次数来判定是否兄弟字符串

此方法的时间复杂度平均是O(m+n+n)


从时间复杂度来说,无疑是选择后面两种方法,但是后面两种方法直接也存在差异,主要表现在查询字符长度的大小,当字符的长度大于三的时候,第三种方法最快。


2.代码

(1)暴力轮询

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.ray.interview.ch01.topic_1_2;  
  2.   
  3. /** 
  4.  * 搜查字典里面的兄弟字符串 
  5.  *  
  6.  * 兄弟字符串概念:长度相同、字符完全相同、顺序不一定一样的字符串,例如“abc”和“cba”就是兄弟字符串,“aab”和“aba”也是 
  7.  *  
  8.  * 在这里的字典我们为了方便,选用简单的字符串数组来替代,一般大的字典都是大数据的字符串文件 
  9.  *  
  10.  *  
  11.  * @author raylee 
  12.  * @date 2016-02-17 
  13.  * @version 1.0 
  14.  */  
  15. public class ContainString_3 {  
  16.   
  17.     public static boolean isBrother(String strSourceA, String strSourceB) {  
  18.         boolean isBrother = true;  
  19.         if (strSourceA.length() != strSourceB.length()) {  
  20.             isBrother = false;  
  21.         } else {  
  22.             char[] charA_array = strSourceA.toCharArray();  
  23.             char[] charB_array = strSourceB.toCharArray();  
  24.             // 暴力轮询  
  25.             for (int i = 0; i < charA_array.length; i++) {  
  26.                 for (int j = 0; j < charB_array.length; j++) {  
  27.                     if (charA_array[i] == charB_array[j]) {  
  28.                         charA_array[i] = charB_array[j] = 0;  
  29.                         break;  
  30.                     }  
  31.                 }  
  32.             }  
  33.             for (int i = 0; i < charB_array.length; i++) {  
  34.                 if (charB_array[i] != 0) {  
  35.                     isBrother = false;  
  36.                     break;  
  37.                 }  
  38.             }  
  39.         }  
  40.         return isBrother;  
  41.     }  
  42.   
  43.     public static void main(String[] args) {  
  44.         String[] source = { "abc""cba""abdc""tec""aab""bbc""bca" };  
  45.         String target = "abc";  
  46.         for (int i = 0; i < source.length; i++) {  
  47.             if (isBrother(source[i], target)) {  
  48.                 System.out.println(source[i]);  
  49.             }  
  50.         }  
  51.     }  
  52.   
  53. }  

测试输出:

abc
cba
bca


(2)排序法

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.ray.interview.ch01.topic_1_2;  
  2.   
  3. /** 
  4.  * 搜查字典里面的兄弟字符串 
  5.  *  
  6.  * 兄弟字符串概念:长度相同、字符完全相同、顺序不一定一样的字符串,例如“abc”和“cba”就是兄弟字符串,“aab”和“aba”也是 
  7.  *  
  8.  * 在这里的字典我们为了方便,选用简单的字符串数组来替代,一般大的字典都是大数据的字符串文件 
  9.  *  
  10.  *  
  11.  * @author raylee 
  12.  * @date 2016-02-17 
  13.  * @version 1.0 
  14.  */  
  15. public class ContainString_4 {  
  16.   
  17.     public static boolean isBrother(String strSourceA, String strSourceB) {  
  18.         boolean isBrother = true;  
  19.         if (strSourceA.length() != strSourceB.length()) {  
  20.             isBrother = false;  
  21.         } else {  
  22.             char[] charA_array = strSourceA.toCharArray();  
  23.             char[] charB_array = strSourceB.toCharArray();  
  24.             // 1.排序  
  25.             quickSort(charA_array, 0, charA_array.length - 1);  
  26.             quickSort(charB_array, 0, charB_array.length - 1);  
  27.             // 2.遍历两个char数组,对比每一个字符,其实就是String里面的equals,但是我们这里不准备使用库函数  
  28.             int i = 0;  
  29.             int n = charA_array.length;  
  30.             while (n-- != 0) {  
  31.                 if (charA_array[i] != charB_array[i]) {  
  32.                     isBrother = false;  
  33.                     break;  
  34.                 }  
  35.                 i++;  
  36.             }  
  37.         }  
  38.         return isBrother;  
  39.     }  
  40.   
  41.     /** 
  42.      * 快速排序 
  43.      *  
  44.      * @param chars 
  45.      * @param start 
  46.      * @param end 
  47.      */  
  48.     public static void quickSort(char[] chars, int start, int end) {  
  49.         if (start < end) {  
  50.             char base = chars[start]; // 选定的基准值(第一个数值作为基准值)  
  51.             char temp; // 记录临时中间值  
  52.             int i = start, j = end;  
  53.             do {  
  54.                 while ((chars[i] < base) && (i < end))  
  55.                     i++;  
  56.                 while ((chars[j] > base) && (j > start))  
  57.                     j--;  
  58.                 if (i <= j) {  
  59.                     temp = chars[i];  
  60.                     chars[i] = chars[j];  
  61.                     chars[j] = temp;  
  62.                     i++;  
  63.                     j--;  
  64.                 }  
  65.             } while (i <= j);  
  66.             if (start < j)  
  67.                 quickSort(chars, start, j);  
  68.             if (end > i)  
  69.                 quickSort(chars, i, end);  
  70.         }  
  71.     }  
  72.   
  73.     public static void main(String[] args) {  
  74.         String[] source = { "abc""cba""abdc""tec""aab""bbc""bca" };  
  75.         String target = "abc";  
  76.         for (int i = 0; i < source.length; i++) {  
  77.             if (isBrother(source[i], target)) {  
  78.                 System.out.println(source[i]);  
  79.             }  
  80.         }  
  81.     }  
  82.   
  83. }  

测试输出:

abc
cba
bca


(3)HashMap方法

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.ray.interview.ch01.topic_1_2;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Iterator;  
  5.   
  6. /** 
  7.  * 搜查字典里面的兄弟字符串 
  8.  *  
  9.  * 兄弟字符串概念:长度相同、字符完全相同、顺序不一定一样的字符串,例如“abc”和“cba”就是兄弟字符串,“aab”和“aba”也是 
  10.  *  
  11.  * 在这里的字典我们为了方便,选用简单的字符串数组来替代,一般大的字典都是大数据的字符串文件 
  12.  *  
  13.  *  
  14.  * @author raylee 
  15.  * @date 2016-02-17 
  16.  * @version 1.0 
  17.  */  
  18. public class ContainString_5 {  
  19.   
  20.     public static boolean isBrother(String strSourceA, String strSourceB) {  
  21.         boolean isBrother = true;  
  22.         if (strSourceA.length() != strSourceB.length()) {  
  23.             isBrother = false;  
  24.         } else {  
  25.             char[] charA_array = strSourceA.toCharArray();  
  26.             char[] charB_array = strSourceB.toCharArray();  
  27.   
  28.             // 1.把字符放到散列表里面  
  29.             HashMap<Character, Integer> mapA = new HashMap<>();  
  30.             for (int i = 0; i < charA_array.length; i++) {  
  31.                 if (mapA.get(charA_array[i]) == null) {  
  32.                     mapA.put(charA_array[i], 1);  
  33.                 } else {  
  34.                     mapA.put(charA_array[i], mapA.get(charA_array[i]) + 1);  
  35.                 }  
  36.             }  
  37.   
  38.             HashMap<Character, Integer> mapB = new HashMap<>();  
  39.             for (int i = 0; i < charB_array.length; i++) {  
  40.                 if (mapB.get(charB_array[i]) == null) {  
  41.                     mapB.put(charB_array[i], 1);  
  42.                 } else {  
  43.                     mapB.put(charB_array[i], mapB.get(charB_array[i]) + 1);  
  44.                 }  
  45.             }  
  46.   
  47.             // 2.对比散列表  
  48.             if (mapA.size() != mapB.size()) {// 由于字符相同,因此map的size相同,不同则不是  
  49.                 isBrother = false;  
  50.             } else {  
  51.                 Iterator<Character> keyIterator = mapA.keySet().iterator();  
  52.                 while (keyIterator.hasNext()) {  
  53.                     Character key = (Character) keyIterator.next();  
  54.                     if (mapA.get(key) != mapB.get(key)) {  
  55.                         isBrother = false;  
  56.                         break;  
  57.                     }  
  58.                 }  
  59.             }  
  60.         }  
  61.         return isBrother;  
  62.     }  
  63.   
  64.     public static void main(String[] args) {  
  65.         String[] source = { "abc""cba""abdc""tec""aab""bbc""bca" };  
  66.         String target = "abc";  
  67.         for (int i = 0; i < source.length; i++) {  
  68.             if (isBrother(source[i], target)) {  
  69.                 System.out.println(source[i]);  
  70.             }  
  71.         }  
  72.   
  73.     }  
  74.   
  75. }  

测试输出:

abc
cba
bca


总结:这一章节主要介绍查找字典里面的兄弟字符串的几种方法。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值