字符串----基础训练(二)

题目一:压缩字符串。实现一个算法,利用字符重复出现的次数,实现基本的字符串压缩功能。比如,测试样例"aabcccccaaa" 返回:"a2b1c5a3"。若压缩后的字符串没有变短,则返回原先的字符串。

代码:

 1 public class Zipper {
 2 
 3     public static void main(String[] args) {
 4         String res = zipString("aabcccccaaa");
 5         System.out.println(res);
 6     }
 7     
 8     static String zipString(String src){
 9         int count = 0; // 记录前一个字符的重复次数
10         char last = 0; // 上一个字符
11         StringBuilder sb = new StringBuilder();
12         for (int i = 0; i < src.length(); i++) {
13             char charAt = src.charAt(i);
14             if (sb.length()==0) {  // 处理第一个字符
15                 sb.append(charAt);
16                 count++;
17             }else {
18                 if (last==charAt){  // 和上一个字符相同
19                     count++;
20                 }else {              // 和上一个字符不同
21                     sb.append(count).append(charAt);
22                     count = 1;
23                 }
24             }
25             last = charAt;
26         }
27         // 考虑最后一个字符的重复次数
28         if (count>=1) {
29             sb.append(count);
30         }
31         // 比较新字符串和原字符串
32         if (sb.length()>= src.length()) {
33             System.out.println("字符串没有变短:"+sb.toString());
34             return "原字符串:"+src;
35         }
36         return sb.toString();
37     }
38 
39 }

题目二:判断两字符串的字符集是否相同。实现一个算法,判断两个字符串是否由相同的字符所组成,不用管重复次数。如"abc","abccc",这两个字符串的字符集相同,都由abc组成,返回true。

代码:

 1 public class HasSameCharSet {
 2 
 3     public static void main(String[] args) {
 4         System.out.println(check_1("abc", "ab"));
 5         System.out.println(check_1("abccc", "abcd"));
 6     }
 7     
 8     /**
 9      * 限制字符串组成的字符为ASCII
10      * 解法一
11      */
12     static boolean check_1(String s1,String s2){
13         int[] help1 = new int[128];
14         //扫描s1
15         for (int i = 0; i < s1.length(); i++) {
16           char c = s1.charAt(i);
17           if (help1[c] == 0)
18             help1[c] = 1;
19         }
20         
21         int[] help2 = new int[128];
22         //扫描s2
23         for (int i = 0; i < s2.length(); i++) {
24           char c = s2.charAt(i);
25           if (help2[c] == 0)
26               help2[c] = 1;
27         }
28         for (int i = 0; i < help2.length; i++) {
29             if (help1[i]!=help2[i]) {
30                 return false;
31             }
32         }
33         return true;
34     }
35     
36 }

题目三:旋转词问题。如果一个字符串str,把字符串str前面任意的部分挪到后面形成的字符串叫做str的旋转词。判断两个字符串是否互为旋转词。

代码:

 1 public class IsRotate {
 2 
 3     public static void main(String[] args) {
 4         System.out.println(isRotate("defa", "fabdde"));
 5         System.out.println(isRotate("abc", "cab"));
 6         // 输出 false true
 7     }
 8     
 9     static boolean isRotate(String a,String b){
10         if (a.length()!=b.length()) {   // 判断 a是不是 b的一个旋转词的字串
11             return false;
12         }
13         StringBuilder sb = new StringBuilder(b).append(b);
14         return sb.toString().contains(a);
15     }
16 }

题目四:翻转单词。将字符串按单词翻转,如here you are 翻转成are you here

代码:

 1 public class WordReverse {
 2 
 3     public static void main(String[] args) {
 4         System.out.println(reverse("where you are"));
 5     }
 6     
 7     // 首先将整个字符串按照字符翻转,再找到每个单词,将单词翻转
 8     static String reverse(String src){
 9         String s1 = reverseString(src);
10         // 切割单词
11         String[]words = s1.split("\s");
12         StringBuilder sb = new StringBuilder();
13         for (int i = 0; i < words.length; i++) {
14             sb.append(reverseString(words[i])+ " ");
15         }
16         return sb.deleteCharAt(sb.length()-1).toString();
17     }
18     public static String reverseString(String iniString){
19 //        StringBuilder sBuilder = new StringBuilder(iniString)  // 和StringBuffer效果差不多。 
20         StringBuffer sBuffer = new StringBuffer(iniString);
21         return sBuffer.reverse().toString();
22     }
23 
24 }
原文地址:https://www.cnblogs.com/xiaoyh/p/10304381.html