-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathNumberOfGoodSplits.java
91 lines (81 loc) · 2.64 KB
/
NumberOfGoodSplits.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*https://leetcode.com/problems/number-of-good-ways-to-split-a-string/*/
/*https://binarysearch.com/problems/Split-String-with-Same-Distinct-Counts*/
/*Prateekshya's Solution*/
class Solution {
public int numSplits(String s) {
if (s.length() <= 1) return 0;
int[] leftHash = new int[26];
int[] rightHash = new int[26];
int leftUnique = 0, rightUnique = 0, goodSplitCount = 0;
//for the initial window
++leftHash[(int)s.charAt(0)-97];
++leftUnique;
for (int i = 1; i < s.length(); ++i)
{
int index = (int)s.charAt(i)-97;
++rightHash[index];
if (rightHash[index] == 1)
++rightUnique;
}
if (leftUnique == rightUnique)
++goodSplitCount;
//for next windows
for (int i = 1; i < s.length()-1; ++i)
{
int index = (int)s.charAt(i)-97;
//delete the current character from right hashtable
--rightHash[index];
//if the count becomes 0 then decrease the right unique count
if (rightHash[index] == 0)
--rightUnique;
//add the current character to the left hash table
++leftHash[index];
//if the count becomes 1, increment the left unique count
if (leftHash[index] == 1)
++leftUnique;
//if the unique counts are equal then increment the split count
if (leftUnique == rightUnique)
++goodSplitCount;
}
return goodSplitCount;
}
}
/*Pratik's Solution*/
class Solution {
public int numSplits(String s) {
HashMap<Character,Integer> mapP = new HashMap<Character,Integer>();
HashMap<Character,Integer> mapQ = new HashMap<Character,Integer>();
int count = 0;
for(int i=0;i<s.length();i++)
{
if(mapQ.containsKey(s.charAt(i)))
{
mapQ.put(s.charAt(i),mapQ.get(s.charAt(i))+1);
}
else
{
mapQ.put(s.charAt(i),1);
}
}
int i = 0;
while(mapQ.size()!=0)
{
if(mapP.size()==mapQ.size())count++;
if(mapP.containsKey(s.charAt(i)))
{
mapP.put(s.charAt(i),mapP.get(s.charAt(i))+1);
}
else
{
mapP.put(s.charAt(i),1);
}
mapQ.put(s.charAt(i),mapQ.get(s.charAt(i))-1);
if(mapQ.get(s.charAt(i))==0)
{
mapQ.remove(s.charAt(i));
}
i++;
}
return count;
}
}