1. String getOrderedString(boolean isDuplicated, String … str)

说明:

Orders all characters in the input strings and return the ordered string.(note: only considering the alphabets and digits) 

i.e:

(false, {"ahcdx", "abcuy", "cejm"}) ->"abcdehjmuxy"

(true, {"ahcdx", "abcuy", "cejm"}) ->"aabcdehjmuxy"

/**
* Orders all characters in the input strings without duplicated str.
*
* @Title: getOrderedString
* @param isDuplicated true/false
* @param str input str
* @return ordered string
*/
public static String getOrderedString(boolean isDuplicated, String... str) {
if (str == null || str.length == 0) {
return null;
} // initialize array
int[] charArrayTmp = new int[MAX_CHAR_LENGTH];
System.arraycopy(CHARARRAY, 0, charArrayTmp, 0, MAX_CHAR_LENGTH); int length = str.length;
String value = null;
int valueLength = 0;
char tempValue;
int totalCounts = 0; // merge and sort the input strs
for (int i = 0; i < length; i++) {
value = str[i];
valueLength = value.length();
totalCounts += valueLength; for (int j = 0; j < valueLength; j++) {
tempValue = value.charAt(j); if (isDuplicated) {
charArrayTmp[tempValue] += 1;
} else {
charArrayTmp[tempValue] = 1;
} }
} char[] newChar = new char[totalCounts];
int counts = 0;
int len = 0;
// append result that has been merged and sorted
for (int i = MIN_CHAR_LENGTH; i < MAX_CHAR_LENGTH; i++) {
len = charArrayTmp[i]; if (len != 0) {
for (int j = 0; j < len; j++) {
newChar[counts++] = (char) i;
}
} }
return new String(newChar, 0, counts);
}

2.  List<String> getMostLongDifferent(String str)
说明:
Returns the longest consecutive different substring.

i.e: 

("abcabefg") -> "cabefg"

    /**
* @Description: get the longest consecutive different substring.
* @Title: getMostLongDifferent
* @param str input string
* @return the longest consecutive different substring of input
*/
public static List<String> getMostLongDifferent(String str) {
if (isEmpty(str)) {
return null;
}
int len = str.length();
Map<Character, Integer> cursor = new HashMap<Character, Integer>();
cursor.put(str.charAt(0), 0);
int[] lengthAt = new int[len];
lengthAt[0] = 1;
int max = 0;
for (int i = 1; i < len; i++) {
char cha = str.charAt(i); if (cursor.containsKey(cha)) {
lengthAt[i] = Math.min(lengthAt[i - 1] + 1, i - cursor.get(cha));
} else {
lengthAt[i] = lengthAt[i - 1] + 1;
}
max = (max >= lengthAt[i]) ? max : lengthAt[i];
cursor.put(cha, i);
} List<String> resultList = new ArrayList<String>();
for (int i = 0; i < len; i++) {
if (max == lengthAt[i]) {
String resultString = str.substring(i - max + 1, i + 1);
if (!resultList.contains(resultString)) {
resultList.add(resultString);
}
}
}
return resultList;
}

3. String normalizeSpace(String str)

说明:
Remove leading and trailing whitespace and then replacing sequences of whitespace characters by a single space.

/**
* Remove leading and trailing whitespace and then replacing sequences of whitespace characters.
* by a single space
*
* @param str the string need to be normalize space
* @return string result after normalize space
*/
public static String normalizeSpace(String str) {
if (str == null) {
return null;
} str = str.trim(); if (str.length() <= 3) {
return str;
} if (str.indexOf(CommonConstants.DOUBLE_BLANKS, 1) >= 0) {
char[] chars = str.toCharArray();
int index = 0; for (int i = 0, len = chars.length; i < len; i++) {
if (chars[i] != CommonConstants.CHAR_BLANKS || chars[i - 1] != CommonConstants.CHAR_BLANKS) {
chars[index++] = chars[i];
}
} return new String(chars, 0, index);
} else {
return str;
}
}

