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获取 ...
随机推荐
- JAVAscript学习笔记 js条件语句 第三节 (原创) 参考js使用表 (2017-09-14 15:55)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【转】解决memcached启动失败
原文:http://chenzhou123520.iteye.com/blog/1925196 linux上启动Memcache报错: 原因一般有两个, 一个是操作系统里确实没有包含该共享库(lib* ...
- .Net 上传图片之前获取图片的宽高
Stream st = Request.Files[0].InputStream; Byte[] buffer = new Byte[st.Length]; ...
- JavaScript系列----面向对象的JavaScript(2)
本文中心: 这篇文章比较难懂,所以读起来比较晦涩.所以,我简单列一下提纲: 在第一部分,从函数原型开始谈起,目的是想搞明白,这个属性是什么,为什么存在,在创建对象的的时候起到了什么作用! 在第二部分, ...
- C# 面向对象基础&封装&继承&多态&加深一下冒泡排序写法
(一)面向对象是什么? 面向对象是一种编程思想 (二)为什么要用面向对象? 1.结构清晰 2.易于维护 3.方便扩展 (三)new一个对象是什么过程? 实例化构造函数创建对象的过程就是将类实例化的过程 ...
- java非阻塞IO(NIO)流程
单线程 多线程(Netty/Mina)
- QT制作窗口切换的小程序
前言:本次实验是在三个窗口之间自由切换,窗口中播放gif格式的动态图. 让我们先来看看使用到的主要的函数: 一.播放gif格式动态图的函数 QMovie *movie = new QMovie(&qu ...
- [转载] Redis之七种武器
转载自http://blog.nosqlfan.com/html/2942.html?ref=rediszt 长生剑.孔雀翎.碧玉刀.多情环.离别钩.霸王枪.拳头是古龙笔下的七种武器,而本文打算将Re ...
- Mybatis查询时报 Bad format for Time '454:54:54' in column 6 异常
报 Bad format for Time '454:54:54' in column 6 解决方案:1. 查询实体类和.xml数据是否相对应 2. 查询sql是否正确 3. 查看表的设计,是 ...
- Go环境搭建
Linux系统golang环境搭建 1.下载安装包go1.8.linux-amd64.tar golang安装包下载地址:https://golang.org/dl/ ( 有可能被FQ) 2.解 ...