String 经常用法最优算法实现总结 (二)
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);
}
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
*
* @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 经常用法最优算法实现总结 (二)的更多相关文章
- String 经常用法最优算法实现总结 (一)
<pre name="code" class="java"><span style="font-family: Arial, Hel ...
- string基本字符系列容器(二)
string对象作为vector元素 string对象可以作为vector向量元素,这种用法类似字符串数组. #include<string> #include<vector> ...
- C#中string.format用法详解
C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...
- String.format()用法
package junit.test; import java.util.Date; import java.util.Locale; import org.junit.Test; pub ...
- java中String的用法
String的用法很活跃,也用到的很多.可以根据自己的需要查询API.这里只有concat和substring,indexof的用法 class TestString { public static ...
- C#中string.Format 用法详解
这篇文章主要介绍了C#中string.format用法,以实例形式较为详细的讲述了string.format格式化的各种用法,非常具有实用价值,需要的朋友可以参考下 本文实例总结了C#中string. ...
- Oracle中dbms_random.string 的用法
转载:https://blog.csdn.net/simonchi/article/details/8657787 DBMS_RANDOM.STRING(var1,var2) 这个函数有两个参数 va ...
- 关于java中String的用法
在java 中String存在许多的基本函数,接下来了解一下这些函数的基本用法 String.equals用法(这个用法比较难) String类中的equals()方法: public boolean ...
- java成神之——java中string的用法
java中String的用法 String基本用法 String分割 String拼接 String截取 String换行符和format格式化 String反转字符串和去除空白字符 String获取 ...
随机推荐
- 在游览器上可以连网,Ionic打包后不能连接网络
在游览器上可以连网,Ionic打包后不能连接网络.可能是没有安装cordova-plugin-whitelist插件. 解决方案:
- javaScript函数提升及作用域
代码片段: var a = 1; function foo() { console.log(a); //输出为undefined if (!a) { var a = 2; } alert(a); }; ...
- 插件lombok的介绍安装
Lombok插件 介绍一个不错的Eclipse插件Lombok 该插件对Log4j简化的代码,因为不大,所以jar包也存在呢! Lombox是Eclipse的一个插件,用来自动生成Java代码,减少手 ...
- struts2 内容记录
多xml文件配置 在开发过程中我们经常会将每一张表(如:user表)的struts.xml文件分开,便于管理,故需要建立struts_user.xml文件管理请求等.那么需要用到inculde标签. ...
- [转载] Java并发编程:Callable、Future和FutureTask
转载自http://www.cnblogs.com/dolphin0520/p/3949310.html 在前面的文章中我们讲述了创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Run ...
- pattern
A compiled representation of a regular expression. A regular expression, specified as a string, must ...
- JavaWeb面试(七)
61,JDBC访问数据库的基本步骤是什么?1,加载驱动2,通过DriverManager对象获取连接对象Connection3,通过连接对象获取会话4,通过会话进行数据的增删改查,封装对象5,关闭资源 ...
- 【Java框架型项目从入门到装逼】第三节 - 如何用Tomcat发布web项目?
啥叫Tomcat?有道词典是这么说的. 这个我们姑且不管,实际上呢,Tomcat是一种Web服务器,我们自己做好了一个Web项目,就可以通过Tomcat来发布.服务器呢,又分为硬件服务器和软件服务器. ...
- jq实现全选或者全不选
$("#all").click(function () { if($(this).is(":checked")){ $("input[name='pr ...
- ORACLE 错误代码提示归集
有时数据库出现问题,不是每次都有网络可查,所以把所有的ora系列的错误整理出来, 在最没有办法的时候,需要自己来解决,有了这些根据,问题会好办的.虽说对于数据库方面, DBA很强大,他们在遇到错误时, ...