為了完成我的作業,我需要一個代碼,它從用戶那里獲取一個單詞,然后識別連續字母的數量并以列印字母和重復次數的方式輸出它。
示例 1 輸入:
hhhttrew
示例 1 輸出:
h3t2rew
示例 2 輸入:
uuuuuuhhhaaajqqq
示例 2 輸出:
u6h3a3jq3
String text = sc.nextLine();
int len = text.length();
int repeat = 0;
char[] chars = new char[len];
// To convert string to char
for (int h = 0; h < len; h )
{
chars[h] = text.charAt(h);
}
String finaly = "";
for (char ignored : chars)
{
for (int j = 0 ; j <len ; j )
{
if (chars[j] == chars[j 1])
{
finaly = String.valueOf(chars[j]);
repeat ;
finaly = String.valueOf(repeat);
}
else
{
j ;
}
}
}
System.out.println(finaly);
uj5u.com熱心網友回復:
這是一種方法。你只需要一個回圈。內部回圈完成作業。外回圈只是提供測驗用例。
- 分配第一個字符
- 并將該字符的計數設定為 1
- 然后迭代直到相鄰字符不同
- 如果 > 1 則追加計數并追加不同的字符
- 將計數設定為 0 以便下次運行。
String[] data = { "uuuuuuhhhaaajqqq",
"hhhttrew","abbcccddddeeeeeffffffggggggg" };
for (String s : data) {
String result = "" s.charAt(0);
int count = 1;
for (int i = 1; i < s.length(); i ) {
if (s.charAt(i - 1) != s.charAt(i)) {
result = count <= 1 ? "" : count;
result = s.charAt(i);
count = 0;
}
count ;
if (i == s.length() - 1) {
result = count <= 1 ? "" : count;
}
}
System.out.printf("%-15s <-- %s%n", result, s);
}
印刷
u6h3a3jq3 <-- uuuuuuhhhaaajqqq
h3t2rew <-- hhhttrew
ab2c3d4e5f6g7 <-- abbcccddddeeeeeffffffggggggg
在評論(現已洗掉)中,您詢問了如何扭轉這一程序。這是一種方法。
- 分配 a
StringBuilder來保存結果。 - 初始化
count和currentChar - 在處理字串時,
- 將字符保存到
currentChar - 然后當下一個字符是數字時,建立計數
- 將字符保存到
- 如果計數仍為 0,則下一個字符是數字,因此將計數加一并將其復制
currentChar到緩沖區 - 否則,使用計算的長度。
String[] encoded =
{ "u6h3a3jq3", "h3t2rew", "ab2c3d4e5f6g7" };
for (String s : encoded) {
StringBuilder sb = new StringBuilder();
int count = 0;
char currentChar = '\0';
for (int i = 0; i < s.length();) {
if (Character.isLetter(s.charAt(i))) {
currentChar = s.charAt(i );
}
while (i < s.length()
&& Character.isDigit(s.charAt(i))) {
count = count * 10 s.charAt(i ) - '0';
}
count = count == 0 ? 1 : count;
sb.append(Character.toString(currentChar)
.repeat(count));
count = 0;
}
System.out.println(s " --> " sb);
}
印刷
u6h3a3jq3 --> uuuuuuhhhaaajqqq
h3t2rew --> hhhttrew
ab2c3d4e5f6g7 --> abbcccddddeeeeeffffffggggggg
uj5u.com熱心網友回復:
只需一個回圈即可大大改進您的解決方案。此外,這不使用任何復雜的資料結構,因此非常高效。
public static String consecutiveLetters(String text) {
if (text == null || text.length() == 0) return "";
// We start with he first letter
char currentChar = text.charAt(0);
int position = 1;
int letterCount = 1;
StringBuilder outputBuilder = new StringBuilder();
while (position < text.length()) {
// get the letter at the current position
char charAtCurrentPos = text.charAt(position);
// if it is different than what we had previously, we store the letter and the number of times it appeared. Reset the counter for the new letter
if (charAtCurrentPos != currentChar) {
outputBuilder.append(currentChar);
currentChar = charAtCurrentPos;
if (letterCount > 1) {
outputBuilder.append(letterCount);
}
letterCount = 1;
} else {
letterCount ;
}
position ;
}
// Add the last character as well
outputBuilder.append(currentChar);
if (letterCount > 1) {
outputBuilder.append(letterCount);
}
return outputBuilder.toString();
}
uj5u.com熱心網友回復:
您可以獲得正則運算式的匹配項
(.)\1*
這會產生一個連續字符的字串陣列。例如,
"uuuuuuhhhaaajqqq" => ["uuuuuu", "hhh", "aaa", "j", "qqq"]
然后將每個元素轉換為所需的形式是一件簡單的事情:
["uuuuuu", "hhh", "aaa", "j", "qqq"] => ["u6", "h3", "a3", "j", "q3"]
然后連接后一個陣列的元素(連接字串為空)以形成所需的字串:
"u6h3a3jq3"
正則運算式可以分解如下:
( # begin capture group 1
. # match any character
) # end capture group 1
\1* # match the contents of capture group 1 zero one or more times
作為提取上述正則運算式的匹配項的一種替代方法,可以在正則運算式的匹配項上拆分字串
(?<=(.))(?!\1)
這將產生與以前相同的陣列:
"uuuuuuhhhaaajqqq" => ["uuuuuu", "hhh", "aaa", "j", "qqq"]
此正則運算式具有以下元素。
(?<= # begin positive lookbehind
( # begin capture group 1
. # match any character
) # end capture group 1
) # end positive lookbehind
(?! # begin a negative lookahead
\1 # match the contents of capture group 1
) # end negative lookahead
此運算式的匹配項是零寬度,這意味著它們匹配相鄰字符之間的位置。例如,如果字串是"abbc",它將匹配'a'and'b'和之間的位置'b'and 'c',將字串分成三部分。
uj5u.com熱心網友回復:
我會采取不同的方法。以下演算法將運行O(n),因此是漸近(!!!)最優的。說清楚,因為這似乎引起了一些混亂。有更有效的解決方案具有更小的常數。
本質上,這個想法是遍歷所有字符并將出現的次數存盤在Map. 如果遇到尚未在 Map 中的值,我們將其添加到 Map 中,并為其計數 1。如果我們在獲取該值之前已經遇到該值并將其加一。最后我們只需要按照插入的順序遍歷map一次,得到key和value。
重要提示:對于這個實作來說,使用 aLinkedHashMap作為插入順序是必須保留的,這一點至關重要。A HashMapofTreeMap會給你一個字串,它的值以未定義的順序排列。有關這方面的更多詳細資訊,請參閱此執行緒。
該演算法的運行時間將是 O(n),因為所有操作LinkedHashMap都O(1)在鏡頭中,其余時間我們只是在n字符上回圈。
import java.util.*;
public class Application {
public static void main(String[] args) {
var result1 = compress("hhhttrew");
var result2 = compress("uuuuuuhhhaaajqqq");
System.out.println(result1);
System.out.println(result2);
}
public static String compress(String uncompressed) {
var charArr = uncompressed.toCharArray();
// LinkedHashMap is required here to preserve the order of insertion
var occurrences = new LinkedHashMap<Character, Integer>();
// iterate over all characters and count all occurrences
for (var character : charArr) {
if (occurrences.containsKey(character)) occurrences.put(character, occurrences.get(character) 1);
else occurrences.put(character, 1);
}
// Create the target String by iterating over the map in order of insertion and concatenate key and value. Don't add the number of occurrence when the character occurs only once
var sb = new StringBuilder();
for (var entry : occurrences.entrySet()) {
sb.append(entry.getKey());
if(entry.getValue() != 1) sb.append(entry.getValue());
}
return sb.toString();
}
}
預期輸出:
h3t2rew
u6h3a3jq3
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/452506.html
上一篇:分詞演算法的空間復雜度
