如何合并两个不同的字符串
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何合并两个不同的字符串相关的知识,希望对你有一定的参考价值。
我正在尝试密码战中的卡塔。这是kata给出的方向:“给定两个字符串s1和s2,我们想直观地看到两个字符串的不同之处。我们只考虑小写字母(a到z)。首先让我们计算一下s1和s2中的每个小写字母。
s1 =“ A aaaaa bb c”
s2 =“&aaa bbb c d”
s1有4个'a',2个'b',1个'c'
s2具有3个'a',3个'b',1个'c',1个'd'
因此s1和s2中'a'的最大值相对于s1为4;从s2开始,“ b”的最大值为3。在下文中,当出现的最大次数小于或等于1时,我们将不考虑字母。“
我设法从字符串中仅将小写字母放入数组中,但是我一直坚持如何比较每个字母出现次数最多的两个数组并构建一个字符串。如果您有任何建议,我们将不胜感激!这是我的代码:
public class Mix
public static void main(String[] args)
mix("1aaAAB ", "cabBbB");
public static String mix(String s1, String s2)
int count1[] = new int[256]; //Number of characters that were found
int count2[] = new int[256];
int length1 = s1.length();
int length2 = s2.length();
char char1[] = new char[s1.length()];
char char2[] = new char[s2.length()];
for(int i = 0; i < length1; i++)
if(Character.isLowerCase(s1.charAt(i)))
char1[i] = s1.charAt(i);
int found1 = 0; //found = 1 if it is found. found = 0 if not found
for(int j = 0; j <= i; j++)
if(s1.charAt(i) == char1[j])
found1++;
//Char[i] has lowercase letters
for(int i = 0; i < length2; i++)
if(Character.isLowerCase(s2.charAt(i)))
char2[i] = s2.charAt(i);
int found2 = 0;
for(int j = 0; j <= i; j++)
if(s2.charAt(i) == char2[j])
found2++;
//Char[i] has lowercase letters
//////////////////////////
for(int i = 0; i < char1.length; i++)
if(char1[i] != 0)
count1[char1[i]]++;
System.out.println("There are " + count1[char1[i]] + " of the letter " + char1[i]);
for(int j = 0; j < char2.length; j++)
if(char2[j] != 0)
count2[char2[j]]++;
System.out.println("There are " + count2[char2[j]] + " of the letter " + char2[j]);
/*
This is where my problems begin. my char1 and char2 arrays contain the lower case letters.
I don't know how to do this part though.
*/
StringBuilder result = new StringBuilder();
int index = 0;
for(int i = 0; i < char1.length; i++)
for(int j = 0; j < char2.length; j++)
if(char1[i] == char2[j])
System.out.println("Char1 at i: " + char1[i] + " char2 at j: " + char2[j]);
if(count1[char1[i]] > count2[char2[j]])
for(int k = 0; k < count1[char1[i]]; k++)
result.append(char1[i]);
else if(count1[char1[i]] < count2[char2[j]])
for(int l = 0; l < count2[char2[j]]; l++)
result.append(char2[j]);
else
for(int h = 0; h < count1[char1[i]]; h++)
result.append(char1[i]);
return "End"; //This return is arbitrary for right now
答案
最好使用HashMap,其中键是字符,值是出现的次数
填写地图将是这样的:(我在这里使用内存,因此可能会有输入错误)
char c = s1.indexAt(i);
if(map.contains(c))
map.put(c, map.get(c)++);
else
map.put(c,1);
一旦填满两个地图,通过将两个HashMap中找到的键的值更新为两个的最大值,您只能保留1。
import java.util.HashMap;
import java.util.Map;
public class Mix
public static void main(String[] args)
System.out.println( mix("A aaaa bb c", "& aaa bbb c d") );
public static String mix(String s1, String s2)
Map<Character, Integer> firstStringCharsOccurences = new HashMap<>();
Map<Character, Integer> secondStringCharsOccurences = new HashMap<>();
Map<Character, Integer> total = new HashMap<>();
for(int i=0; i<s1.length(); i++)
if (Character.isLowerCase(s1.charAt(i)))
/*
* it will put a pair into the map with the char at index i as key and the value either the old value+1
* if it already exist, or if this is the first occurrence of this character it will put 0+1
*/
firstStringCharsOccurences.put(s1.charAt(i),
firstStringCharsOccurences.getOrDefault(s1.charAt(i), 0) + 1);
for(int i=0; i<s2.length(); i++)
if (Character.isLowerCase(s2.charAt(i)))
secondStringCharsOccurences.put(s2.charAt(i),
secondStringCharsOccurences.getOrDefault(s2.charAt(i), 0) + 1);
for( Character c: secondStringCharsOccurences.keySet() )
total.put(c,
Math.max(firstStringCharsOccurences.getOrDefault(c, 0),secondStringCharsOccurences.get(c)));
for( Character c: firstStringCharsOccurences.keySet() )
total.put(c,
Math.max(secondStringCharsOccurences.getOrDefault(c, 0),firstStringCharsOccurences.get(c)));
StringBuilder answer = new StringBuilder();
for( Character c: total.keySet() )
char stringWithMostOccurrences = firstStringCharsOccurences.getOrDefault(c, 0) > secondStringCharsOccurences.getOrDefault(c, 0) ? '1' :
( firstStringCharsOccurences.getOrDefault(c, 0) < secondStringCharsOccurences.getOrDefault(c, 0) ? '2' : '=' );
answer.append(stringWithMostOccurrences);
answer.append(':');
for(int i=0; i< total.get(c) ; i++)
answer.append(c);
answer.append('/');
return answer.toString();
一件事,我没有在最终输出中添加不小写的字符。
另一答案
最好的处理方式是HashMaps。
这里是我的解决方案:
将String转换为HashMap的方法。我使用了merge方法来避免检查密钥。
private HashMap<Character, Integer> stringToHashMap(String s)
HashMap<Character, Integer> hashMap = new HashMap<>();
for (char c: s.toCharArray())
if (Character.isLowerCase(c))
hashMap.merge(c, 1, (a, b) -> a + b );
return hashMap;
将HashMap转换为String的方法。
private String hashMapToString(HashMap<Character, Integer> hashMap)
StringBuilder result = new StringBuilder();
for (Map.Entry<Character,Integer> me : hashMap.entrySet())
for (int i= 0; i < me.getValue(); i++)
result.append(me.getKey());
return result.toString();
这里是调用两个助手并合并两个HashMap的方法。
public String solution(String s1, String s2)
HashMap<Character, Integer> s1Map = stringToHashMap(s1);
HashMap<Character, Integer> s2Map = stringToHashMap(s2);
s2Map.forEach((key, value) -> s1Map.merge(key, value, Math::max));
return hashMapToString(s1Map);
以上是关于如何合并两个不同的字符串的主要内容,如果未能解决你的问题,请参考以下文章