-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathString-DS
341 lines (254 loc) · 6.6 KB
/
String-DS
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
//String Programs-Java and Python
//Reverse a string in Java
// java program to reverse a word
import java.io.*;
import java.util.Scanner;
class GFG {
public static void main (String[] args) {
String str= "SVCE", nstr="";
char ch;
System.out.print("Original word: ");
System.out.println("SVCE"); //Example word
for (int i=0; i<str.length(); i++)
{
ch= str.charAt(i); //extracts each character
nstr= ch+nstr; //adds each character in front of the existing string
}
System.out.println("Reversed word: "+ nstr);
}
}
//Reverse a string in Python using a loop
def reverse(s):
str = ""
for i in s:
str = i + str
return str
s = "Geeksforgeeks"
print("The original string is : ", end="")
print(s)
print("The reversed string(using loops) is : ", end="")
print(reverse(s))
//Java Program to Count Number of Vowels in a String
// Java Program to Count Number of Vowels
// in a String in a iterative way
import java.io.*;
public class vowel {
public static void main(String[] args)
throws IOException
{
String str = "SvceCollege";
str = str.toLowerCase();
int count = 0;
for (int i = 0; i < str.length(); i++) {
// check if char[i] is vowel
if (str.charAt(i) == 'a' || str.charAt(i) == 'e'
|| str.charAt(i) == 'i'
|| str.charAt(i) == 'o'
|| str.charAt(i) == 'u') {
// count increments if there is vowel in
// char[i]
count++;
}
}
// display total count of vowels in string
System.out.println(
"Total no of vowels in string are: " + count);
}
}
//Python program to count number of vowels using sets in given string
# Python3 code to count vowel in
# a string using set
# Function to count vowel
def vowel_count(str):
# Initializing count variable to 0
count = 0
# Creating a set of vowels
vowel = set("aeiouAEIOU")
# Loop to traverse the alphabet
# in the given string
for alphabet in str:
# If alphabet is present
# in set vowel
if alphabet in vowel:
count = count + 1
print("No. of vowels :", count)
# Driver code
str = "SvcnCollege"
# Function Call
vowel_count(str)
//Substring in Java
// Java code to demonstrate the
// working of substring(int begIndex)
public class Substr1 {
public static void main(String args[])
{
// Initializing String
String Str = new String("Welcome to svcn college");
System.out.print("The extracted substring is : ");
System.out.println(Str.substring(10));
}
}
//Python | Check if a Substring is Present in a Given String
# input strings str1 and substr
string = "Svcn college Svcn" # or string=input() -> taking input from the user
substring = "college" # or substring=input()
# splitting words in a given string
s = string.split()
# checking condition
# if substring is present in the given string then it gives output as yes
if substring in s:
print("yes")
else:
print("no")
//Count Uppercase, Lowercase, special character and numeric values
// Java program to count the uppercase,
// lowercase, special characters
// and numeric values
import java.io.*;
class Count
{
public static void main(String args[])
{
String str = "#SvCe01fOr@sVc07";
int upper = 0, lower = 0, number = 0, special = 0;
for(int i = 0; i < str.length(); i++)
{
char ch = str.charAt(i);
if (ch >= 'A' && ch <= 'Z')
upper++;
else if (ch >= 'a' && ch <= 'z')
lower++;
else if (ch >= '0' && ch <= '9')
number++;
else
special++;
}
System.out.println("Lower case letters : " + lower);
System.out.println("Upper case letters : " + upper);
System.out.println("Number : " + number);
System.out.println("Special characters : " + special);
}
}
//Python
# Python 3 program to count the uppercase,
# lowercase, special characters
# and numeric values
# Function to count uppercase, lowercase,
# special characters and numbers
def Count(str):
upper, lower, number, special = 0, 0, 0, 0
for i in range(len(str)):
if str[i].isupper():
upper += 1
else if str[i].islower():
lower += 1
else if str[i].isdigit():
number += 1
else:
special += 1
print('Upper case letters:', upper)
print('Lower case letters:', lower)
print('Number:', number)
print('Special characters:', special)
# Driver Code
str = "#sVcE01fOr@SvCe07"
Count(str)
//Number of substrings with count of each character as k
// Java program to count number of substrings
// with counts of distinct characters as k.
class substring_prog
{
static int MAX_CHAR = 26;
// Returns true if all values
// in freq[] are either 0 or k.
static boolean check(int freq[], int k)
{
for (int i = 0; i < MAX_CHAR; i++)
if (freq[i] !=0 && freq[i] != k)
return false;
return true;
}
// Returns count of substrings where frequency
// of every present character is k
static int substrings(String s, int k)
{
int res = 0; // Initialize result
// Pick a starting point
for (int i = 0; i< s.length(); i++)
{
// Initialize all frequencies as 0
// for this starting point
int freq[] = new int[MAX_CHAR];
// One by one pick ending points
for (int j = i; j<s.length(); j++)
{
// Increment frequency of current char
int index = s.charAt(j) - 'a';
freq[index]++;
// If frequency becomes more than
// k, we can't have more substrings
// starting with i
if (freq[index] > k)
break;
// If frequency becomes k, then check
// other frequencies as well.
else if (freq[index] == k &&
check(freq, k) == true)
res++;
}
}
return res;
}
// Driver code
public static void main(String[] args)
{
String s = "aabbcc";
int k = 2;
System.out.println(substrings(s, k));
s = "aabbc";
k = 2;
System.out.println(substrings(s, k));
}
}
//In Python
# Python3 program to count number of substrings
# with counts of distinct characters as k.
MAX_CHAR = 26
# Returns true if all values
# in freq[] are either 0 or k.
def check(freq, k):
for i in range(0, MAX_CHAR):
if(freq[i] and freq[i] != k):
return False
return True
# Returns count of substrings where
# frequency of every present character is k
def substrings(s, k):
res = 0 # Initialize result
# Pick a starting point
for i in range(0, len(s)):
# Initialize all frequencies as 0
# for this starting point
freq = [0] * MAX_CHAR
# One by one pick ending points
for j in range(i, len(s)):
# Increment frequency of current char
index = ord(s[j]) - ord('a')
freq[index] += 1
# If frequency becomes more than
# k, we can't have more substrings
# starting with i
if(freq[index] > k):
break
elif(freq[index] == k and
check(freq, k) == True):
res += 1
return res
# Driver Code
if __name__ == "__main__":
s = "aabbcc"
k = 2
print(substrings(s, k))
s = "aabbc";
k = 2;
print(substrings(s, k))