Answers for "sherlock and the valid string c#"

0

sherlock and the valid string c#

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    static String isValid(String s){
        HashMap<Character, Integer> count_char = new HashMap<Character, Integer>();
        int del_count = 0;
        String result = "YES";

        for(int i = 0; i < s.length(); i++){
            if(count_char.containsKey(s.charAt(i))){
                count_char.put(s.charAt(i), count_char.get(s.charAt(i)) +1 );
            }else{
                count_char.put(s.charAt(i), 1);        
            }
        }
        int current = 0;
        for (Map.Entry<Character, Integer> count : count_char.entrySet()){
            if(current == 0){
                current = count.getValue();
            }else{
                int diff = Math.abs(current - count.getValue());
                if(count.getValue() > 1 && diff > 0){
                    del_count += diff;
                }else if(count.getValue() == 1 && diff > 0){
                    del_count++;
                }
            }
        }
        if(del_count > 1){
            result = "NO";
        }
        return result;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.next();
        String result = isValid(s);
        System.out.println(result);
    }
}
Posted by: Guest on August-31-2020

Code answers related to "sherlock and the valid string c#"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language