---------- android培训java培训、期待与您交流! ----------

践行渐远2014

[黑马论坛]23期技术活动题目及答案!

-------------23期限技术题:

题目:已知两个字符串,String a = "abcdefghijklmnopqrstuvwxyz";String b="1234567890abcdfrgtddd12321414";编写代码求出这两个字符串中最大公共字符串(最大公共字符串:比如 “abc123edf”和"bc123jg"的最大公共字符串是“bc123”)。上传代码的时候将运算结果截图一起提交。

 

-------------答案:

 

package com.itheima.practice;

 

public class MaxLenghtStringIntersection {

 

/**
* @param args
*/
public static void main(String[] args) {

 

  String a = "abcdefghijklmnopqrstuvwxyz";
  String b = "1234567890abcdfrgtddd12321414";

 

  String result = getMaxLenghtStringIntersection(a, b);
  System.out.println(result);

 

}

 

private static String getMaxLenghtStringIntersection(String a, String b) {

 

  String maxLenStr = null;
  String minLenStr = null;
  maxLenStr = (a.length() > b.length())? a : b;
  minLenStr = maxLenStr.equals(a) ? b : a;
  for (int i = 0; i < minLenStr.length(); i++) {
    for (int start = 0, end = minLenStr.length() - i; end != minLenStr.length() + 1; start++, end++) {
      String sub = minLenStr.substring(start, end);
        if (maxLenStr.contains(sub))
          return sub;
    }
  }
  return null;
  }
}

posted on 2014-11-28 15:53  践行渐远2014  阅读(165)  评论(0编辑  收藏  举报

---------- android培训java培训、期待与您交流! ----------