-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f0c12e0
commit 5bf7b27
Showing
4 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
//{ Driver Code Starts | ||
/*package whatever //do not write package name here */ | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
class Node { | ||
char data; | ||
Node next; | ||
|
||
public Node(char data){ | ||
this.data = data; | ||
next = null; | ||
} | ||
} | ||
|
||
class GFG { | ||
public static void main (String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
int t = sc.nextInt(); | ||
while(t-- > 0){ | ||
int n = sc.nextInt(); | ||
Node head = null, tail = null; | ||
|
||
char head_c = sc.next().charAt(0); | ||
head = new Node(head_c); | ||
tail = head; | ||
|
||
while(n-- > 1){ | ||
tail.next = new Node(sc.next().charAt(0)); | ||
tail = tail.next; | ||
} | ||
|
||
Solution obj = new Solution(); | ||
//show(head); | ||
show(obj.arrangeCV(head)); | ||
|
||
} | ||
} | ||
|
||
public static void po(Object o){ | ||
System.out.println(o); | ||
} | ||
|
||
public static void show(Node head){ | ||
while(head != null){ | ||
System.out.print(head.data+" "); | ||
head = head.next; | ||
} | ||
System.out.println(); | ||
} | ||
} | ||
|
||
// } Driver Code Ends | ||
|
||
|
||
/* | ||
Structure of node class is: | ||
class Node { | ||
char data; | ||
Node next; | ||
public Node(char data){ | ||
this.data = data; | ||
next = null; | ||
} | ||
} | ||
*/ | ||
class Solution { | ||
|
||
public Node arrangeCV(Node head){ | ||
//write code here and return the head of changed linked list | ||
Node vowel = new Node('1'); | ||
Node v = vowel; | ||
|
||
Node constant = new Node('1'); | ||
Node c = constant; | ||
|
||
|
||
while(head!=null) | ||
{ | ||
if(isVowel(head.data)) | ||
{ | ||
v.next = new Node(head.data); | ||
v = v.next; | ||
} | ||
else | ||
{ | ||
c.next = new Node(head.data); | ||
c = c.next; | ||
} | ||
|
||
head = head.next; | ||
} | ||
|
||
v.next = constant.next; | ||
|
||
return vowel.next; | ||
} | ||
|
||
|
||
public static boolean isVowel(char a) | ||
{ | ||
if(a=='a' || a=='e' || a=='i' || a=='o' || a=='u') | ||
return true; | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class Solution { | ||
public String reversePrefix(String word, char ch) { | ||
int i = word.indexOf(ch) + 1; | ||
return new StringBuilder(word.substring(0, i)).reverse().append(word.substring(i)).toString(); | ||
} | ||
} |