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获取 ...
随机推荐
- 利用echarts highcharts 实现自定义地图 关系图效果 侧边3D柱形图饼图散点图
github 地址: https://https://github.com/Gengshaoxuan/medataMap github 地址: https://https://github.com ...
- 八皇后问题 dfs/递归
#include <bits/stdc++.h> using namespace std; const int maxn = 55; int ans=0; int vis_Q[maxn]; ...
- ruby 正则表达式 匹配规则
- 浅析Struts2中的OGNL和ValueStack
要了解Struts2与OGNL表达式的关系,我们必须先搞清楚以下三个概念: 1. ActionContext它是Action运行的上下文环境,Action的多项设置都存放在次,我们每一次Action ...
- Spring 4 MVC example with Maven
In this tutorial, we show you a Spring 4 MVC example, using Maven build tool. Technologies used : Sp ...
- javascript第七章--DOM
① 节点层次 ② DOM操作技术
- LDA算法入门
http://blog.csdn.net/warmyellow/article/details/5454943 LDA算法入门 一. LDA算法概述: 线性判别式分析(Linear Discrimin ...
- git使用(上)-----基本的方法
git应该是一项必须要掌握的工具.先简述它和SVN的区别 SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是自己的电脑,所以首先要从中央服务器哪里得到最新的版本,然后干活 ...
- 玩转html
简介 CSS 是什么? CSS是Cascading Style Sheets的简称,中文称为层叠样式表. 作用 能够对网页中元素位置的排版进行像素级精确控制,支持几乎所有的字体字号样式,拥有对网页对象 ...
- 2.1synchronized同步方法
由前言: 在第一章已经出现了非线程安全的情况."非线程安全"其实会发生在多个线程同时对同一个对象中的实例变量进行访问时发生.产生的结果就是脏读(读到被修改过的数据). " ...