Java 在通过删除字符而不是删除次数使两个字符串变位后打印它们?
>在这里,我可以打印字符的重复次数,但我不能 能够得到如何比较和删除。这里我为两个字符串和键取了两个映射作为字符,值作为重复Java 在通过删除字符而不是删除次数使两个字符串变位后打印它们?,java,string,Java,String,>在这里,我可以打印字符的重复次数,但我不能 能够得到如何比较和删除。这里我为两个字符串和键取了两个映射作为字符,值作为重复 public类noofdeletionTosthTringAngram{ @抑制警告(“资源”) 公共静态void main(字符串[]args){ System.out.println(“字符串”); 扫描仪sc=新的扫描仪(System.in); System.out.println(“输入字符串1”); 字符串s1=sc.next(); System.out.pri
public类noofdeletionTosthTringAngram{
@抑制警告(“资源”)
公共静态void main(字符串[]args){
System.out.println(“字符串”);
扫描仪sc=新的扫描仪(System.in);
System.out.println(“输入字符串1”);
字符串s1=sc.next();
System.out.println(“输入字符串2”);
字符串s2=sc.next();
Map m1s1=新的HashMap();
Map m2s2=新的HashMap();
对于(int i=0;i
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.util.*;
导入java.lang.*;
导入java.io.*;
表意文字
{
公共静态void main(字符串[]args)引发java.lang.Exception
{
System.out.println(“字符串”);
扫描仪sc=新的扫描仪(System.in);
System.out.println(“输入字符串1”);
字符串s1=sc.next();
System.out.println(“输入字符串2”);
字符串s2=sc.next();
Map m1s1=新的HashMap();
Map m2s2=新的HashMap();
对于(int i=0;i 对于(i=0;我不知道你们为什么投反对票。我也通过发布代码来询问我的疑问。你能在你的问题中添加示例输入和输出吗?不清楚你在问什么。@NicholasK示例输入和输出添加了什么问题?你被困在哪里了?我不知道如何迭代删除字符以使anagramThanks变得非常@anupam、 .非常有用
/* 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);
}
}
}
}