|
9 | 9 | import java.io.InputStreamReader;
|
10 | 10 | import java.net.HttpURLConnection;
|
11 | 11 | 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.*; |
17 | 13 |
|
18 | 14 | public class _99999RandomQuestions {
|
19 | 15 |
|
@@ -117,14 +113,70 @@ public static void main(String... args) {
|
117 | 113 | // [[3,1],[5,1]],
|
118 | 114 | // [[5,3],[6,4]]]
|
119 | 115 |
|
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); |
125 | 154 | }
|
126 | 155 | }
|
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; |
128 | 180 | }
|
129 | 181 |
|
130 | 182 | /**
|
|
0 commit comments