2011-10-23 150 views
0

给定一个字符串,我怎么能弄清楚一个字符串中的每个字符重演 前的次数:aaaabbaaDD 输出:4a2b2a2D我如何计算字符串中特定字符的数量?

public static void Calc() { 

     Input(); 

     int count = 1; 

     String compressed = ""; 

     for (int i = 0; i < input.length(); i++) { 

      if (lastChar == input.charAt(i)) { 

       count++; 

       compressed += Integer.toString(count) + input.charAt(i); 
      } 

      else { 

       lastChar = input.charAt(i); 
       count = 1; 
      } 

     } 

     System.out.println(compressed); 

    } 
+0

这是一个非常具体的要求,所以你必须为此编写代码。不过应该不难,只是一个带有游程长度计数器的循环。 – Thilo

+0

我试过,但由于某种原因,我无法弄清楚它。 – user647207

+0

你可以把你的代码放在这里,这将帮助我们指出你在正确的方向。 –

回答

1

找你要找的是“行程编码”是什么。这是做这件事的工作代码;

import java.util.regex.Matcher; 
import java.util.regex.Pattern; 
public class RunLengthEncoding { 

public static String encode(String source) { 
    StringBuffer dest = new StringBuffer(); 
    // iterate through input string 
    // Iterate the string N no.of.times where N is size of the string to find run length for each character 
    for (int i = 0; i < source.length(); i++) { 
     // By default run Length for all character is one 
     int runLength = 1; 

     // Loop condition will break when it finds next character is different from previous character. 
     while (i+1 < source.length() && source.charAt(i) == source.charAt(i+1)) { 
      runLength++; 
      i++; 
     } 
     dest.append(runLength); 
     dest.append(source.charAt(i)); 
    } 
    return dest.toString(); 
} 

public static String decode(String source) { 
    StringBuffer dest = new StringBuffer(); 
    Pattern pattern = Pattern.compile("[0-9]+|[a-zA-Z]"); 
    Matcher matcher = pattern.matcher(source); 
    while (matcher.find()) { 
     int number = Integer.parseInt(matcher.group()); 
     matcher.find(); 
     while (number-- != 0) { 
      dest.append(matcher.group()); 
     } 
    } 
    return dest.toString(); 
} 

public static void main(String[] args) { 
    String example = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW"; 
    System.out.println(encode(example)); 
    System.out.println(decode("1W1B1W1B1W1B1W1B1W1B1W1B1W1B")); 
} 
} 
+0

我确实遇到过它不明白它正在做的一点.. – user647207

+0

在编码方法中添加文档。看看它是否有道理。 – Bala

0

一些提示:在您的代码示例,你还需要重置count为0时运行结束(当你更新lastChar)。并且你需要输出最后的运行(循环完成后)。这两种情况之间需要某种elsecontinue

0

@Balarmurugan K公司的解决方案是更好的 - 只是通过提高在你的代码,我想出了这一点 -

String input = "aaaabbaaDD"; 
    int count = 0; 
    char lastChar = 0; 
    int inputSize = input.length(); 
    String output = ""; 
    for (int i = 0; i < inputSize; i++) { 
     if (i == 0) { 
      lastChar = input.charAt(i); 
      count++; 
     } else { 
      if (lastChar == input.charAt(i)) { 
       count++; 
      } else { 
       output = output + count + "" + lastChar; 
       count = 1; 
       lastChar = input.charAt(i); 
      } 
     } 
    } 
    output = output + count + "" + lastChar; 
    System.out.println(output); 
+0

如果你不介意,你能解释一下你的代码吗? – user647207

+0

我怎样才能把所有的输出放到一个字符串中 – user647207

+0

@ user647207 - 编辑我的代码以适合您的需求 - 查看它。 –

1

该程序首先查找字符串中的独特字符或数字。然后它会检查发生的频率。 该程序将大写和小写视为不同的字符。如果需要,可以使用ignorecase方法修改它。


import java.io.*; 
public class RunLength { 
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    public static void main(String[] args) throws IOException { 
     System.out.println("Please enter the string"); 
     String str = br.readLine();//the input string is in str 
     calculateFrequency(str); 
    } 
    private static void calculateFrequency(String str) { 
     int length = str.length(); 
     String characters[] = new String[length];//to store all unique characters in string 
     int frequency[] = new int[length];//to store the frequency of the characters 
     for (int i = 0; i < length; i++) { 
      characters[i] = null; 
      frequency[i] = 0; 
     }

//To get unique characters char temp; String temporary; int uniqueCount = 0; for (int i = 0; i < length; i++) { int flag = 0; temp = str.charAt(i); temporary = "" + temp; for (int j = 0; j < length; j++) { if (characters[j] != null && characters[j].equals(temporary)) { flag = 1; break; } } if (flag == 0) { characters[uniqueCount] = temporary; uniqueCount++; } } // To get the frequency of the characters for(int i=0;i<length;i++){ temp=str.charAt(i); temporary = ""+temp; for(int j=0;i<characters.length;j++){ if(characters[j].equals(temporary)){ frequency[j]++; break; } } } // To display the output for (int i = 0; i < length; i++) { if (characters[i] != null) { System.out.println(characters[i]+" "+frequency[i]); } } }}
相关问题