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. Linux学习(二十)软件安装与卸载(三)源码包安装

    一.概述 源码包安装的优点在于它自由程度比较高,可以指定目录与组件.再有,你要是能改源码也可以. 二.安装方法 步骤 1.从官网或者信任站点下载源码包 [root@localhost ~]# wget ...

  2. app.config 配置多项 配置集合 自定义配置(3)

    再说说利用app.config配置多个自定义的方法.先看这个例子:美国家庭Simpson的家里有父亲母亲和三个儿女,而中国的老王只有独生子女.结构如下: <?xml version=" ...

  3. 在昆明网络SEO的走向站外的优化该何去何从?

    昨天大概讲了SEO的站内优化,今天我们来讲讲网站站外的优化. 站外主要以第三平台为主,其中包含站外推广:常规推广.外链建设:利用第三方平台优化关键词排名: 1.博客平台,现在有好多博客平台是很不错的, ...

  4. github创建远程仓库

    创建远程仓库 当你已经在本地创建了一个Git仓库后,又想在GitHub创建一个Git仓库,并且让这两个仓库进行远程同步,这样,GitHub上的仓库既可以作为备份,又可以让其他人通过该仓库来协作,真是一 ...

  5. 从零一起学Spring Boot之LayIM项目长成记(五)websocket

    前言 距离上一篇已经比较久的时间了,项目也是开了个头.并且,由于网上的关于Spring Boot的websocket讲解也比较多.于是我采用了另外的一个通讯框架 t-io 来实现LayIM中的通讯功能 ...

  6. auxblogcms1.0.6|代码审计

    这周的审计任务,两天前的任务呀~拖延症呀~ 这次审计一个博客----auxblogcms1.0.6,网上也有所记载,我下面会做个总结. axublog是一款php个人博客系统,小巧强大的PHP+MyS ...

  7. 如何用while循环输出十行十列变色★☆

    输出十行十列星星 k = 0 #设置一个终止变量 while k < 10: i = 0 #设置一个满十换行变量 while i < 10: print('★',end='') i += ...

  8. ERP服务器简单维护

    前言: 此页内容对于网管高手来说是小儿科,但是以我们对大多数企业的了解,依然有好多企业将服务器的日常维护给忽视了. 所以在此,给大家做一个宣传.让大家提高服务器维护的意识 以提高服务器运行的稳定性.安 ...

  9. MyRapid WinForm 快速开发框架

    MyRapid 框架介绍开发历程:作者是数据库相关软件开发从业人员,懒惰的,能交给电脑做的事情懒得自己做开发目的:处理底层数据传输,减少工作量,提高开发效率框架特点:数据库相关开发.易学易用.快速上手 ...

  10. 那些年我们写js烦的不疼不痒的错误

    1.Js 字符变量不加双/单引号. 列如:var strJsonInfo = '@Html.Raw(ViewBag.JsonInfo)'; 2.js 对象初始化器,最后一个属性值加逗号. 例如:var ...