A simple algorithmic question! (Check if it is the same character pair)

Asked 2 years ago, Updated 2 years ago, 73 views

Hello! It's a simple question. Hmm... I don't know what to do with the algorithm I'm trying to get two strings from the user and check if they're the same For example, even if the string is flipped, the same character is used! If it's the same character pair, it's true. If it's different, it's a question that prints false Even if the strings are different, it says true. What's the reason? When I say apple apple, it should say it's wrong, but it says true. Where is wrong?

public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        String a = sc.next();
        String b = sc.next();
        boolean confirm = false;
        int[] arr_a = new int[26];
        int[] arr_b = new int[26];
        boolean c = false;
        char[] chk_a = a.toCharArray();
        char[] chk_b = b.toCharArray();


        if(chk_a.length==chk_b.length){


            for(int i=0;i<chk_a.length;i++){
                int tmp = chk_a[i]-'a';
                arr_a[tmp]++;
            }

            for(int j=0;j<chk_b.length;j++){
                int tmp2 = chk_b[j]-'a';
                arr_b[tmp2]++;
            }

        //This code below is the code in question!!1


            for(int i=0;i<arr_a.length;i++){
                if(arr_a[i]==arr_b[i]){

                    if(i==arr_a.length-1){
                        confirm=true;
                    }

                }
                else{break;}
            }

            if(confirm=true){
                System.out.println("true");
            }
            else{
                System.out.println("fail");
            }
        }
        else System.out.println("fail");








    }
}// Enter code here

java data-structure algorithm

2022-09-22 20:56

2 Answers

It's because I don't understand the problem properly. If there are the same number of characters in the input strings a and b, is it a program that outputs true or fails? If so, you can do it as below!

public static void main(String[] args) {
 Scanner sc = new Scanner(System.in);
 String a = sc.next();
 String b = sc.next();
 int[] cntA = new int[26];
 int[] cntB = new int[26];
 char[] arrA = a.toCharArray();
 char[] arrB = b.toCharArray();

 if (arrA.length == arrB.length) {
  for (int i = 0; i < arrA.length; i++) {
   cntA[arrA[i] - 'a']++;
   cntB[arrB[i] - 'a']++;
  }
  int i;
  for (i = 0; i < 26; i++) {
   if (cntA[i] != cntB[i]) {
    System.out.println("fail");
    break;
   }
  }
  if (i == 26) {
   System.out.println("true");
  }
 } } else {
  System.out.println("fail");
 }
}

I don't know Java grammar well, but I made it once. We put the number of characters in the string in cntA and cntB, and compared cntA and cntB for 26 times from 0 to 25, and if it is not the same, print fail, exit the for statement, and print true if it has been operated for 26 times.


2022-09-22 20:56

Just receive one string and then dedupe

After deduplicating the following characters

Can't we compare it?


2022-09-22 20:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.