-
Notifications
You must be signed in to change notification settings - Fork 0
/
strings.java
128 lines (100 loc) · 3.12 KB
/
strings.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
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
import java.util.*;
public class strings {
public static void print_letter(String str) {
for(int i = 0 ; i < str.length();i++){
System.out.print(str.charAt(i) + " ");
}
}
public static boolean palindrome(String str){
for (int i = 0; i < str.length()/2; i++) {
if (str.charAt(i) != str.charAt(str.length() - i -1)) {
return false;
}
}
return true;
}
public static int lengthOfLongestSubstring(String s) {
int count = 0;
int first = 0;
int second = 1;
while (first < (s.length()-1)) {
if (s.charAt(first) == s.charAt(second)) {
count = Math.max(second-first,count);
first++;
second = first+1;
}else if (second == (s.length() - 1)){
first++;
second = first+1;
}
else{
second++;
}
}
return count;
}
public static float shortest_path(String str){
int x = 0 , y = 0;
for(int i =0 ; i<str.length();i++){
char dic = str.charAt(i);
if (dic == 'N') {
y++;
}else if(dic== 'S'){
y--;
}else if(dic == 'E'){
x++;
}else{
x--;
}
}
return (float)Math.sqrt(x*x + y*y);
}
public static int lower_vowel_check(String str) {
int count = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch == 'a' || ch == 'e'||ch == 'o' || ch == 'i' || ch == 'u'){
count++;
}
}
return count;
}
public static void check_anagram(String str1,String str2) {
str1 = str1.toLowerCase();
str2 = str2.toLowerCase();
if (str1.length() == str2.length()) {
char[] arr1 = str1.toCharArray();
char[] arr2 = str2.toCharArray();
Arrays.sort(arr1);
Arrays.sort(arr2);
boolean result = Arrays.equals(arr1, arr2);
if(result){
System.out.println("Given Stings are anagrams");
}else{
System.out.println("Not Anagram");
}
}else{
System.out.println("not equal length");
}
}
public static void repeat_counter(String str) {
str = str.toLowerCase();
char[] strs = str.toCharArray();
Arrays.sort(strs);
int count = 0;
char lastChar = strs[0];
for (char c : strs) {
if (lastChar == c) {
count++;
}else{
System.out.println(lastChar + " count = " + count);
lastChar = c;
count = 1;
}
}
System.out.println(lastChar + " count = " + count);
}
public static void main(String[] args) {
String adi = "bbbbb";
System.out.println(lengthOfLongestSubstring(adi));
}
}