Skip to content

Commit 733cdd7

Browse files
committed
Reverse vowels done + some comments refactoring
1 parent b6a3322 commit 733cdd7

File tree

4 files changed

+84
-3
lines changed

4 files changed

+84
-3
lines changed

src/main/java/com/leetcode/strings/AnagramsInString.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
public class AnagramsInString {
1414

1515
/**
16-
* Time complexity: O ((n-m) * m log m)
16+
* Time complexity: O((n-m) * m log m)
1717
* where,
1818
* n = text length
1919
* m = pattern length
@@ -50,10 +50,11 @@ private static List<Integer> findAllAnagramsInTextNaive(String text, String patt
5050
}
5151

5252
/**
53-
* Completes within <a href="https://leetcode.com/submissions/detail/222911434/">7ms on leetcode.</a>
5453
* Time complexity: O(n)
5554
* where,
5655
* n = length of text or number of characters in text
56+
* <p>
57+
* Runtime: <a href="https://leetcode.com/submissions/detail/222911434/">7 ms on leetcode</a>.
5758
*
5859
* @param text
5960
* @param pattern
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.leetcode.strings;
2+
3+
/**
4+
* Leetcode Problem: https://leetcode.com/problems/reverse-vowels-of-a-string/
5+
*
6+
* @author rampatra
7+
* @since 2019-04-19
8+
*/
9+
public class ReverseVowels {
10+
11+
/**
12+
* Reverse only the vowels in the string {@code str}.
13+
* <p>
14+
* Time Complexity: O(n)
15+
* where,
16+
* n = no. of characters in the string
17+
* <p>
18+
* Runtime: <a href="https://leetcode.com/submissions/detail/223519160/">2 ms on leetcode</a>.
19+
*
20+
* @param str
21+
* @return
22+
*/
23+
private static String reverseVowels(String str) {
24+
25+
char[] chars = str.toCharArray();
26+
char temp;
27+
int left = 0;
28+
int right = str.length() - 1;
29+
30+
while (left < right) {
31+
// find the vowel from left
32+
while (!isVowel(chars[left]) && left < right) {
33+
left++;
34+
}
35+
// find the vowel from right
36+
while (!isVowel(chars[right]) && left < right) {
37+
right--;
38+
}
39+
40+
if (!isVowel(chars[left]) || !isVowel(chars[right])) {
41+
break;
42+
}
43+
44+
// swap the characters
45+
temp = chars[left];
46+
chars[left] = chars[right];
47+
chars[right] = temp;
48+
49+
left++;
50+
right--;
51+
}
52+
return new String(chars);
53+
}
54+
55+
private static boolean isVowel(char c) {
56+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
57+
c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U';
58+
}
59+
60+
public static void main(String[] args) {
61+
System.out.println(reverseVowels("hello"));
62+
System.out.println(reverseVowels("a"));
63+
System.out.println(reverseVowels(""));
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.leetcode.strings;
2+
3+
/**
4+
* @author rampatra
5+
* @since 2019-04-16
6+
*/
7+
public class StringCompression {
8+
9+
private static int compress(char[] chars) {
10+
return -1;
11+
}
12+
13+
public static void main(String[] args) {
14+
}
15+
}

src/main/java/com/leetcode/strings/UniqueCharacterInString.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
public class UniqueCharacterInString {
1010

1111
/**
12-
* Completes within <a href="https://leetcode.com/submissions/detail/222914261/">7ms on leetcode.</a>
1312
* Time complexity: O(n)
13+
* Runtime: <a href="https://leetcode.com/submissions/detail/222914261/">7 ms on leetcode</a>.
1414
*
1515
* @param str the input string
1616
* @return the index of the first non-repeating character in {@code str}, {@code -1} otherwise.

0 commit comments

Comments
 (0)