gpt4 book ai didi

java - 通过删除字符而不是删除次数来制作字谜后打印两个字符串?

转载 作者:行者123 更新时间:2023-12-02 10:43:03 27 4
gpt4 key购买 nike

> 在这里我可以打印字符的重复次数,但我不能 能够了解如何比较和删除。这里我为两个字符串和键设置了两个映射作为字符,值作为重复

public class NoOfDeletionsTomakeStringAnagram {

@SuppressWarnings("resource")
public static void main(String[] args) {
System.out.println("string");
Scanner sc = new Scanner(System.in);
System.out.println("enter string 1");
String s1 = sc.next();
System.out.println("enter string 2");
String s2 = sc.next();
Map<Character, Integer> m1s1 = new HashMap<>();
Map<Character, Integer> m2s2 = new HashMap<>();

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

if (m1s1.containsKey(s1.charAt(i))) {
m1s1.put((Character) s1.charAt(i), m1s1.get((Character)
s1.charAt(i)) + 1);
} else {

m1s1.put((Character) s1.charAt(i), 1);

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

if (m2s2.containsKey(s2.charAt(i))) {
m2s2.put((Character) s2.charAt(i), m2s2.get((Character)
s2.charAt(i)) + 1);
} else {
m2s2.put((Character) s2.charAt(i), 1);

}
}
System.out.println("m1s1....." + m1s1);
System.out.println("m221...." + m2s2);
}
}

Samlie 输入: s1=abc s2=cde 这里我们必须通过从两个字符串中删除字符来制作字谜 输出: s1=c s2=c

最佳答案

    /* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;



class Ideone
{

public static void main (String[] args) throws java.lang.Exception
{

System.out.println("string");
Scanner sc = new Scanner(System.in);
System.out.println("enter string 1");
String s1 = sc.next();
System.out.println("enter string 2");
String s2 = sc.next();
Map<Character, Integer> m1s1 = new HashMap<>();
Map<Character, Integer> m2s2 = new HashMap<>();

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

if (m1s1.containsKey(s1.charAt(i))) {
m1s1.put((Character) s1.charAt(i), m1s1.get((Character)
s1.charAt(i)) + 1);
} else {

m1s1.put((Character) s1.charAt(i), 1);

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

if (m2s2.containsKey(s2.charAt(i))) {
m2s2.put((Character) s2.charAt(i), m2s2.get((Character)
s2.charAt(i)) + 1);
} else {
m2s2.put((Character) s2.charAt(i), 1);

}
}
System.out.println("m1s1....." + m1s1);
System.out.println("m221...." + m2s2);

// ADDED MY CODE FROM HERE
// TAKEN STRING 1 AND MAP OF STRING 2, CHECK CHARACTER BY CHARACTER OF STRING 1 IN MAP //OF STRING 2, IF THE CHARACTER EXISTS PRINT IT & DECREASE ITS VALUE IN MAP BY 1 , IF //AFTER DECREASING THE VALUE BECOMES 0, REMOVE IT FROM MAP, IF THE CHARACTER OF //STRING1 DOES NOT EXIST IN MAP2 DON'T DO ANYTHING, FOLLOW ABOVE RULES FOR EVERY //CHARACTER OF STRING 1

// NOW TAKE STRING2 AND MAP OF STRING1 AND FOLLOW THE EXACT PROCEDURE ABOVE

int i,x;
char c;

for(i=0;i<s1.length();i++)
{
c=s1.charAt(i);
if(m2s2.containsKey(c))
{
System.out.print(c);
x=m2s2.get((Character)c);
x=x-1;
if(x==0)
m2s2.remove(new Character(c));
else
m2s2.put((Character) c,x);
}
}
System.out.println();

for(i=0;i<s2.length();i++)
{
c=s2.charAt(i);
if(m1s1.containsKey(c))
{
System.out.print(c);
x=m1s1.get((Character)c);
x=x-1;
if(x==0)
m1s1.remove(new Character(c));
else
m1s1.put((Character) c,x);
}
}


}
}

关于java - 通过删除字符而不是删除次数来制作字谜后打印两个字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52795317/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com