-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1704. Determine if String Halves Are Alike.cpp
75 lines (60 loc) · 1.62 KB
/
1704. Determine if String Halves Are Alike.cpp
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
/*--------method - 1-----------------*/
// Time complexity - O(N)
// Space complexity- O(10)
class Solution {
public:
bool flag=0;
unordered_map<char,int> map;
void help()
{
map['A']=map['E']=map['I']=map['O']=map['U']=1;
map['a']=map['e']=map['i']=map['o']=map['u']=1;
flag=1;
}
bool halvesAreAlike(string s) {
if(s.length()&1)
return 0;
if(!flag)
help();
int cnt1=0;
int cnt2=0;
for(int i=0;i<s.length();i++)
{
char it=s[i];
if(i>=(s.length()/2) and map.count(it))
cnt1++;
if(i<(s.length()/2) and map.count(it))
cnt2++;
}
if(cnt1==cnt2)
return 1;
else
return 0;
}
};
/*--------method - 2--------------*/
// Time complexity - O(N)
// Space complexity- O(1)
class Solution {
public:
bool halvesAreAlike(string s) {
if(s.length()&1)
return 0;
int cnt1=0;
int cnt2=0;
for(int i=0;i<s.length();i++)
{
char it=s[i];
if(it=='A' or it=='E' or it=='I' or it=='O' or it=='U' or
it=='a' or it=='e' or it=='i' or it=='o' or it=='u')
cnt1++;
if(i<(s.length()/2) and (it=='A' or it=='E' or it=='I' or it=='O' or it=='U' or
it=='a' or it=='e' or it=='i' or it=='o' or it=='u'))
cnt2++;
}
if(cnt1-cnt2==cnt2)
return 1;
else
return 0;
}
};