Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
549 views
in Online Assessments by Expert (46,090 points) | 549 views

2 Answers

0 like 0 dislike
Best answer
Given a string s, find the minimum number of substrings you can create without having the same letters repeating in each substring.
Example: 
    world -> 1, as the string has no letters that occur more than once. 
    dddd -> 4, as you can only create substring of each character.
    abba -> 2, as you can make substrings of ab, ba.	
    cycle-> 2, you can create substrings of (cy, cle) or (c, ycle)
 
by Expert (46,090 points)
0 like 0 dislike
 public static int subStringsWithNoRepeat(String s) {
		if (s == null || s.isEmpty()) {
			return 0;
		}
		Set set = new HashSet<>();
		int res = 1;
		for (char c : s.toCharArray()) {
			if (set.contains(c)) {
				res++;
				set.clear();
			}
			set.add(c);
		}
		return res;
	}
by Expert (46,090 points)