String 经常用法最优算法实现总结 (二)的更多相关文章

  1. String 经常用法最优算法实现总结 (一)

    <pre name="code" class="java"><span style="font-family: Arial, Hel ...

  2. string基本字符系列容器(二)

    string对象作为vector元素 string对象可以作为vector向量元素,这种用法类似字符串数组. #include<string> #include<vector> ...

  3. C#中string.format用法详解

    C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...

  4. String.format()用法

    package junit.test;   import java.util.Date; import java.util.Locale;   import org.junit.Test;   pub ...

  5. java中String的用法

    String的用法很活跃,也用到的很多.可以根据自己的需要查询API.这里只有concat和substring,indexof的用法 class TestString { public static ...

  6. C#中string.Format 用法详解

    这篇文章主要介绍了C#中string.format用法,以实例形式较为详细的讲述了string.format格式化的各种用法,非常具有实用价值,需要的朋友可以参考下 本文实例总结了C#中string. ...

  7. Oracle中dbms_random.string 的用法

    转载:https://blog.csdn.net/simonchi/article/details/8657787 DBMS_RANDOM.STRING(var1,var2) 这个函数有两个参数 va ...

  8. 关于java中String的用法

    在java 中String存在许多的基本函数,接下来了解一下这些函数的基本用法 String.equals用法(这个用法比较难) String类中的equals()方法: public boolean ...

  9. java成神之——java中string的用法

    java中String的用法 String基本用法 String分割 String拼接 String截取 String换行符和format格式化 String反转字符串和去除空白字符 String获取 ...

随机推荐

  1. 疑似CPU或者内存故障导致进程崩溃

    我们有一个服务跑在微软云的所有宿主机上.最近发现某一台机器上该服务进程持续崩溃.崩溃原因是访问了一个无效指针,对应的代码如下 serviceListIniBuffer.AppendF("Se ...

  2. 清理win10过期补丁的命令

    作用是删除已经被新版本取代的旧系统文件 DISM.exe /Online /Cleanup-Image /StartComponentCleanup /ResetBase 注1: 执行后, 补丁就无法 ...

  3. word,excel,ppt转Pdf,Pdf转Swf,通过flexpaper+swftools实现在线预览

    其实这是我好几年前的项目,现在再用这种方式我也不建议了,毕竟未来flash慢慢会淘汰,此方式也是因为目测大部分人都装了flash,才这么做的,但是页面展示效果也不好.其实还是考虑收费的控件,毕竟收费的 ...

  4. [转载] 谷歌技术"三宝"之MapReduce

    转载自http://blog.csdn.net/opennaive/article/details/7514146 江湖传说永流传:谷歌技术有"三宝",GFS.MapReduce和 ...

  5. 【转】MYSQL 使用SQLyog导入遇到问题解决

    原文地址:http://blog.163.com/o5655@126/blog/static/1667428342010910112510738/  昨天公司想要将一个数据库的数据导出再导入到另外一个 ...

  6. 基础拾遗-----mongoDB操作

    基础拾遗 基础拾遗------特性详解 基础拾遗------webservice详解 基础拾遗------redis详解 基础拾遗------反射详解 基础拾遗------委托详解 基础拾遗----- ...

  7. hibernate:There is a cycle in the hierarchy! 造成死循环解决办法

    下面是报的异常:在网上搜了关于:There is a cycle in the hierarchy!,才知道原来是因为死循环造成的!解决了好久,没有成功,后台不得已请教老大,老大说是因为在使用JSON ...

  8. P3092 [USACO13NOV]没有找零No Change

    题目描述 Farmer John is at the market to purchase supplies for his farm. He has in his pocket K coins (1 ...

  9. P1457 城堡 The Castle

    轻度中毒 原题 :The Castle 以下为题解部分:明明辣么简单的一道题,硬是搞了1.5h,WTF?以下列出本题的一些要点. 搜索(DFS)嘛,染色嘛,统计大小嘛,很容易想,也很更易处理. 接下来 ...

  10. thinkphp做搜索功能

    一般后台都需要做一些搜索功能,直接上图. 至于前端页面大家自己设计.