Need Assistance: Good String (Educational Round no. 62) Div 2C

Revision en4, by ericdai1, 2020-07-30 04:41:18

Hi I attempted this problem in today's contest and I am very confused about this problem and what I am doing wrong. Firstly, my code is in Java (only language i'm good enough at to attempt these tough problems), and I decided to use HashMaps to store each SINGLE letter as well as each DOUBLE letter (ex: "ab", "ba", "cp", etc.) and got their total values at the end to see which one was in the string the most. That way, I knew how many letters I could keep and just subtracted that from the total string length to find the minimum number of deletions. However, it keeps giving the wrong answer for Test Case 2, and I don't know how to make my code any better. https://codeforces.me/contest/1389/problem/C


import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int i = 0; i < t; i++) { int max = 0; HashMap<String, Integer> map = new HashMap<String, Integer>(); String cyc = sc.next(); for (int j = 0; j < cyc.length(); j++) { String single = cyc.substring(j, j+1); if (j < cyc.length()-1 && !single.equals(cyc.substring(j+1,j+2))) { String doub = cyc.substring(j, j+2); if (map.containsKey(doub)) { map.replace(doub, map.get(doub)+1); max = Math.max(max, map.get(doub) *2); } else { map.put(doub, 1); max = Math.max(max, map.get(doub) *2); } } if (map.containsKey(single)) { map.replace(single, map.get(single)+1); max = Math.max(max, map.get(single)); } else { map.put(single, 1); max = Math.max(max, map.get(single)); } } int fin = cyc.length() - max; System.out.println(fin); } } }
Tags problem, div2c, string, good string

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en4 English ericdai1 2020-07-30 04:41:18 6
en3 English ericdai1 2020-07-30 04:40:36 117
en2 English ericdai1 2020-07-30 04:39:00 17
en1 English ericdai1 2020-07-30 04:38:40 1889 Initial revision (published)