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

标签 java string

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

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/

相关文章:

c++ - 为什么C++标准要保留这样一个冗余函数呢?

java - 如何在java或php中获取单词的所有词形推导

java - 奇怪的字符串越界异常

c# - string.insert 多个值。这可能吗?

javascript - 如何替换javascript字符串中的圆括号标签

string - 查找与查询 : how to use a suffix tree? 匹配的所有名称

java - 在 liferay 门户中如何将多 session 限制为基本 session 模式?

java - 2个类加载器之间的交互

java - 如何从文件路径中获取文件所在的文件夹路径?

c++ - 异常错误 : Access violation reading location 0xDDDDDDDD