Skip to content

Commit 516eb67

Browse files
add a random interview question
1 parent 24ceed5 commit 516eb67

File tree

1 file changed

+63
-11
lines changed

1 file changed

+63
-11
lines changed

src/main/java/com/fishercoder/solutions/_99999RandomQuestions.java

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@
99
import java.io.InputStreamReader;
1010
import java.net.HttpURLConnection;
1111
import java.net.URL;
12-
import java.util.ArrayList;
13-
import java.util.Collections;
14-
import java.util.List;
15-
import java.util.Map;
16-
import java.util.TreeMap;
12+
import java.util.*;
1713

1814
public class _99999RandomQuestions {
1915

@@ -117,14 +113,70 @@ public static void main(String... args) {
117113
// [[3,1],[5,1]],
118114
// [[5,3],[6,4]]]
119115

120-
int[][] result = findEdges(image4);
121-
System.out.println("here");
122-
for (int[] list : result) {
123-
for (int num : list) {
124-
System.out.println(num);
116+
// int[][] result = findEdges(image4);
117+
// System.out.println("here");
118+
// for (int[] list : result) {
119+
// for (int num : list) {
120+
// System.out.println(num);
121+
// }
122+
// }
123+
124+
System.out.println(isValidString("aabbcd"));//should be false
125+
System.out.println(isValidString("abc"));//should be true
126+
System.out.println(isValidString("aabbbbc"));//should be false
127+
System.out.println("ended");
128+
}
129+
130+
/**
131+
Consider a string to be valid if all characters of the string appear the same number of times.
132+
It is also valid if he can remove just a character at any one index in the string,
133+
and the remaining characters will occur the same number of times. Given a string ,
134+
determine if it is valid. If so, return YES, otherwise return NO.
135+
136+
For example,
137+
138+
1. abc, is a valid string because frequencies of a, b, c are 1 .
139+
2. aabc, is a valid string because you can remove an occurance of a and then frequencies of a, b, c would be 1.
140+
3. aaabc, is not a valid string because you remove maximumn one occurance of a, and the frequencies of b and c would be 1, where as a it is 2
141+
*/
142+
143+
public static boolean isValidString(String s) {
144+
if (s == null || s.length() == 0) {
145+
return false;
146+
}
147+
Map<Character, Integer> map = new HashMap();
148+
for (char c : s.toCharArray()) {
149+
if (map.containsKey(c)) {
150+
int times = map.get(c);
151+
map.put(c, times + 1);
152+
} else {
153+
map.put(c, 1);
125154
}
126155
}
127-
System.out.println("ended");
156+
//a-1, b-1, c-1
157+
//a-2, b-4, c-1
158+
//a-1, b-1, c-2
159+
//a-2, b-2, c-1, d -1
160+
// times1 2
161+
// times2 0
162+
int times1 = 0;//Integer.MIN_VALUE;
163+
int times2 = 0;
164+
for (Character key : map.keySet()) {
165+
times1 = map.get(key);
166+
break;
167+
}
168+
int diff = 0;
169+
for (Character key : map.keySet()) {
170+
if (map.get(key) != times1 && times2 == 0) {
171+
times2 = map.get(key);
172+
diff++;
173+
continue;
174+
}
175+
if (map.get(key) != times2 && map.get(key) != times1) {
176+
return false;
177+
}
178+
}
179+
return (Math.abs(times1 - times2) == 1) && diff < 2;
128180
}
129181

130182
/**

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy