Your window size should not exceed k if it is exceeding k you need to release the characters ====================================================== //Earlier we have checked for unique strings int release=0; for(int i=0;ik) { Character ch=str.charAt(release); int x=hm.get(ch); x--; hm.put(ch,x) if(hm.get(ch)==0) { hm.remove(ch); } release++; } count=count+(i-release+1); // max=Math.max(max,(i-release+1)); } return max or count ==================================== k=3 str="geeksforgeeks" g 1 e 2 k 1 s 1 https://www.naukri.com/code360/problems/distinct-characters_2221410?leftPanelTabValue=SUBMISSION import java.util.*; public class Solution { public static int kDistinctChars(int k, String str) { // Write your code here int count=0; int release=0; int max=-1; HashMap hm=new HashMap<>(); for(int i=0;ik){ Character relchar=str.charAt(release); int y=hm.get(relchar); y--; hm.put(relchar,y); if(hm.get(relchar)==0){ hm.remove(relchar); } release++; } max=Math.max(max,(i-release+1)); } return max; } } =================================================================== Total Unique elements in each window ==================================================================