Skip to content

Commit 2338d99

Browse files
Merge branch 'SarthakKeshari:master' into master
2 parents ca3f25c + cd3d03b commit 2338d99

2 files changed

Lines changed: 83 additions & 0 deletions

File tree

GeeksForGeeks/CountText.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//Counting number of lines, words, characters and paragraphs in a text file using Java
2+
3+
import java.io.*;
4+
5+
public class CountText {
6+
public static void main(String[] args)
7+
throws IOException
8+
{
9+
File file = new File(
10+
"C:\\TextReader.txt"); //Read text from TextReader.txt file
11+
FileInputStream fileInputStream
12+
= new FileInputStream(file);
13+
InputStreamReader inputStreamReader
14+
= new InputStreamReader(fileInputStream);
15+
BufferedReader bufferedReader
16+
= new BufferedReader(inputStreamReader);
17+
18+
/*
19+
* BufferedReader is a class which simplifies reading text from a character input stream.
20+
* It buffers the characters in order to enable efficient reading of text data.
21+
*/
22+
23+
String line;
24+
int WordCount = 0;
25+
int CharacterCount = 0;
26+
int ParagraphCount = 0;
27+
int WhiteSpaceCount = 0;
28+
int SentenceCount = 0;
29+
30+
while ((line = bufferedReader.readLine()) != null) {
31+
if (line.equals("")) {
32+
ParagraphCount += 1;
33+
}
34+
else {
35+
CharacterCount += line.length();
36+
String words[] = line.split("\\s+");
37+
WordCount += words.length; //count words from file
38+
WhiteSpaceCount += WordCount - 1;
39+
String sentence[] = line.split("[!?.:]+"); //count lines from file
40+
SentenceCount += sentence.length;
41+
}
42+
}
43+
if (SentenceCount >= 1) {
44+
ParagraphCount++;
45+
}
46+
System.out.println("Total word count = "
47+
+ WordCount);
48+
System.out.println("Total number of sentences = "
49+
+ SentenceCount);
50+
System.out.println("Total number of characters = "
51+
+ CharacterCount);
52+
System.out.println("Number of paragraphs = "
53+
+ ParagraphCount);
54+
System.out.println("Total number of whitespaces = "
55+
+ WhiteSpaceCount);
56+
}
57+
}

LeetCode/DeleteNode.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// https://leetcode.com/problems/delete-node-in-a-linked-list/
2+
// O(n) solution
3+
4+
//Aproach: The question gives access to the node to be deleted instead of the head node. The node to be deleted will not
5+
//be a tail node hence we will set the value of the current node to be that of the next node and set the next of the
6+
//current node to the next of it's next. Simple and efficient solution, 0ms runtime.
7+
8+
public class DeleteNode {
9+
public class ListNode {
10+
int val;
11+
ListNode next;
12+
13+
ListNode(int x) {
14+
this.val = x;
15+
}
16+
}
17+
public void delete(ListNode node) {
18+
if (node.next.next != null) {
19+
node.val = node.next.val;
20+
node.next = node.next.next;
21+
} else if (node.next.next == null) {
22+
node.val = node.next.val;
23+
node.next = null;
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)