作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
> 在这里我可以打印字符的重复次数,但我不能 能够了解如何比较和删除。这里我为两个字符串和键设置了两个映射作为字符,值作为重复
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/
我的程序运行良好,这只是我需要的一个小建议;首先这是我的代码,用于检查两个单词是否是字谜;效果很好; #include int anagram_check(char [], char []); in
面试问题给定两个输入字符串,您只能交换字符串中的两个连续元素。您必须展示将一个字符串转换为另一个字符串的所有步骤(两个字符串将是彼此的字谜)。例如。口香糖到马克杯 古姆百货公司通用汽车大学微量气体发生
我正在尝试学习 Python。考虑这个简单的 C 语言变位词检查器: bool are_anagrams(const char* str1, const char* str2) { int str
(下面关于我的问题的代码) 根据 this stack overflow question我使用 Pegolon 的方法来生成 NSString 中一组字符的所有可能排列。但是,我现在试图让它不仅生成
testing if strings are anagrams有很多种方式.但是,我想知道是否有一种方法可以只对每个单词进行一次迭代?如果不是,在 Python 中最有效的方法是什么? 我们可以遍历第
我是一名优秀的程序员,十分优秀!