Share my clear java solution


  • 0
    R
    public class Solution {
    public String getHint(String s, String g) {
        int[] cs = new int[255];
        int[] cg = new int[255];
        int n = s.length();
        int m = g.length();
        
        int A = 0;
        int B = 0;
        
        for(int i = 0 ; i < n; i++) {
            cs[s.charAt(i)]++;
        }
        
        for(int i = 0 ; i < m; i++) {
            cg[g.charAt(i)]++;
        }
        
        for(int i = 0; i < 255; i++) {
            B += Math.min(cs[i], cg[i]);
        }
        
        for(int i = 0 ; i < n; i++){
            if(s.charAt(i) == g.charAt(i)){
                B--;
                A++;
            }
        }
        return A+"A"+B+"B";
        
     }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.