-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringProblems
More file actions
150 lines (93 loc) · 3.57 KB
/
StringProblems
File metadata and controls
150 lines (93 loc) · 3.57 KB
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
// problem 1: print duplicate character
public static void printDuplicateChar() {
String str = "Ajay Rajput";
HashMap<Character, Integer> duplicateMap = new HashMap<>();
char wordArray[] = str.toCharArray();
for (Character ch : wordArray) {
if (duplicateMap.containsKey(ch)) {
duplicateMap.put(ch, duplicateMap.get(ch) + 1);
} else {
duplicateMap.put(ch, 1);
}
}
System.out.println(duplicateMap);
Iterator<Map.Entry<Character, Integer>> itr = duplicateMap.entrySet().iterator();
System.out.println("Duplicate character print here");
while (itr.hasNext()) {
Map.Entry<Character, Integer> entry = itr.next();
if (entry.getValue() > 1 && !entry.getKey().equals(" "))
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
// problem 2: check anagram number
public static boolean checkAnagram(String str1, String str2) {
//boolean isAnagram = false;
char character[] = str1.toLowerCase().toCharArray();
StringBuffer stringBuffer = new StringBuffer(str2);
if (str1.length() != str2.length())
return false;
for (Character ch : character) {
int index = stringBuffer.indexOf("" + ch);
if (index != -1) {
stringBuffer.deleteCharAt(index);
} else return false;
}
return stringBuffer.length() == 0 ? true : false;
}
// problem 3:
public static char firstNonRepeatingChar(String str1) {
char firstChar = ' ';
char character[] = str1.toLowerCase().toCharArray();
//use linked hashMap bcz we have need for order
Map<Character, Integer> characterIntegerHashMap = new LinkedHashMap<>();
for (Character ch : character) {
if (characterIntegerHashMap.containsKey(ch)) {
characterIntegerHashMap.put(ch, characterIntegerHashMap.get(ch) + 1);
} else {
characterIntegerHashMap.put(ch, 1);
}
}
for (Map.Entry<Character, Integer> entry : characterIntegerHashMap.entrySet()) {
if (entry.getValue() == 1) {
firstChar = entry.getKey();
break;
}
}
return firstChar;
}
// problem 4:
public static void reverseString(){
String revStr="Ajay Rajput";
StringBuffer stringBuffer=new StringBuffer();
char charArray[]=revStr.toCharArray();
for (int i=charArray.length-1;i>=0;i--){
stringBuffer.append(charArray[i]);
}
System.out.println("Reverse String \n"+stringBuffer);
// second way
StringBuffer strBuffer=new StringBuffer(revStr);
strBuffer.reverse();
System.out.println("Reverse String \n"+strBuffer);
}
// problem 5:
public static void countConsonantVolels(){
String strCount="There is a many subString found";
char [] charArray=strCount.toLowerCase().toCharArray();
int count=0 ,space=0;
for (char ch:charArray){
switch (ch){
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
count++;
break;
case ' ':
space++;
break;
default:
}
}
System.out.println("Vovels is "+count +" consonants is "+(charArray.length-(count+space)));
}